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
 * Autoload again for dev environments.
13
 *
14
 * @package    symfony
15
 * @subpackage autoload
16
 * @author     Kris Wallsmith <kris.wallsmith@symfony-project.com>
17
 * @version    SVN: $Id: sfAutoloadAgain.class.php 22248 2009-09-22 17:15:16Z fabien $
18
 */
19
class sfAutoloadAgain
20
{
21
  static protected
22
    $instance = null;
23
 
24
  protected
25
    $registered = false,
26
    $reloaded   = false;
27
 
28
  /**
29
   * Returns the singleton autoloader.
30
   *
31
   * @return sfAutoloadAgain
32
   */
33
  static public function getInstance()
34
  {
35
    if (null === self::$instance)
36
    {
37
      self::$instance = new self();
38
    }
39
 
40
    return self::$instance;
41
  }
42
 
43
  /**
44
   * Constructor.
45
   */
46
  protected function __construct()
47
  {
48
  }
49
 
50
  /**
51
   * Reloads the autoloader.
52
   *
53
   * @param  string $class
54
   *
55
   * @return boolean
56
   */
57
  public function autoload($class)
58
  {
59
    // only reload once
60
    if ($this->reloaded)
61
    {
62
      return false;
63
    }
64
 
65
    $autoloads = spl_autoload_functions();
66
 
67
    // as of PHP 5.2.11, spl_autoload_functions() returns the object as the first element of the array instead of the class name
68
    if (version_compare(PHP_VERSION, '5.2.11', '>='))
69
    {
70
      foreach ($autoloads as $position => $autoload)
71
      {
72
        if ($this === $autoload[0])
73
        {
74
          break;
75
        }
76
      }
77
    }
78
    else
79
    {
80
      $position  = array_search(array(__CLASS__, 'autoload'), $autoloads, true);
81
    }
82
 
83
    if (isset($autoloads[$position + 1]))
84
    {
85
      $this->unregister();
86
      $this->register();
87
 
88
      // since we're rearranged things, call the chain again
89
      spl_autoload_call($class);
90
 
91
      return class_exists($class, false) || interface_exists($class, false);
92
    }
93
 
94
    $autoload = sfAutoload::getInstance();
95
    $autoload->reloadClasses(true);
96
 
97
    $this->reloaded = true;
98
 
99
    return $autoload->autoload($class);
100
  }
101
 
102
  /**
103
   * Returns true if the autoloader is registered.
104
   *
105
   * @return boolean
106
   */
107
  public function isRegistered()
108
  {
109
    return $this->registered;
110
  }
111
 
112
  /**
113
   * Registers the autoloader function.
114
   */
115
  public function register()
116
  {
117
    if (!$this->isRegistered())
118
    {
119
      spl_autoload_register(array($this, 'autoload'));
120
      $this->registered = true;
121
    }
122
  }
123
 
124
  /**
125
   * Unregisters the autoloader function.
126
   */
127
  public function unregister()
128
  {
129
    spl_autoload_unregister(array($this, 'autoload'));
130
    $this->registered = false;
131
  }
132
}