Development

/branches/1.1/lib/autoload/sfSimpleAutoload.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/autoload/sfSimpleAutoload.class.php

Revision 13317, 5.7 kB (checked in by fabien, 6 years ago)

[1.1, 1.2] fixed warning for symfony CLI (closes #5019)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 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  * sfSimpleAutoload class.
13  *
14  * This class is a singleton as PHP seems to be unable to register 2 autoloaders that are instances
15  * of the same class (why?).
16  *
17  * @package    symfony
18  * @subpackage autoload
19  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
20  * @version    SVN: $Id$
21  */
22 class sfSimpleAutoload
23 {
24   static protected
25     $instance = null;
26
27   protected
28     $cacheFile    = null,
29     $cacheLoaded  = false,
30     $cacheChanged = false,
31     $dirs         = array(),
32     $files        = array(),
33     $classes      = array();
34
35   protected function __construct($cacheFile = null)
36   {
37     if (!is_null($cacheFile))
38     {
39       $this->cacheFile = $cacheFile;
40     }
41
42     $this->loadCache();
43   }
44
45   /**
46    * Retrieves the singleton instance of this class.
47    *
48    * @param  string $cacheFile  The file path to save the cache
49    *
50    * @return sfSimpleAutoload   A sfSimpleAutoload implementation instance.
51    */
52   static public function getInstance($cacheFile = null)
53   {
54     if (!isset(self::$instance))
55     {
56       self::$instance = new sfSimpleAutoload($cacheFile);
57     }
58
59     return self::$instance;
60   }
61
62   /**
63    * Register sfSimpleAutoload in spl autoloader.
64    *
65    * @return void
66    */
67   static public function register()
68   {
69     ini_set('unserialize_callback_func', 'spl_autoload_call');
70     if (false === spl_autoload_register(array(self::getInstance(), 'autoload')))
71     {
72       throw new sfException(sprintf('Unable to register %s::autoload as an autoloading method.', get_class(self::getInstance())));
73     }
74
75     if (self::getInstance()->cacheFile)
76     {
77       register_shutdown_function(array(self::getInstance(), 'saveCache'));
78     }
79   }
80
81   /**
82    * Unregister sfSimpleAutoload from spl autoloader.
83    *
84    * @return void
85    */
86   static public function unregister()
87   {
88     spl_autoload_unregister(array(self::getInstance(), 'autoload'));
89   }
90
91   /**
92    * Handles autoloading of classes.
93    *
94    * @param  string  A class name.
95    *
96    * @return boolean Returns true if the class has been loaded
97    */
98   public function autoload($class)
99   {
100     // class already exists
101     if (class_exists($class, false) || interface_exists($class, false))
102     {
103       return true;
104     }
105
106     // we have a class path, let's include it
107     if (isset($this->classes[$class]))
108     {
109       require($this->classes[$class]);
110
111       return true;
112     }
113
114     return false;
115   }
116
117   /**
118    * Loads the cache.
119    */
120   public function loadCache()
121   {
122     if (!$this->cacheFile || !is_readable($this->cacheFile))
123     {
124       return;
125     }
126
127     list($this->classes, $this->dirs, $this->files) = unserialize(file_get_contents($this->cacheFile));
128
129     $this->cacheLoaded = true;
130     $this->cacheChanged = false;
131   }
132
133   /**
134    * Saves the cache.
135    */
136   public function saveCache()
137   {
138     if ($this->cacheChanged)
139     {
140       if (is_writable(dirname($this->cacheFile)))
141       {
142         file_put_contents($this->cacheFile, serialize(array($this->classes, $this->dirs, $this->files)));
143       }
144
145       $this->cacheChanged = false;
146     }
147   }
148
149   /**
150    * Reloads cache.
151    */
152   public function reload()
153   {
154     $this->classes = array();
155     $this->cacheLoaded = false;
156
157     foreach ($this->dirs as $dir)
158     {
159       $this->addDirectory($dir);
160     }
161
162     foreach ($this->files as $file)
163     {
164       $this->addFile($file);
165     }
166
167     $this->cacheLoaded = true;
168     $this->cacheChanged = true;
169   }
170
171   /**
172    * Removes the cache.
173    */
174   public function removeCache()
175   {
176     @unlink($this->cacheFile);
177   }
178
179   /**
180    * Adds a directory to the autoloading system if not yet present and give it the highest possible precedence.
181    *
182    * @param string The directory to look for classes
183    * @param string The extension to look for
184    */
185   public function addDirectory($dir, $ext = '.php')
186   {
187     $finder = sfFinder::type('file')->follow_link()->name('*'.$ext);
188
189     if($dirs = glob($dir))
190     {
191       foreach ($dirs as $dir)
192       {
193         if (false !== ($key = array_search($dir, $this->dirs)))
194         {
195           unset($this->dirs[$key]);
196           $this->dirs[] = $dir;
197
198           if ($this->cacheLoaded)
199           {
200             continue;
201           }
202         }
203         else
204         {
205           $this->dirs[] = $dir;
206         }
207
208         $this->cacheChanged = true;
209         $this->addFiles($finder->in($dir), false);
210       }
211     }
212   }
213
214   /**
215    * Adds files to the autoloading system.
216    *
217    * @param array   An array of files
218    * @param Boolean Whether to register those files as single entities (used when reloading)
219    */
220   public function addFiles(array $files, $register = true)
221   {
222     foreach ($files as $file)
223     {
224       $this->addFile($file, $register);
225     }
226   }
227
228   /**
229    * Adds a file to the autoloading system.
230    *
231    * @param string  A file path
232    * @param Boolean Whether to register those files as single entities (used when reloading)
233    */
234   public function addFile($file, $register = true)
235   {
236     if (!is_file($file))
237     {
238       return;
239     }
240
241     if (in_array($file, $this->files))
242     {
243       if ($this->cacheLoaded)
244       {
245         return;
246       }
247     }
248     else
249     {
250       if ($register)
251       {
252         $this->files[] = $file;
253       }
254     }
255
256     if ($register)
257     {
258       $this->cacheChanged = true;
259     }
260
261     preg_match_all('~^\s*(?:abstract\s+|final\s+)?(?:class|interface)\s+(\w+)~mi', file_get_contents($file), $classes);
262     foreach ($classes[1] as $class)
263     {
264       $this->classes[$class] = $file;
265     }
266   }
267
268   public function setClassPath($class, $path)
269   {
270     $this->overriden[$class] = $path;
271
272     $this->classes[$class] = $path;
273   }
274 }
275
Note: See TracBrowser for help on using the browser.