Development

/branches/1.2/lib/autoload/sfAutoload.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/autoload/sfAutoload.class.php

Revision 17858, 4.1 kB (checked in by FabianLange, 6 years ago)

[1.1, 1.2, 1.3] fixed some codeing standards (fixes #6376 - patch from gimler)

  • 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  * sfAutoload 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 sfAutoload
23 {
24   static protected
25     $freshCache = false,
26     $instance   = null;
27
28   protected
29     $overriden = array(),
30     $classes   = array();
31
32   protected function __construct()
33   {
34   }
35
36   /**
37    * Retrieves the singleton instance of this class.
38    *
39    * @return sfCoreAutoload A sfCoreAutoload implementation instance.
40    */
41   static public function getInstance()
42   {
43     if (!isset(self::$instance))
44     {
45       self::$instance = new sfAutoload();
46     }
47
48     return self::$instance;
49   }
50
51   /**
52    * Register sfAutoload in spl autoloader.
53    *
54    * @return void
55    */
56   static public function register()
57   {
58     ini_set('unserialize_callback_func', 'spl_autoload_call');
59
60     if (false === spl_autoload_register(array(self::getInstance(), 'autoload')))
61     {
62       throw new sfException(sprintf('Unable to register %s::autoload as an autoloading method.', get_class(self::getInstance())));
63     }
64   }
65
66   /**
67    * Unregister sfAutoload from spl autoloader.
68    *
69    * @return void
70    */
71   static public function unregister()
72   {
73     spl_autoload_unregister(array(self::getInstance(), 'autoload'));
74   }
75
76   public function setClassPath($class, $path)
77   {
78     $this->overriden[$class] = $path;
79
80     $this->classes[$class] = $path;
81   }
82
83   public function getClassPath($class)
84   {
85     return isset($this->classes[$class]) ? $this->classes[$class] : null;
86   }
87
88   public function reloadClasses($force = false)
89   {
90     // only (re)load the autoloading cache once per request
91     if (self::$freshCache)
92     {
93       return;
94     }
95
96     $configuration = sfProjectConfiguration::getActive();
97     if (!$configuration || !$configuration instanceof sfApplicationConfiguration)
98     {
99       return;
100     }
101
102     self::$freshCache = true;
103     if (file_exists($configuration->getConfigCache()->getCacheName('config/autoload.yml')))
104     {
105       self::$freshCache = false;
106       if ($force)
107       {
108         unlink($configuration->getConfigCache()->getCacheName('config/autoload.yml'));
109       }
110     }
111
112     $file = $configuration->getConfigCache()->checkConfig('config/autoload.yml');
113
114     $this->classes = include($file);
115
116     foreach ($this->overriden as $class => $path)
117     {
118       $this->classes[$class] = $path;
119     }
120   }
121
122   /**
123    * Handles autoloading of classes that have been specified in autoload.yml.
124    *
125    * @param string $class A class name.
126    *
127    * @return boolean Returns true if the class has been loaded
128    */
129   public function autoload($class)
130   {
131     // load the list of autoload classes
132     if (!$this->classes)
133     {
134       self::reloadClasses();
135     }
136
137     return self::loadClass($class);
138   }
139
140   public function autoloadAgain($class)
141   {
142     self::reloadClasses(true);
143
144     return self::loadClass($class);
145   }
146
147   /**
148    * Tries to load a class that has been specified in autoload.yml.
149    *
150    * @param string $class A class name.
151    *
152    * @return boolean Returns true if the class has been loaded
153    */
154   public function loadClass($class)
155   {
156     // class already exists
157     if (class_exists($class, false) || interface_exists($class, false))
158     {
159       return true;
160     }
161
162     // we have a class path, let's include it
163     if (isset($this->classes[$class]))
164     {
165       require($this->classes[$class]);
166
167       return true;
168     }
169
170     // see if the file exists in the current module lib directory
171     // must be in a module context
172     if (sfContext::hasInstance() && ($module = sfContext::getInstance()->getModuleName()) && isset($this->classes[$module.'/'.$class]))
173     {
174       require($this->classes[$module.'/'.$class]);
175
176       return true;
177     }
178
179     return false;
180   }
181 }
182
Note: See TracBrowser for help on using the browser.