PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Dependency/Injector.php

https://github.com/Kazuzeya/OpenFlame-Framework
PHP | 296 lines | 138 code | 43 blank | 115 comment | 3 complexity | f31d358b2ef7fcfd49747d4f553ff99c MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package openflame-framework
  5. * @subpackage dependency
  6. * @copyright (c) 2010 - 2011 openflame-project.org
  7. * @license http://opensource.org/licenses/mit-license.php The MIT License
  8. * @link https://github.com/OpenFlame/OpenFlame-Framework
  9. *
  10. * Minimum Requirement: PHP 5.3.0
  11. */
  12. namespace OpenFlame\Framework\Dependency;
  13. use OpenFlame\Framework\Core;
  14. /**
  15. * OpenFlame Framework - Dependency injector
  16. * Provides fluid dependency injection.
  17. *
  18. *
  19. * @license http://opensource.org/licenses/mit-license.php The MIT License
  20. * @link https://github.com/OpenFlame/OpenFlame-Framework
  21. */
  22. class Injector implements \ArrayAccess
  23. {
  24. /**
  25. * @var array - Array of closures which prepare the requested instance on demand.
  26. */
  27. protected $injectors = array();
  28. /**
  29. * @var \OpenFlame\Framework\Dependency\Injector - Singleton instance of the dependency injector
  30. */
  31. protected static $instance;
  32. /**
  33. * Constructor
  34. */
  35. protected function __construct()
  36. {
  37. // Avoiding problems with use() here, need to pass $injector and not $this to the closure
  38. $injector = $this;
  39. // Define a bunch of injectors
  40. $this->setInjector('router', function() {
  41. return new \OpenFlame\Framework\Router\Router();
  42. });
  43. $this->setInjector('alias_router', function() {
  44. return new \OpenFlame\Framework\Router\AliasRouter();
  45. });
  46. $this->setInjector('input', function() {
  47. return new \OpenFlame\Framework\Input\Handler();
  48. });
  49. $this->setInjector('template', function() {
  50. return new \OpenFlame\Framework\Twig\Variables();
  51. });
  52. $this->setInjector('form', function() {
  53. return new \OpenFlame\Framework\Security\Form();
  54. });
  55. $this->setInjector('asset', function() {
  56. return new \OpenFlame\Framework\Asset\Manager();
  57. });
  58. $this->setInjector('asset_proxy', function() use($injector) {
  59. return new \OpenFlame\Framework\Asset\Proxy($injector->get('asset'));
  60. });
  61. $this->setInjector('dispatcher', function() {
  62. return new \OpenFlame\Framework\Event\Dispatcher();
  63. });
  64. $this->setInjector('language', function() {
  65. return new \OpenFlame\Framework\Language\Handler();
  66. });
  67. $this->setInjector('language_proxy', function() use($injector) {
  68. return new \OpenFlame\Framework\Language\Proxy($injector->get('language'));
  69. });
  70. $this->setInjector('cookie', function() {
  71. return new \OpenFlame\Framework\Cookie\Manager();
  72. });
  73. $this->setInjector('header', function() use($injector) {
  74. $header = new \OpenFlame\Framework\Header\Manager();
  75. $cookie = $header->getSubmodule('Cookie');
  76. $cookie->setCookieManager($injector->get('cookie'));
  77. return $header;
  78. });
  79. $this->setInjector('url', function() {
  80. return new \OpenFlame\Framework\URL\Builder();
  81. });
  82. $this->setInjector('url_proxy', function() use($injector) {
  83. return new \OpenFlame\Framework\URL\BuilderProxy($injector->get('url'));
  84. });
  85. $this->setInjector('hasher', function() {
  86. return new \OpenFlame\Framework\Security\Hasher();
  87. });
  88. $this->setInjector('seeder', function() {
  89. return new \OpenFlame\Framework\Security\Seeder();
  90. });
  91. $this->setInjector('timer', function() {
  92. return new \OpenFlame\Framework\Utility\Timer();
  93. });
  94. $this->setInjector('session_store_engine', function() {
  95. return new \OpenFlame\Framework\Session\Storage\EngineFilesystem();
  96. });
  97. $this->setInjector('session_client_engine', function() {
  98. return new \OpenFlame\Framework\Session\Client\EngineCookie();
  99. });
  100. $this->setInjector('session', function() use($injector) {
  101. $session = new \OpenFlame\Framework\Session\Driver();
  102. $session->setStorageEngine($injector->get('session_store_engine'));
  103. $session->setClientEngine($injector->get('session_client_engine'));
  104. return $session;
  105. });
  106. // These injectors should be manually defined, as we do not expect any path constants to be defined in the OpenFlame Framework
  107. /*
  108. $this->setInjector('twig', function() {
  109. $twig = new \OpenFlame\Framework\Twig\Wrapper();
  110. $twig->setTwigRootPath(Core::getConfig('twig.lib_path') ?: '/vendor/Twig/lib/Twig/')
  111. ->setTwigCachePath((Core::getConfig('twig.cache_path') ?: '/cache/twig/'))
  112. ->setTemplatePath((Core::getConfig('twig.template_path') ?: '/data/template/'))
  113. ->setTwigOption('debug', (Core::getConfig('twig.debug') ?: false));
  114. $twig->initTwig();
  115. return $twig;
  116. });
  117. $this->setInjector('cache_engine', function() {
  118. $cache_engine = new \OpenFlame\Framework\Cache\Engine\File\FileEngineJSON();
  119. $cache_engine->setCachePath('/cache/');
  120. return $cache_engine;
  121. });
  122. $this->setInjector('cache', function() use($injector) {
  123. $cache = new \OpenFlame\Framework\Cache\Driver();
  124. $cache->setEngine($injector->get('cache_engine'));
  125. return $cache;
  126. });
  127. */
  128. }
  129. /**
  130. * Get the singleton instance of the dependency injector.
  131. * @return \OpenFlame\Framework\Dependency\Injector - Singleton instance of the dependency injector
  132. */
  133. public static function getInstance()
  134. {
  135. if(self::$instance === NULL)
  136. {
  137. self::$instance = new static();
  138. }
  139. return self::$instance;
  140. }
  141. /**
  142. * Get a dependency (and fire the injector if the dependency has not been instantiated)
  143. * @param string $name - The name of the dependency to inject.
  144. * @return object - The object we are injecting.
  145. */
  146. public function get($name)
  147. {
  148. $object = \OpenFlame\Framework\Core::getObject($name);
  149. if($object === NULL)
  150. {
  151. $object = $this->fireInjector($name);
  152. }
  153. return $object;
  154. }
  155. /**
  156. * Register a new dependency injector closure.
  157. * @param string $name - The name of the dependency
  158. * @param \Closure $injector - The closure to use when injecting the dependency
  159. * @return \OpenFlame\Framework\Dependency\Injector - Provides a fluent interface.
  160. */
  161. public function setInjector($name, \Closure $injector)
  162. {
  163. $this->injectors[$name] = $injector;
  164. return $this;
  165. }
  166. /**
  167. * Removes the specified injector
  168. * @param string $name - The name of the dependency
  169. * @return \OpenFlame\Framework\Dependency\Injector - Provides a fluent interface.
  170. */
  171. public function unsetInjector($name)
  172. {
  173. $this->injectors[$name] = NULL;
  174. return $this;
  175. }
  176. /**
  177. * Check to see if an injector has been defined for a particular dependency.
  178. * @param string $name - The name of the dependency to check.
  179. * @return boolean - Is the injector present?
  180. */
  181. public function injectorPresent($name)
  182. {
  183. return !empty($this->injectors[$name]);
  184. }
  185. /**
  186. * Get the injector closure.
  187. * @param string $name - The name of the component to grab the injector for.
  188. * @return \Closure - Returns the dependency injector closure to use.
  189. *
  190. * @throws \LogicException
  191. */
  192. protected function getInjector($name)
  193. {
  194. if(!isset($this->injectors[$name]))
  195. {
  196. throw new \LogicException(sprintf('Cannot fetch dependency object "%s", no injector defined', $name));
  197. }
  198. return $this->injectors[$name];
  199. }
  200. /**
  201. * Trigger the dependency injector and store a reference to the resulting object in the OpenFlame core
  202. * @param string $name - The name of the dependency to inject.
  203. * @return object - The object that we are injecting.
  204. */
  205. protected function fireInjector($name)
  206. {
  207. $injector = $this->getInjector($name);
  208. return \OpenFlame\Framework\Core::setObject($name, $injector());
  209. }
  210. /**
  211. * ArrayAccess methods
  212. */
  213. /**
  214. * Check if an "array" offset exists in this object.
  215. * @param mixed $offset - The offset to check.
  216. * @return boolean - Does anything exist for this offset?
  217. */
  218. public function offsetExists($offset)
  219. {
  220. return $this->injectorPresent($offset);
  221. }
  222. /**
  223. * Get an "array" offset for this object.
  224. * @param mixed $offset - The offset to grab from.
  225. * @return mixed - The value of the offset, or null if the offset does not exist.
  226. */
  227. public function offsetGet($offset)
  228. {
  229. return $this->get($offset);
  230. }
  231. /**
  232. * Set an "array" offset to a certain value, if the offset exists
  233. * @param mixed $offset - The offset to set.
  234. * @param mixed $value - The value to set to the offset.
  235. * @return void
  236. */
  237. public function offsetSet($offset, $value)
  238. {
  239. $this->setInjector($offset, $value);
  240. }
  241. /**
  242. * Unset an "array" offset.
  243. * @param mixed $offset - The offset to clear out.
  244. * @return void
  245. */
  246. public function offsetUnset($offset)
  247. {
  248. $this->unsetInjector($offset);
  249. }
  250. }