sfAutoloadAgain.class.php 2.6 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
<?php

/*
 * This file is part of the symfony package.
 * (c) 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.
 */

/**
 * Autoload again for dev environments.
 * 
 * @package    symfony
 * @subpackage autoload
 * @author     Kris Wallsmith <kris.wallsmith@symfony-project.com>
 * @version    SVN: $Id: sfAutoloadAgain.class.php 22248 2009-09-22 17:15:16Z fabien $
 */
class sfAutoloadAgain
{
  static protected
    $instance = null;

  protected
    $registered = false,
    $reloaded   = false;

  /**
   * Returns the singleton autoloader.
   * 
   * @return sfAutoloadAgain
   */
  static public function getInstance()
  {
    if (null === self::$instance)
    {
      self::$instance = new self();
    }

    return self::$instance;
  }

  /**
   * Constructor.
   */
  protected function __construct()
  {
  }

  /**
   * Reloads the autoloader.
   * 
   * @param  string $class
   * 
   * @return boolean
   */
  public function autoload($class)
  {
    // only reload once
    if ($this->reloaded)
    {
      return false;
    }

    $autoloads = spl_autoload_functions();

    // as of PHP 5.2.11, spl_autoload_functions() returns the object as the first element of the array instead of the class name
    if (version_compare(PHP_VERSION, '5.2.11', '>='))
    {
      foreach ($autoloads as $position => $autoload)
      {
        if ($this === $autoload[0])
        {
          break;
        }
      }
    }
    else
    {
      $position  = array_search(array(__CLASS__, 'autoload'), $autoloads, true);
    }

    if (isset($autoloads[$position + 1]))
    {
      $this->unregister();
      $this->register();

      // since we're rearranged things, call the chain again
      spl_autoload_call($class);

      return class_exists($class, false) || interface_exists($class, false);
    }

    $autoload = sfAutoload::getInstance();
    $autoload->reloadClasses(true);

    $this->reloaded = true;

    return $autoload->autoload($class);
  }

  /**
   * Returns true if the autoloader is registered.
   * 
   * @return boolean
   */
  public function isRegistered()
  {
    return $this->registered;
  }

  /**
   * Registers the autoloader function.
   */
  public function register()
  {
    if (!$this->isRegistered())
    {
      spl_autoload_register(array($this, 'autoload'));
      $this->registered = true;
    }
  }

  /**
   * Unregisters the autoloader function.
   */
  public function unregister()
  {
    spl_autoload_unregister(array($this, 'autoload'));
    $this->registered = false;
  }
}