~ubuntu-branches/debian/experimental/php-nette/experimental

« back to all changes in this revision

Viewing changes to Nette-2.1.0RC/Nette/Latte/Macros/MacroSet.php

  • Committer: Package Import Robot
  • Author(s): David Prévot
  • Date: 2013-11-30 08:47:54 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20131130084754-4udf1xsu9085tnfc
Tags: 2.1.0~rc-1
* New upstream branch
* Update copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/**
 
4
 * This file is part of the Nette Framework (http://nette.org)
 
5
 *
 
6
 * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
 
7
 *
 
8
 * For the full copyright and license information, please view
 
9
 * the file license.txt that was distributed with this source code.
 
10
 */
 
11
 
 
12
namespace Nette\Latte\Macros;
 
13
 
 
14
use Nette,
 
15
        Nette\Latte,
 
16
        Nette\Latte\MacroNode;
 
17
 
 
18
 
 
19
/**
 
20
 * Base IMacro implementation. Allows add multiple macros.
 
21
 *
 
22
 * @author     David Grudl
 
23
 */
 
24
class MacroSet extends Nette\Object implements Latte\IMacro
 
25
{
 
26
        /** @var Latte\Compiler */
 
27
        private $compiler;
 
28
 
 
29
        /** @var array */
 
30
        private $macros;
 
31
 
 
32
 
 
33
        public function __construct(Latte\Compiler $compiler)
 
34
        {
 
35
                $this->compiler = $compiler;
 
36
        }
 
37
 
 
38
 
 
39
        public function addMacro($name, $begin, $end = NULL, $attr = NULL)
 
40
        {
 
41
                $this->macros[$name] = array($begin, $end, $attr);
 
42
                $this->compiler->addMacro($name, $this);
 
43
                return $this;
 
44
        }
 
45
 
 
46
 
 
47
        public static function install(Latte\Compiler $compiler)
 
48
        {
 
49
                return new static($compiler);
 
50
        }
 
51
 
 
52
 
 
53
        /**
 
54
         * Initializes before template parsing.
 
55
         * @return void
 
56
         */
 
57
        public function initialize()
 
58
        {
 
59
        }
 
60
 
 
61
 
 
62
        /**
 
63
         * Finishes template parsing.
 
64
         * @return array(prolog, epilog)
 
65
         */
 
66
        public function finalize()
 
67
        {
 
68
        }
 
69
 
 
70
 
 
71
        /**
 
72
         * New node is found.
 
73
         * @return bool
 
74
         */
 
75
        public function nodeOpened(MacroNode $node)
 
76
        {
 
77
                if ($this->macros[$node->name][2] && $node->prefix) {
 
78
                        $node->isEmpty = TRUE;
 
79
                        $this->compiler->setContext(Latte\Compiler::CONTEXT_DOUBLE_QUOTED_ATTR);
 
80
                        $res = $this->compile($node, $this->macros[$node->name][2]);
 
81
                        $this->compiler->setContext(NULL);
 
82
                        if (!$node->attrCode) {
 
83
                                $node->attrCode = "<?php $res ?>";
 
84
                        }
 
85
                } else {
 
86
                        $node->isEmpty = !isset($this->macros[$node->name][1]);
 
87
                        $res = $this->compile($node, $this->macros[$node->name][0]);
 
88
                        if (!$node->openingCode) {
 
89
                                $node->openingCode = "<?php $res ?>";
 
90
                        }
 
91
                }
 
92
                return $res !== FALSE;
 
93
        }
 
94
 
 
95
 
 
96
        /**
 
97
         * Node is closed.
 
98
         * @return void
 
99
         */
 
100
        public function nodeClosed(MacroNode $node)
 
101
        {
 
102
                $res = $this->compile($node, $this->macros[$node->name][1]);
 
103
                if (!$node->closingCode) {
 
104
                        $node->closingCode = "<?php $res ?>";
 
105
                }
 
106
        }
 
107
 
 
108
 
 
109
        /**
 
110
         * Generates code.
 
111
         * @return string
 
112
         */
 
113
        private function compile(MacroNode $node, $def)
 
114
        {
 
115
                $node->tokenizer->reset();
 
116
                $writer = Latte\PhpWriter::using($node, $this->compiler);
 
117
                if (is_string($def)) {
 
118
                        return $writer->write($def);
 
119
                } else {
 
120
                        return Nette\Utils\Callback::invoke($def, $node, $writer);
 
121
                }
 
122
        }
 
123
 
 
124
 
 
125
        /**
 
126
         * @return Latte\Compiler
 
127
         */
 
128
        public function getCompiler()
 
129
        {
 
130
                return $this->compiler;
 
131
        }
 
132
 
 
133
}