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: sfI18nExtract.class.php 9128 2008-05-21 00:58:19Z Carl.Vondrick $
16
 */
17
abstract class sfI18nExtract
18
{
19
  protected
20
    $currentMessages = array(),
21
    $newMessages     = array(),
22
    $allSeenMessages = array(),
23
    $culture         = null,
24
    $parameters      = array(),
25
    $i18n            = null;
26
 
27
  /**
28
   * Class constructor.
29
   *
30
   * @see initialize()
31
   */
32
  public function __construct(sfI18N $i18n, $culture, $parameters = array())
33
  {
34
    $this->initialize($i18n, $culture, $parameters);
35
  }
36
 
37
  /**
38
   * Initializes the current extract object.
39
   *
40
   * @param sfI18N $i18n        A sfI18N instance
41
   * @param string $culture     The culture
42
   * @param array  $parameters  An array of parameters
43
   */
44
  function initialize(sfI18N $i18n, $culture, $parameters = array())
45
  {
46
    $this->allSeenMessages = array();
47
    $this->newMessages = array();
48
    $this->currentMessages = array();
49
 
50
    $this->culture = $culture;
51
    $this->parameters = $parameters;
52
 
53
    $this->i18n = $i18n;
54
 
55
    $this->configure();
56
 
57
    $this->loadMessageSources();
58
    $this->loadCurrentMessages();
59
  }
60
 
61
  /**
62
   * Configures the current extract object.
63
   */
64
  public function configure()
65
  {
66
  }
67
 
68
  /**
69
   * Extracts i18n strings.
70
   *
71
   * This class must be implemented by subclasses.
72
   */
73
  abstract public function extract();
74
 
75
  /**
76
   * Saves the new messages.
77
   *
78
   * Current limitations:
79
   *  - For file backends (XLIFF and gettext), it only saves in the "most global" file
80
   */
81
  public function saveNewMessages()
82
  {
83
    $messageSource = $this->i18n->getMessageSource();
84
    foreach ($this->getNewMessages() as $message)
85
    {
86
      $messageSource->append($message);
87
    }
88
 
89
    $messageSource->save();
90
  }
91
 
92
  /**
93
   * Deletes old messages.
94
   *
95
   * Current limitations:
96
   *  - For file backends (XLIFF and gettext), it only deletes in the "most global" file
97
   */
98
  public function deleteOldMessages()
99
  {
100
    $messageSource = $this->i18n->getMessageSource();
101
    foreach ($this->getOldMessages() as $message)
102
    {
103
      $messageSource->delete($message);
104
    }
105
  }
106
 
107
  /**
108
   * Gets the new i18n strings.
109
   *
110
   * @return array An array of i18n strings
111
   */
112
  final public function getNewMessages()
113
  {
114
    return array_diff($this->getAllSeenMessages(), $this->getCurrentMessages());
115
  }
116
 
117
  /**
118
   * Gets the current i18n strings.
119
   *
120
   * @return array An array of i18n strings
121
   */
122
  public function getCurrentMessages()
123
  {
124
    return $this->currentMessages;
125
  }
126
 
127
  /**
128
   * Gets all i18n strings seen during the extraction process.
129
   *
130
   * @return array An array of i18n strings
131
   */
132
  public function getAllSeenMessages()
133
  {
134
    return $this->allSeenMessages;
135
  }
136
 
137
  /**
138
   * Gets old i18n strings.
139
   *
140
   * This returns all strings that weren't seen during the extraction process
141
   * and are in the current messages.
142
   *
143
   * @return array An array of i18n strings
144
   */
145
  final public function getOldMessages()
146
  {
147
    return array_diff($this->getCurrentMessages(), $this->getAllSeenMessages());
148
  }
149
 
150
  /**
151
   * Loads message sources objects and sets the culture.
152
   */
153
  protected function loadMessageSources()
154
  {
155
    $this->i18n->getMessageSource()->setCulture($this->culture);
156
    $this->i18n->getMessageSource()->load();
157
  }
158
 
159
  /**
160
   * Loads messages already saved in the message sources.
161
   */
162
  protected function loadCurrentMessages()
163
  {
164
    $this->currentMessages = array();
165
    foreach ($this->i18n->getMessageSource()->read() as $catalogue => $translations)
166
    {
167
      foreach ($translations as $key => $values)
168
      {
169
        $this->currentMessages[] = $key;
170
      }
171
    }
172
  }
173
 
174
  /**
175
   * Extracts i18n strings from PHP files.
176
   *
177
   * @param string $dir The PHP full path name
178
   */
179
  protected function extractFromPhpFiles($dir)
180
  {
181
    $phpExtractor = new sfI18nPhpExtractor();
182
 
183
    $files = sfFinder::type('file')->name('*.php');
184
    $messages = array();
185
    foreach ($files->in($dir) as $file)
186
    {
187
      $messages = array_merge($messages, $phpExtractor->extract(file_get_contents($file)));
188
    }
189
 
190
    $this->updateMessages($messages);
191
  }
192
 
193
  /**
194
   * Updates the internal arrays with new messages.
195
   *
196
   * @param array $messages An array of new i18n strings
197
   */
198
  protected function updateMessages($messages)
199
  {
200
    $this->allSeenMessages = array_unique(array_merge($this->allSeenMessages, $messages));
201
  }
202
}