Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
 
3
/*
4
 * This file is part of the symfony package.
5
 * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
 
11
/**
12
 * @package    symfony
13
 * @subpackage i18n
14
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
15
 * @version    SVN: $Id: sfI18nPhpExtractor.class.php 28843 2010-03-29 08:25:39Z fabien $
16
 */
17
class sfI18nPhpExtractor implements sfI18nExtractorInterface
18
{
19
  /**
20
   * Extract i18n strings for the given content.
21
   *
22
   * @param  string $content The content
23
   *
24
   * @return array An array of i18n strings
25
   */
26
  public function extract($content)
27
  {
28
    $tokens = token_get_all($content);
29
 
30
    $strings = array();
31
    $i18n_function = 0;
32
    $line = 0;
33
    $heredoc = false;
34
    $buffer = '';
35
    foreach ($tokens as $token)
36
    {
37
      if (is_string($token))
38
      {
39
        switch ($token)
40
        {
41
          case '(':
42
            if (1 == $i18n_function)
43
            {
44
              $i18n_function = 2;
45
            }
46
 
47
            break;
48
          default:
49
            $i18n_function = 0;
50
        }
51
      }
52
      else
53
      {
54
        list($id, $text) = $token;
55
 
56
        switch ($id)
57
        {
58
          case T_STRING:
59
            if ($heredoc && 2 == $i18n_function)
60
            {
61
              $buffer .= $text;
62
            }
63
            else
64
            {
65
              $i18n_function = ('__' == $text || 'format_number_choice' == $text) ? 1 : 0;
66
            }
67
            break;
68
          case T_WHITESPACE:
69
            break;
70
          case T_START_HEREDOC:
71
            $heredoc = true;
72
            break;
73
          case T_END_HEREDOC:
74
            $heredoc = false;
75
            if ($buffer)
76
            {
77
              $strings[] = $buffer;
78
              $buffer = '';
79
            }
80
            $i18n_function = 0;
81
            break;
82
          case T_CONSTANT_ENCAPSED_STRING:
83
            if (2 == $i18n_function)
84
            {
85
              $delimiter = $text[0];
86
              $strings[] = str_replace('\\'.$delimiter, $delimiter, substr($text, 1, -1));
87
            }
88
            $i18n_function = 0;
89
            break;
90
          default:
91
            if ($heredoc && 2 == $i18n_function)
92
            {
93
              $buffer .= $text;
94
            }
95
            else
96
            {
97
              $i18n_function = 0;
98
            }
99
        }
100
      }
101
    }
102
 
103
    return $strings;
104
  }
105
}