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.phpdoctrine.net>.
|
20 |
*/
|
21 |
|
22 |
/**
|
23 |
* Doctrine_Locator_Injectable
|
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.phpdoctrine.net
|
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_Injectable
|
37 |
{
|
38 |
/**
|
39 |
* @var Doctrine_Locator the locator object
|
40 |
*/
|
41 |
protected $_locator;
|
42 |
|
43 |
/**
|
44 |
* @var array an array of bound resources
|
45 |
*/
|
46 |
protected $_resources = array();
|
47 |
|
48 |
/**
|
49 |
* @var Doctrine_Null $null Doctrine_Null object, used for extremely fast null value checking
|
50 |
*/
|
51 |
protected static $_null;
|
52 |
|
53 |
/**
|
54 |
* setLocator
|
55 |
* this method can be used for setting the locator object locally
|
56 |
*
|
57 |
* @param Doctrine_Locator the locator object
|
58 |
* @return Doctrine_Locator_Injectable this instance
|
59 |
*/
|
60 |
public function setLocator(Doctrine_Locator $locator)
|
61 |
{
|
62 |
$this->_locator = $locator;
|
63 |
return $this;
|
64 |
}
|
65 |
|
66 |
/**
|
67 |
* getLocator
|
68 |
* returns the locator associated with this object
|
69 |
*
|
70 |
* if there are no locator locally associated then
|
71 |
* this method tries to fetch the current global locator
|
72 |
*
|
73 |
* @return Doctrine_Locator
|
74 |
*/
|
75 |
public function getLocator()
|
76 |
{
|
77 |
if ( ! isset($this->_locator)) {
|
78 |
$this->_locator = Doctrine_Locator::instance();
|
79 |
|
80 |
}
|
81 |
return $this->_locator;
|
82 |
}
|
83 |
|
84 |
/**
|
85 |
* locate
|
86 |
* locates a resource by given name and returns it
|
87 |
*
|
88 |
* if the resource cannot be found locally this method tries
|
89 |
* to use the global locator for finding the resource
|
90 |
*
|
91 |
* @see Doctrine_Locator::locate()
|
92 |
* @throws Doctrine_Locator_Exception if the resource could not be found
|
93 |
* @param string $name the name of the resource
|
94 |
* @return mixed the located resource
|
95 |
*/
|
96 |
public function locate($name)
|
97 |
{
|
98 |
if (isset($this->_resources[$name])) {
|
99 |
if (is_object($this->_resources[$name])) {
|
100 |
return $this->_resources[$name];
|
101 |
} else {
|
102 |
// get the name of the concrete implementation
|
103 |
$concreteImpl = $this->_resources[$name];
|
104 |
|
105 |
return $this->getLocator()->locate($concreteImpl);
|
106 |
}
|
107 |
} else {
|
108 |
return $this->getLocator()->locate($name);
|
109 |
}
|
110 |
}
|
111 |
|
112 |
/**
|
113 |
* bind
|
114 |
* binds a resource to a name
|
115 |
*
|
116 |
* @param string $name the name of the resource to bind
|
117 |
* @param mixed $value the value of the resource
|
118 |
* @return Doctrine_Locator this object
|
119 |
*/
|
120 |
public function bind($name, $resource)
|
121 |
{
|
122 |
$this->_resources[$name] = $resource;
|
123 |
|
124 |
return $this;
|
125 |
}
|
126 |
|
127 |
/**
|
128 |
* initNullObject
|
129 |
* initializes the null object
|
130 |
*
|
131 |
* @param Doctrine_Null $null
|
132 |
* @return void
|
133 |
*/
|
134 |
public static function initNullObject(Doctrine_Null $null)
|
135 |
{
|
136 |
self::$_null = $null;
|
137 |
}
|
138 |
|
139 |
/**
|
140 |
* getNullObject
|
141 |
* returns the null object associated with this object
|
142 |
*
|
143 |
* @return Doctrine_Null
|
144 |
*/
|
145 |
public static function getNullObject()
|
146 |
{
|
147 |
return self::$_null;
|
148 |
}
|
149 |
}
|