Attribute.php
3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
 * PHPTAL templating engine
 *
 * PHP Version 5
 *
 * @category HTML
 * @package  PHPTAL
 * @author   Laurent Bedubourg <lbedubourg@motion-twin.com>
 * @author   Kornel Lesiński <kornel@aardvarkmedia.co.uk>
 * @license  http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
 * @version  SVN: $Id: Attribute.php 605 2009-05-03 02:50:26Z kornel $
 * @link     http://phptal.org/
 */
require_once 'PHPTAL/Php/TalesChainExecutor.php';
/**
 * Base class for all PHPTAL attributes.
 *
 * Attributes are first ordered by PHPTAL then called depending on their
 * priority before and after the element printing.
 *
 * An attribute must implements start() and end().
 *
 * @package PHPTAL
 * @subpackage Php
 * @author Laurent Bedubourg <lbedubourg@motion-twin.com>
 */
abstract class PHPTAL_Php_Attribute
{
    const ECHO_TEXT = 'text';
    const ECHO_STRUCTURE = 'structure';
    /** Attribute value specified by the element. */
    protected $expression;
    /** Element using this attribute (PHPTAL's counterpart of XML node) */
    protected $phpelement;
    /**
      * Called before element printing.
      * Default implementation is for backwards compatibility only. Please always override both before() and after().
      */
    public function before(PHPTAL_Php_CodeWriter $codewriter)
    {
        $this->tag = $this->phpelement; $this->phpelement->generator = $codewriter; $this->start(); // FIXME: remove
    }
    /**
      * Called after element printing.
      * Default implementation is for backwards compatibility only. Please always override both before() and after().
      */
    public function after(PHPTAL_Php_CodeWriter $codewriter)
    {
        $this->tag = $this->phpelement; $this->phpelement->generator = $codewriter; $this->end(); // FIXME: remove
    }
    /**
     * for backwards compatibility ONLY. Do not use!
     * @deprecated
     */
    public function start() { throw new PHPTAL_Exception('Do not use'); }
    /**
     * for backwards compatibility ONLY. Do not use!
     * @deprecated
     */
    public function end() { throw new PHPTAL_Exception('Do not use'); }
    /**
     * for backwards compatibility ONLY. Do not use!
     * @deprecated
     */
    final public function doEcho($code) { $this->doEchoAttribute($this->tag->generator, $code); }
    function __construct(PHPTAL_Dom_Element $phpelement, $expression)
    {
        $this->expression = $expression;
        $this->phpelement = $phpelement;
    }
    /**
     * Remove structure|text keyword from expression and stores it for later
     * doEcho() usage.
     *
     * $expression = 'stucture my/path';
     * $expression = $this->extractEchoType($expression);
     *
     * ...
     *
     * $this->doEcho($code);
     */
    protected function extractEchoType($expression)
    {
        $echoType = self::ECHO_TEXT;
        $expression = trim($expression);
        if (preg_match('/^(text|structure)\s+(.*?)$/ism', $expression, $m)) {
            list(, $echoType, $expression) = $m;
        }
        $this->_echoType = strtolower($echoType);
        return trim($expression);
    }
    protected function doEchoAttribute(PHPTAL_Php_CodeWriter $codewriter, $code)
    {
        if ($this->_echoType === self::ECHO_TEXT)
            $codewriter->doEcho($code);
        else
            $codewriter->doEchoRaw($code);
    }
    protected function parseSetExpression($exp)
    {
        $exp = trim($exp);
        // (dest) (value)
        if (preg_match('/^([a-z0-9:\-_]+)\s+(.*?)$/si', $exp, $m)) {
            return array($m[1], trim($m[2]));
        }
        // (dest)
        return array($exp, null);
    }
    protected $_echoType = PHPTAL_Php_Attribute::ECHO_TEXT;
}