sfSimpleAutoload.class.php 7.21 KB
Newer Older
Игорь's avatar
init    
Игорь committed
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
<?php

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * sfSimpleAutoload class.
 *
 * This class is a singleton as PHP seems to be unable to register 2 autoloaders that are instances
 * of the same class (why?).
 *
 * @package    symfony
 * @subpackage autoload
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
 * @version    SVN: $Id: sfSimpleAutoload.class.php 23205 2009-10-20 13:20:17Z Kris.Wallsmith $
 */
class sfSimpleAutoload
{
  static protected
    $registered = false,
    $instance   = null;

  protected
    $cacheFile    = null,
    $cacheLoaded  = false,
    $cacheChanged = false,
    $dirs         = array(),
    $files        = array(),
    $classes      = array(),
    $overriden    = array();

  protected function __construct($cacheFile = null)
  {
    if (null !== $cacheFile)
    {
      $this->cacheFile = $cacheFile;
    }

    $this->loadCache();
  }

  /**
   * Retrieves the singleton instance of this class.
   *
   * @param  string $cacheFile  The file path to save the cache
   *
   * @return sfSimpleAutoload   A sfSimpleAutoload implementation instance.
   */
  static public function getInstance($cacheFile = null)
  {
    if (!isset(self::$instance))
    {
      self::$instance = new sfSimpleAutoload($cacheFile);
    }

    return self::$instance;
  }

  /**
   * Register sfSimpleAutoload in spl autoloader.
   *
   * @return void
   */
  static public function register()
  {
    if (self::$registered)
    {
      return;
    }

    ini_set('unserialize_callback_func', 'spl_autoload_call');
    if (false === spl_autoload_register(array(self::getInstance(), 'autoload')))
    {
      throw new sfException(sprintf('Unable to register %s::autoload as an autoloading method.', get_class(self::getInstance())));
    }

    if (self::getInstance()->cacheFile)
    {
      register_shutdown_function(array(self::getInstance(), 'saveCache'));
    }

    self::$registered = true;
  }

  /**
   * Unregister sfSimpleAutoload from spl autoloader.
   *
   * @return void
   */
  static public function unregister()
  {
    spl_autoload_unregister(array(self::getInstance(), 'autoload'));
    self::$registered = false;
  }

  /**
   * Handles autoloading of classes.
   *
   * @param  string $class A class name.
   *
   * @return boolean Returns true if the class has been loaded
   */
  public function autoload($class)
  {
    $class = strtolower($class);

    // class already exists
    if (class_exists($class, false) || interface_exists($class, false))
    {
      return true;
    }

    // we have a class path, let's include it
    if (isset($this->classes[$class]))
    {
      try
      {
        require $this->classes[$class];
      }
      catch (sfException $e)
      {
        $e->printStackTrace();
      }
      catch (Exception $e)
      {
        sfException::createFromException($e)->printStackTrace();
      }

      return true;
    }

    return false;
  }

  /**
   * Loads the cache.
   */
  public function loadCache()
  {
    if (!$this->cacheFile || !is_readable($this->cacheFile))
    {
      return;
    }

    list($this->classes, $this->dirs, $this->files) = unserialize(file_get_contents($this->cacheFile));

    $this->cacheLoaded = true;
    $this->cacheChanged = false;
  }

  /**
   * Saves the cache.
   */
  public function saveCache()
  {
    if ($this->cacheChanged)
    {
      if (is_writable(dirname($this->cacheFile)))
      {
        file_put_contents($this->cacheFile, serialize(array($this->classes, $this->dirs, $this->files)));
      }

      $this->cacheChanged = false;
    }
  }

  /**
   * Reloads cache.
   */
  public function reload()
  {
    $this->classes = array();
    $this->cacheLoaded = false;

    foreach ($this->dirs as $dir)
    {
      $this->addDirectory($dir);
    }

    foreach ($this->files as $file)
    {
      $this->addFile($file);
    }

    foreach ($this->overriden as $class => $path)
    {
      $this->classes[$class] = $path;
    }

    $this->cacheLoaded = true;
    $this->cacheChanged = true;
  }

  /**
   * Removes the cache.
   */
  public function removeCache()
  {
    @unlink($this->cacheFile);
  }

  /**
   * Adds a directory to the autoloading system if not yet present and give it the highest possible precedence.
   *
   * @param string $dir The directory to look for classes
   * @param string $ext The extension to look for
   */
  public function addDirectory($dir, $ext = '.php')
  {
    $finder = sfFinder::type('file')->follow_link()->name('*'.$ext);

    if ($dirs = glob($dir))
    {
      foreach ($dirs as $dir)
      {
        if (false !== $key = array_search($dir, $this->dirs))
        {
          unset($this->dirs[$key]);
          $this->dirs[] = $dir;

          if ($this->cacheLoaded)
          {
            continue;
          }
        }
        else
        {
          $this->dirs[] = $dir;
        }

        $this->cacheChanged = true;
        $this->addFiles($finder->in($dir), false);
      }
    }
  }

  /**
   * Adds files to the autoloading system.
   *
   * @param array   $files    An array of files
   * @param Boolean $register Whether to register those files as single entities (used when reloading)
   */
  public function addFiles(array $files, $register = true)
  {
    foreach ($files as $file)
    {
      $this->addFile($file, $register);
    }
  }

  /**
   * Adds a file to the autoloading system.
   *
   * @param string  $file     A file path
   * @param Boolean $register Whether to register those files as single entities (used when reloading)
   */
  public function addFile($file, $register = true)
  {
    if (!is_file($file))
    {
      return;
    }

    if (in_array($file, $this->files))
    {
      if ($this->cacheLoaded)
      {
        return;
      }
    }
    else
    {
      if ($register)
      {
        $this->files[] = $file;
      }
    }

    if ($register)
    {
      $this->cacheChanged = true;
    }

    preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', file_get_contents($file), $classes);
    foreach ($classes[1] as $class)
    {
      $this->classes[strtolower($class)] = $file;
    }
  }

  /**
   * Sets the path for a particular class.
   *
   * @param string $class A PHP class name
   * @param string $path  An absolute path
   */
  public function setClassPath($class, $path)
  {
    $class = strtolower($class);

    $this->overriden[$class] = $path;

    $this->classes[$class] = $path;
  }

  /**
   * Returns the path where a particular class can be found.
   *
   * @param string $class A PHP class name
   *
   * @return string|null An absolute path
   */
  public function getClassPath($class)
  {
    $class = strtolower($class);

    return isset($this->classes[$class]) ? $this->classes[$class] : null;
  }

  /**
   * Loads configuration from the supplied files.
   *
   * @param array $files An array of autoload.yml files
   * 
   * @see sfAutoloadConfigHandler
   */
  public function loadConfiguration(array $files)
  {
    $config = new sfAutoloadConfigHandler();
    foreach ($config->evaluate($files) as $class => $file)
    {
      $this->setClassPath($class, $file);
    }
  }
}