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) 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: sfAutoload.class.php 23205 2009-10-20 13:20:17Z Kris.Wallsmith $
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
  /**
77
   * Sets the path for a particular class.
78
   *
79
   * @param string $class A PHP class name
80
   * @param string $path  An absolute path
81
   */
82
  public function setClassPath($class, $path)
83
  {
84
    $class = strtolower($class);
85
 
86
    $this->overriden[$class] = $path;
87
 
88
    $this->classes[$class] = $path;
89
  }
90
 
91
  /**
92
   * Returns the path where a particular class can be found.
93
   *
94
   * @param string $class A PHP class name
95
   *
96
   * @return string|null An absolute path
97
   */
98
  public function getClassPath($class)
99
  {
100
    $class = strtolower($class);
101
 
102
    return isset($this->classes[$class]) ? $this->classes[$class] : null;
103
  }
104
 
105
  /**
106
   * Reloads the autoloader.
107
   *
108
   * @param  boolean $force Whether to force a reload
109
   *
110
   * @return boolean True if the reload was successful, otherwise false
111
   */
112
  public function reloadClasses($force = false)
113
  {
114
    // only (re)load the autoloading cache once per request
115
    if (self::$freshCache && !$force)
116
    {
117
      return false;
118
    }
119
 
120
    $configuration = sfProjectConfiguration::getActive();
121
    if (!$configuration || !$configuration instanceof sfApplicationConfiguration)
122
    {
123
      return false;
124
    }
125
 
126
    self::$freshCache = true;
127
    if (file_exists($configuration->getConfigCache()->getCacheName('config/autoload.yml')))
128
    {
129
      self::$freshCache = false;
130
      if ($force)
131
      {
132
        unlink($configuration->getConfigCache()->getCacheName('config/autoload.yml'));
133
      }
134
    }
135
 
136
    $file = $configuration->getConfigCache()->checkConfig('config/autoload.yml');
137
 
138
    $this->classes = include($file);
139
 
140
    foreach ($this->overriden as $class => $path)
141
    {
142
      $this->classes[$class] = $path;
143
    }
144
 
145
    return true;
146
  }
147
 
148
  /**
149
   * Handles autoloading of classes that have been specified in autoload.yml.
150
   *
151
   * @param  string  $class  A class name.
152
   *
153
   * @return boolean Returns true if the class has been loaded
154
   */
155
  public function autoload($class)
156
  {
157
    // load the list of autoload classes
158
    if (!$this->classes)
159
    {
160
      self::reloadClasses();
161
    }
162
 
163
    return self::loadClass($class);
164
  }
165
 
166
  /**
167
   * Tries to load a class that has been specified in autoload.yml.
168
   *
169
   * @param  string  $class  A class name.
170
   *
171
   * @return boolean Returns true if the class has been loaded
172
   */
173
  public function loadClass($class)
174
  {
175
    $class = strtolower($class);
176
 
177
    // class already exists
178
    if (class_exists($class, false) || interface_exists($class, false))
179
    {
180
      return true;
181
    }
182
 
183
    // we have a class path, let's include it
184
    if (isset($this->classes[$class]))
185
    {
186
      try
187
      {
188
        require $this->classes[$class];
189
      }
190
      catch (sfException $e)
191
      {
192
        $e->printStackTrace();
193
      }
194
      catch (Exception $e)
195
      {
196
        sfException::createFromException($e)->printStackTrace();
197
      }
198
 
199
      return true;
200
    }
201
 
202
    // see if the file exists in the current module lib directory
203
    if (
204
      sfContext::hasInstance()
205
      &&
206
      ($module = sfContext::getInstance()->getModuleName())
207
      &&
208
      isset($this->classes[$module.'/'.$class])
209
    )
210
    {
211
      try
212
      {
213
        require $this->classes[$module.'/'.$class];
214
      }
215
      catch (sfException $e)
216
      {
217
        $e->printStackTrace();
218
      }
219
      catch (Exception $e)
220
      {
221
        sfException::createFromException($e)->printStackTrace();
222
      }
223
 
224
      return true;
225
    }
226
 
227
    return false;
228
  }
229
}