srchub

srchub Git Source Tree


Root/pluf/src/Pluf/Translation/TemplateExtractor.php

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * Class to extract the translation strings from the template.
 *
 * Based on Pluf_Template_Compiler with code:
 *   Copyright (C) 2006 Laurent Jouanneau.
 */
class Pluf_Translation_TemplateExtractor
{
     
    /**
     * Store the literal blocks.
     **/
    protected $_literals;
     
    /**
     * Variables.
     */
    protected $_vartype = array(T_CHARACTER, T_CONSTANT_ENCAPSED_STRING,
                                T_DNUMBER, T_ENCAPSED_AND_WHITESPACE,
                                T_LNUMBER, T_OBJECT_OPERATOR, T_STRING,
                                T_WHITESPACE, T_ARRAY);
 
    /**
     * Assignation operators.
     */
    protected $_assignOp = array(T_AND_EQUAL, T_DIV_EQUAL, T_MINUS_EQUAL,
                                 T_MOD_EQUAL, T_MUL_EQUAL, T_OR_EQUAL,
                                 T_PLUS_EQUAL, T_PLUS_EQUAL, T_SL_EQUAL,
                                 T_SR_EQUAL, T_XOR_EQUAL);
 
    /**
     * Operators.
     */
    protected  $_op = array(T_BOOLEAN_AND, T_BOOLEAN_OR, T_EMPTY, T_INC,
                            T_ISSET, T_IS_EQUAL, T_IS_GREATER_OR_EQUAL,
                            T_IS_IDENTICAL, T_IS_NOT_EQUAL, T_IS_NOT_IDENTICAL,
                            T_IS_SMALLER_OR_EQUAL, T_LOGICAL_AND, T_LOGICAL_OR,
                            T_LOGICAL_XOR, T_SR, T_SL, T_DOUBLE_ARROW);
 
    /**
     * Authorized elements in variables.
     */
    protected $_allowedInVar;
 
    /**
     * Authorized elements in expression.
     */
    protected $_allowedInExpr;
 
    /**
     * Authorized elements in assignation.
     */
    protected $_allowedAssign;
 
    /**
     * Output filters.
     */
    protected $_modifier = array('upper' => 'strtoupper',
                                 'lower' => 'strtolower',
                                 'escxml' => 'htmlspecialchars',
                                 'escape' => 'Pluf_Template_htmlspecialchars',
                                 'strip_tags' => 'strip_tags',
                                 'escurl' => 'rawurlencode',
                                 'capitalize' => 'ucwords',
                                 // Not var_export because of recursive issues.
                                 'debug' => 'print_r',
                                 'fulldebug' => 'var_export',
                                 'count' => 'count',
                                 'nl2br' => 'nl2br',
                                 'trim' => 'trim',
                                 'unsafe' => 'Pluf_Template_unsafe',
                                 'safe' => 'Pluf_Template_unsafe',
                                 'date' => 'Pluf_Template_dateFormat',
                                 'time' => 'Pluf_Template_timeFormat',
                                 );
 
    /**
     * After the compilation is completed, this contains the list of
     * modifiers used in the template. The GetCompiledTemplate method
     * will add a series of Pluf::loadFunction at the top to preload
     * these modifiers.
     */
    public $_usedModifiers = array();
 
    /**
     * Default allowed extra tags/functions.
     *
     * These default tags are merged with the 'template_tags' defined
     * in the configuration of the application.
     */
    protected $_allowedTags = array(
                                    'url' => 'Pluf_Template_Tag_Url',
                                    );
    /**
     * During compilation, all the tags are created once so to query
     * their interface easily.
     */
    protected $_extraTags = array();
 
    /**
     * The block stack to see if the blocks are correctly closed.
     */
    protected $_blockStack = array();
 
    /**
     * Special stack for the translation handling in blocktrans.
     */
    protected $_transStack = array();
    protected $_transPlural = false;
 
    /**
     * Current template source file.
     */
    protected $_sourceFile;
 
    /**
     * Current tag.
     */
    protected $_currentTag;
 
    /**
     * Template folders.
     */
    public $templateFolders = array();
 
    /**
     * Template content. It can be set directly from a string.
     */
    public $templateContent = '';
 
    /**
     * Construct the compiler.
     *
     * @param string Basename of the template file.
     * @param array Base folders in which the templates files
     *              should be found. (array())
     * @param bool Load directly the template content. (true)
     */
    function __construct($template_file, $folders=array(), $load=true)
    {
        $allowedtags = Pluf::f('template_tags', array());
        $this->_allowedTags = array_merge($allowedtags, $this->_allowedTags);
        $modifiers = Pluf::f('template_modifiers', array());
        $this->_modifier = array_merge($modifiers, $this->_modifier);
 
        foreach ($this->_allowedTags as $name=>$model) {
            $this->_extraTags[$name] = new $model();
        }
        $this->_sourceFile = $template_file;
        $this->_allowedInVar = array_merge($this->_vartype, $this->_op);
        $this->_allowedInExpr = array_merge($this->_vartype, $this->_op);
        $this->_allowedAssign = array_merge($this->_vartype, $this->_assignOp,
                                            $this->_op);
        $this->templateFolders = $folders;
        if ($load) {
            $this->loadTemplateFile($template_file);
        }
    }
 
    /**
     * Get blocktrans.
     */
    function getBlockTrans()
    {
        $tplcontent = $this->templateContent;
        $tplcontent = preg_replace('!{\*(.*?)\*}!s', '', $tplcontent);
        $match = array();
        preg_match_all('!{blocktrans(.*?){/blocktrans}!s', $tplcontent, $match);
        $res = array();
        foreach ($match[1] as $m) {
            $res[] = '{blocktrans'.$m.'{/blocktrans}';
        }
        return $res;
    }
 
    /**
     * Get simple trans call.
     */
    function getSimpleTrans()
    {
        $tplcontent = $this->templateContent;
        $tplcontent = preg_replace('!{\*(.*?)\*}!s', '', $tplcontent);
        $match = array();
        preg_match_all('!{trans(.*?)}!s', $tplcontent, $match);
        $res = array();
        foreach ($match[1] as $m) {
            $res[] = '{trans'.$m.'}';
        }
        return $res;
    }
 
    /**
     * Compile the template into a file ready to parse with xgettext.
     *
     * @return string PHP code of the compiled template.
     */
    function compile()
    {
        $result = '';
        $blocktrans = $this->getBlockTrans();
        // Parse the blocktrans
        foreach ($blocktrans as $block) {
            $match = array();
            if (preg_match('!{blocktrans(.*?)}(.*?){plural}(.*?){/blocktrans}!s', $block, $match)) {
                $sing = $match[2];
                $plural = $match[3];
                $sing = preg_replace_callback('/{((.).*?)}/s',
                                               array($this, '_callbackInTransBlock'),
                                               $sing);
                $plural = preg_replace_callback('/{((.).*?)}/s',
                                               array($this, '_callbackInTransBlock'),
                                               $plural);
                $result .= '_n(\''.addcslashes($sing, "'").'\', \''.addcslashes($plural, "'").'\', $n);'."\n";
            } elseif (preg_match('!{blocktrans}(.*?){/blocktrans}!s', $block, $match)) {
                $sing = preg_replace_callback('/{((.).*?)}/s',
                                               array($this, '_callbackInTransBlock'),
                                               $match[1]);
                $result .= '__(\''.addcslashes($sing, "'").'\');'."\n";
            }
        }
 
        $simpletrans = $this->getSimpleTrans();
        foreach ($simpletrans as $content) {
            $result .= preg_replace_callback('/{((.).*?)}/s',
                                             array($this, '_callback'),
                                             $content);
        }
        return '<?php # This is not a valid php code. It is just for gettext use'."\n\n".$result.' ?>';
    }
 
 
    /**
     * Load a template file.
     *
     * The path to the file to load is relative and the file is found
     * in one of the $templateFolders array of folders.
     *
     * @param string Relative path of the file to load.
     */
    function loadTemplateFile($file)
    {
        // FIXME: Very small security check, could be better.
        if (strpos($file, '..') !== false) {
            throw new Exception(sprintf(__('Template file contains invalid characters: %s'), $file));
        }
        foreach ($this->templateFolders as $folder) {
            if (file_exists($folder.'/'.$file)) {
                $this->templateContent = file_get_contents($folder.'/'.$file);
                return;
            }
        }
        // File not found in all the folders.
        throw new Exception(sprintf(__('Template file not found: %s'), $file));
    }
 
    function _callback($matches)
    {
        list(,$tag, $firstcar) = $matches;
        if ($firstcar != 't') {
            trigger_error(sprintf(__('Invalid tag in translation extractor: %s'), $tag), E_USER_ERROR);
            return '';
        }
        $this->_currentTag = $tag;
        if (!preg_match('/^(\/?[a-zA-Z0-9_]+)(?:(?:\s+(.*))|(?:\((.*)\)))?$/', $tag, $m)) {
            trigger_error(sprintf(__('Invalid function syntax: %s'), $tag), E_USER_ERROR);
            return '';
        }
        if (count($m) == 4){
            $m[2] = $m[3];
        }
        if (!isset($m[2])) $m[2] = '';
        if ($m[1] == 'trans') {
            return $this->_parseFunction($m[1], $m[2]);
        }
        return '';
    }
 
    function _callbackInTransBlock($matches)
    {
        list(,$tag, $firstcar) = $matches;
        if (!preg_match('/^\$|[\'"]|[a-zA-Z\/]$/', $firstcar)) {
            trigger_error(sprintf(__('Invalid tag syntax: %s'), $tag), E_USER_ERROR);
            return '';
        }
        $this->_currentTag = $tag;
        if ($firstcar == '$') {
            $tok = explode('|', $tag);
            $this->_transStack[substr($tok[0], 1)] = $this->_parseVariable($tag);
            return '%%'.substr($tok[0], 1).'%%';
        }
        return '';
    }
 
    function _parseVariable($expr)
    {
        $tok = explode('|', $expr);
        $res = $this->_parseFinal(array_shift($tok), $this->_allowedInVar);
        // We do not take into account the modifiers.
        return $res;
    }
 
    function _parseFunction($name, $args)
    {
        switch ($name) {
        case 'trans':
            $argfct = $this->_parseFinal($args, $this->_allowedAssign);
            $res = '__('.$argfct.');'."\n";
            break;
        default:
            $res = '';
            break;
        }
        return $res;
    }
 
    /*
 
    -------
    if:        op, autre, var
    foreach:   T_AS, T_DOUBLE_ARROW, T_VARIABLE, @locale@
    for:       autre, fin_instruction
    while:     op, autre, var
    assign:    T_VARIABLE puis assign puis autre, ponctuation, T_STRING
    echo:      T_VARIABLE/@locale@ puis autre + ponctuation
    modificateur: serie de autre séparé par une virgule
 
    tous : T_VARIABLE, @locale@
 
    */
 
    function _parseFinal($string, $allowed=array(),
                         $exceptchar=array(';'))
    {
        $tokens = token_get_all('<?php '.$string.'?>');
        $result = '';
        $first = true;
        $inDot = false;
        $firstok = array_shift($tokens);
        $afterAs = false;
        $f_key = '';
        $f_val = '';
        $results = array();
 
        // il y a un bug, parfois le premier token n'est pas T_OPEN_TAG...
        if ($firstok == '<' && $tokens[0] == '?' && is_array($tokens[1])
            && $tokens[1][0] == T_STRING && $tokens[1][1] == 'php') {
            array_shift($tokens);
            array_shift($tokens);
        }
        foreach ($tokens as $tok) {
            if (is_array($tok)) {
                list($type, $str) = $tok;
                $first = false;
                if($type == T_CLOSE_TAG){
                    continue;
                }
                if ($type == T_AS) {
                    $afterAs = true;
                }
                if ($inDot) {
                    $result .= $str;
                } elseif ($type == T_VARIABLE) {
                    $result .= '$t->_vars[\''.substr($str, 1).'\']';
                } elseif ($type == T_WHITESPACE || in_array($type, $allowed)) {
                    $result .= $str;
                } else {
                    trigger_error(sprintf(__('Invalid syntax: (%s) %s.'), $this->_currentTag, $str), E_USER_ERROR);
                    return '';
                }
            } else {
                if (in_array($tok, $exceptchar)) {
                    trigger_error(sprintf(__('Invalid character: (%s) %s.'), $this->_currentTag, $tok), E_USER_ERROR);
                } elseif ($tok == '.') {
                    $inDot = true;
                    $result .= '->';
                } elseif ($tok == '~') {
                    $result .= '.';
                } elseif ($tok =='[') {
                    $result.=$tok;
                } elseif ($tok ==']') {
                    $result.=$tok;
                } elseif ($tok == ',') { // $getAsArray not defined anywhere...
                    $results[]=$result;
                    $result='';
                } else {
                    $result .= $tok;
                }
                $first = false;
            }
        }
        return $result;
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.10908s using 11 queries.