PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/ManaPHP/Component.php

https://gitlab.com/szlongshu/manaphp
PHP | 192 lines | 79 code | 26 blank | 87 comment | 11 complexity | 6aecc2cd4f934259b927587ab9addb18 MD5 | raw file
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mark
  5. * Date: 2016/1/18
  6. */
  7. namespace ManaPHP {
  8. use ManaPHP\Event\Manager;
  9. /**
  10. * ManaPHP\Component
  11. *
  12. * @property \ManaPHP\Mvc\DispatcherInterface $dispatcher;
  13. * @property \ManaPHP\Mvc\RouterInterface $router
  14. //* @property \ManaPHP\Mvc\UrlInterface $url
  15. * @property \ManaPHP\Http\RequestInterface $request
  16. * @property \ManaPHP\Http\ResponseInterface $response
  17. * @property \ManaPHP\Http\Response\CookiesInterface $cookies
  18. //* @property \ManaPHP\FilterInterface $filter
  19. * @property \ManaPHP\Flash\Direct $flash
  20. //* @property \ManaPHP\Flash\Session $flashSession
  21. * @property \ManaPHP\Http\SessionInterface $session
  22. * @property \ManaPHP\Event\ManagerInterface $eventsManager
  23. * @property \ManaPHP\DbInterface $db
  24. //* @property \ManaPHP\Security $security
  25. * //* @property \ManaPHP\CryptInterface $crypt
  26. * // * @property \ManaPHP\EscaperInterface $escaper
  27. * @property \ManaPHP\Mvc\Model\ManagerInterface $modelsManager
  28. * @property \ManaPHP\Mvc\Model\MetadataInterface $modelsMetadata
  29. // * @property \ManaPHP\Assets\Manager $assets
  30. * @property \ManaPHP\Di|\ManaPHP\DiInterface $di
  31. * @property \ManaPHP\Http\Session\BagInterface $persistent
  32. * @property \ManaPHP\Mvc\ViewInterface $view
  33. * @property \ManaPHP\Mvc\View\Tag $tag
  34. * @property \ManaPHP\Loader $loader
  35. * @property \ManaPHP\Log\Logger $logger
  36. * @property \ManaPHP\Mvc\View\Renderer $renderer
  37. * @property \Application\Configure $configure
  38. * @property \ManaPHP\ApplicationInterface $application
  39. */
  40. class Component implements ComponentInterface
  41. {
  42. /**
  43. * @var \ManaPHP\Event\Manager
  44. */
  45. protected $_eventsManager;
  46. /**
  47. * @var array
  48. */
  49. protected static $_eventPeeks;
  50. /**
  51. * @var \ManaPHP\DiInterface
  52. */
  53. protected $_dependencyInjector;
  54. /**
  55. * Sets the dependency injector
  56. *
  57. * @param \ManaPHP\DiInterface $dependencyInjector
  58. *
  59. * @return static
  60. */
  61. public function setDependencyInjector($dependencyInjector)
  62. {
  63. $this->_dependencyInjector = $dependencyInjector;
  64. return $this;
  65. }
  66. /**
  67. * Returns the internal dependency injector
  68. *
  69. * @return \ManaPHP\DiInterface
  70. */
  71. public function getDependencyInjector()
  72. {
  73. if ($this->_dependencyInjector === null) {
  74. $this->_dependencyInjector = Di::getDefault();
  75. }
  76. return $this->_dependencyInjector;
  77. }
  78. /**
  79. * Magic method __get
  80. *
  81. * @param string $propertyName
  82. *
  83. * @return object
  84. */
  85. public function __get($propertyName)
  86. {
  87. if (!is_object($this->_dependencyInjector)) {
  88. $this->_dependencyInjector = Di::getDefault();
  89. }
  90. if ($this->_dependencyInjector->has($propertyName)) {
  91. return $this->{$propertyName} = $this->_dependencyInjector->getShared($propertyName);
  92. }
  93. if ($propertyName === 'di') {
  94. return $this->{'di'} = $this->_dependencyInjector;
  95. }
  96. if ($propertyName === 'persistent') {
  97. return $this->{'persistent'} = $this->_dependencyInjector->get('sessionBag', [get_class($this), $this->_dependencyInjector]);
  98. }
  99. trigger_error('Access to undefined property ' . $propertyName);
  100. return null;
  101. }
  102. /**
  103. * Attach a listener to the events manager
  104. *
  105. * @param string $event
  106. * @param callable|\ManaPHP\Event\ListenerInterface $handler
  107. *
  108. * @return static
  109. * @throws \ManaPHP\Event\Exception
  110. */
  111. public function attachEvent($event, $handler)
  112. {
  113. if ($this->_eventsManager === null) {
  114. $this->_eventsManager = new Manager();
  115. }
  116. $this->_eventsManager->attachEvent($event, $handler);
  117. return $this;
  118. }
  119. /**
  120. * Fires an event in the events manager causing that the active listeners will be notified about it
  121. *
  122. * @param string $event
  123. * @param mixed $data
  124. *
  125. * @return mixed
  126. */
  127. public function fireEvent($event, $data = null)
  128. {
  129. if (self::$_eventPeeks !== null) {
  130. foreach (self::$_eventPeeks as $peek) {
  131. $peek($event, $this, $data);
  132. }
  133. }
  134. if ($this->_eventsManager !== null) {
  135. /** @noinspection ExceptionsAnnotatingAndHandlingInspection */
  136. return $this->_eventsManager->fireEvent($event, $this, $data);
  137. }
  138. return null;
  139. }
  140. /**
  141. * @param \Closure $peek
  142. *
  143. * @throws Exception
  144. */
  145. public static function peekEvents($peek)
  146. {
  147. if (self::$_eventPeeks === null) {
  148. self::$_eventPeeks = [$peek];
  149. } else {
  150. self::$_eventPeeks[] = $peek;
  151. }
  152. }
  153. public function __debugInfo()
  154. {
  155. $defaultDi = Di::getDefault();
  156. $data = [];
  157. foreach (get_object_vars($this) as $k => $v) {
  158. if ($v === $defaultDi) {
  159. continue;
  160. }
  161. $data[$k] = $v;
  162. }
  163. return $data;
  164. }
  165. }
  166. }