| 1 |
lars |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* $Id$
|
|
|
4 |
*
|
|
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
16 |
*
|
|
|
17 |
* This software consists of voluntary contributions made by many individuals
|
|
|
18 |
* and is licensed under the LGPL. For more information, see
|
|
|
19 |
* <http://www.doctrine-project.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Doctrine_Locator
|
|
|
24 |
*
|
|
|
25 |
* @package Doctrine
|
|
|
26 |
* @subpackage Doctrine_Locator
|
|
|
27 |
* @category Locator
|
|
|
28 |
* @license http://www.gnu.org/licenses/lgpl.txt LGPL
|
|
|
29 |
* @link http://www.doctrine-project.org
|
|
|
30 |
* @author Janne Vanhala <jpvanhal@cc.hut.fi>
|
|
|
31 |
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
|
|
|
32 |
* @author Eevert Saukkokoski <dmnEe0@gmail.com>
|
|
|
33 |
* @version $Revision$
|
|
|
34 |
* @since 1.0
|
|
|
35 |
*/
|
|
|
36 |
class Doctrine_Locator implements Countable, IteratorAggregate
|
|
|
37 |
{
|
|
|
38 |
/**
|
|
|
39 |
* @var array $_resources an array of bound resources
|
|
|
40 |
*/
|
|
|
41 |
protected $_resources = array();
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* @var string $_classPrefix the default class prefix
|
|
|
45 |
*/
|
|
|
46 |
protected $_classPrefix = 'Doctrine_';
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* @var array $_instances a pool of this object's instances
|
|
|
50 |
*/
|
|
|
51 |
protected static $_instances = array();
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Constructor. Provide an array of resources to set initial contents.
|
|
|
55 |
*
|
|
|
56 |
* @param array
|
|
|
57 |
* @return void
|
|
|
58 |
*/
|
|
|
59 |
public function __construct(array $defaults = null)
|
|
|
60 |
{
|
|
|
61 |
if (null !== $defaults) {
|
|
|
62 |
foreach ($defaults as $name => $resource) {
|
|
|
63 |
if ($resource instanceof Doctrine_Locator_Injectable) {
|
|
|
64 |
$resource->setLocator($this);
|
|
|
65 |
}
|
|
|
66 |
$this->_resources[$name] = $resource;
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
self::$_instances[] = $this;
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* instance
|
|
|
74 |
*
|
|
|
75 |
* @return Sensei_Locator
|
|
|
76 |
*/
|
|
|
77 |
public static function instance()
|
|
|
78 |
{
|
|
|
79 |
if (empty(self::$_instances)) {
|
|
|
80 |
$obj = new Doctrine_Locator();
|
|
|
81 |
}
|
|
|
82 |
return current(self::$_instances);
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
/**
|
|
|
86 |
* setClassPrefix
|
|
|
87 |
*
|
|
|
88 |
* @param string $prefix
|
|
|
89 |
*/
|
|
|
90 |
public function setClassPrefix($prefix)
|
|
|
91 |
{
|
|
|
92 |
$this->_classPrefix = $prefix;
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
* getClassPrefix
|
|
|
97 |
*
|
|
|
98 |
* @return string
|
|
|
99 |
*/
|
|
|
100 |
public function getClassPrefix()
|
|
|
101 |
{
|
|
|
102 |
return $this->_classPrefix;
|
|
|
103 |
}
|
|
|
104 |
|
|
|
105 |
/**
|
|
|
106 |
* contains
|
|
|
107 |
* checks if a resource exists under the given name
|
|
|
108 |
*
|
|
|
109 |
* @return boolean whether or not given resource name exists
|
|
|
110 |
*/
|
|
|
111 |
public function contains($name)
|
|
|
112 |
{
|
|
|
113 |
return isset($this->_resources[$name]);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
/**
|
|
|
117 |
* bind
|
|
|
118 |
* binds a resource to a name
|
|
|
119 |
*
|
|
|
120 |
* @param string $name the name of the resource to bind
|
|
|
121 |
* @param mixed $value the value of the resource
|
|
|
122 |
* @return Sensei_Locator this object
|
|
|
123 |
*/
|
|
|
124 |
public function bind($name, $value)
|
|
|
125 |
{
|
|
|
126 |
$this->_resources[$name] = $value;
|
|
|
127 |
|
|
|
128 |
return $this;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
/**
|
|
|
132 |
* locate
|
|
|
133 |
* locates a resource by given name and returns it
|
|
|
134 |
*
|
|
|
135 |
* @throws Doctrine_Locator_Exception if the resource could not be found
|
|
|
136 |
* @param string $name the name of the resource
|
|
|
137 |
* @return mixed the located resource
|
|
|
138 |
*/
|
|
|
139 |
public function locate($name)
|
|
|
140 |
{
|
|
|
141 |
if (isset($this->_resources[$name])) {
|
|
|
142 |
return $this->_resources[$name];
|
|
|
143 |
} else {
|
|
|
144 |
$className = $name;
|
|
|
145 |
|
|
|
146 |
if ( ! class_exists($className)) {
|
|
|
147 |
|
|
|
148 |
$name = explode('.', $name);
|
|
|
149 |
$name = array_map('strtolower', $name);
|
|
|
150 |
$name = array_map('ucfirst', $name);
|
|
|
151 |
$name = implode('_', $name);
|
|
|
152 |
|
|
|
153 |
$className = $this->_classPrefix . $name;
|
|
|
154 |
|
|
|
155 |
if ( ! class_exists($className)) {
|
|
|
156 |
throw new Doctrine_Locator_Exception("Couldn't locate resource " . $className);
|
|
|
157 |
}
|
|
|
158 |
}
|
|
|
159 |
|
|
|
160 |
$this->_resources[$name] = new $className();
|
|
|
161 |
|
|
|
162 |
if ($this->_resources[$name] instanceof Doctrine_Locator_Injectable) {
|
|
|
163 |
$this->_resources[$name]->setLocator($this);
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
return $this->_resources[$name];
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
throw new Doctrine_Locator_Exception("Couldn't locate resource " . $name);
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
/**
|
|
|
173 |
* count
|
|
|
174 |
* returns the number of bound resources associated with
|
|
|
175 |
* this object
|
|
|
176 |
*
|
|
|
177 |
* @see Countable interface
|
|
|
178 |
* @return integer the number of resources
|
|
|
179 |
*/
|
|
|
180 |
public function count()
|
|
|
181 |
{
|
|
|
182 |
return count($this->_resources);
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
/**
|
|
|
186 |
* getIterator
|
|
|
187 |
* returns an ArrayIterator that iterates through all
|
|
|
188 |
* bound resources
|
|
|
189 |
*
|
|
|
190 |
* @return ArrayIterator an iterator for iterating through
|
|
|
191 |
* all bound resources
|
|
|
192 |
*/
|
|
|
193 |
public function getIterator()
|
|
|
194 |
{
|
|
|
195 |
return new ArrayIterator($this->_resources);
|
|
|
196 |
}
|
|
|
197 |
}
|