PageRenderTime 35ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/ManaPHP/Mvc/Model/Manager.php

https://gitlab.com/szlongshu/manaphp
PHP | 272 lines | 104 code | 35 blank | 133 comment | 7 complexity | ec1d888b1daa7b52f5b0cef62901d76a MD5 | raw file
  1. <?php
  2. namespace ManaPHP\Mvc\Model {
  3. use ManaPHP\Component;
  4. /**
  5. * ManaPHP\Mvc\Model\Manager
  6. *
  7. * This components controls the initialization of models, keeping record of relations
  8. * between the different models of the application.
  9. *
  10. * A ModelsManager is injected to a model via a Dependency Injector/Services Container such as ManaPHP\Di.
  11. *
  12. * <code>
  13. * $di = new ManaPHP\Di();
  14. *
  15. * $di->set('modelsManager', function() {
  16. * return new ManaPHP\Mvc\Model\Manager();
  17. * });
  18. *
  19. * $robot = new Robots($di);
  20. * </code>
  21. */
  22. class Manager extends Component implements ManagerInterface
  23. {
  24. /**
  25. * @var array
  26. */
  27. protected $_readConnectionServices = [];
  28. /**
  29. * @var array
  30. */
  31. protected $_writeConnectionServices = [];
  32. /**
  33. * @var \ManaPHP\Mvc\ModelInterface[]
  34. */
  35. protected $_initialized = [];
  36. /**
  37. * @var array
  38. */
  39. protected $_sources = [];
  40. /**
  41. * @var \ManaPHP\Mvc\Model\Query\BuilderInterface
  42. */
  43. protected $_builder;
  44. /**
  45. * Initializes a model in the model manager
  46. *
  47. * @param \ManaPHP\Mvc\ModelInterface $model
  48. *
  49. * @return boolean
  50. */
  51. public function initModel($model)
  52. {
  53. $modelName = get_class($model);
  54. /**
  55. * Models are just initialized once per request
  56. */
  57. if (!isset($this->_initialized[$modelName])) {
  58. $this->_initialized[$modelName] = $model;
  59. if (method_exists($model, 'initialize')) {
  60. $model->initialize();
  61. }
  62. }
  63. }
  64. /**
  65. * Loads a model throwing an exception if it does't exist
  66. *
  67. * @param string $modelName
  68. * @param boolean $newInstance
  69. *
  70. * @return \ManaPHP\Mvc\ModelInterface
  71. * @throws \ManaPHP\Mvc\Model\Exception
  72. */
  73. public function getModelInstance($modelName, $newInstance)
  74. {
  75. if (isset($this->_initialized[$modelName])) {
  76. if ($newInstance) {
  77. return new $modelName(null, $this->_dependencyInjector);
  78. }
  79. return $this->_initialized[$modelName];
  80. } else {
  81. if (class_exists($modelName)) {
  82. return new $modelName(null, $this->_dependencyInjector);
  83. }
  84. throw new Exception("Model '" . $modelName . "' could not be loaded");
  85. }
  86. }
  87. /**
  88. * Sets the mapped source for a model
  89. *
  90. * @param \ManaPHP\Mvc\ModelInterface|string $model
  91. * @param string $source
  92. *
  93. * @return static
  94. */
  95. public function setModelSource($model, $source)
  96. {
  97. $modelName = is_string($model) ? $model : get_class($model);
  98. $this->_sources[$modelName] = $source;
  99. return $this;
  100. }
  101. /**
  102. * Returns the mapped source for a model
  103. *
  104. * @param \ManaPHP\Mvc\ModelInterface|string $model
  105. *
  106. * @return string
  107. * @throws \ManaPHP\Mvc\Model\Exception
  108. */
  109. public function getModelSource($model)
  110. {
  111. $modelName = is_string($model) ? $model : get_class($model);
  112. if (!isset($this->_sources[$modelName])) {
  113. $this->_sources[$modelName] = ltrim(
  114. preg_replace_callback('#[A-Z]#',
  115. function ($match) {
  116. return '_' . strtolower($match[0]);
  117. },
  118. basename($modelName)), '_');
  119. }
  120. return $this->_sources[$modelName];
  121. }
  122. /**
  123. * Sets both write and read connection service for a model
  124. *
  125. * @param \ManaPHP\Mvc\ModelInterface|string $model
  126. * @param string $connectionService
  127. *
  128. * @return static
  129. */
  130. public function setConnectionService($model, $connectionService)
  131. {
  132. $modelName = is_string($model) ? $model : get_class($model);
  133. $this->_readConnectionServices[$modelName] = $connectionService;
  134. $this->_writeConnectionServices[$modelName] = $connectionService;
  135. return $this;
  136. }
  137. /**
  138. * Sets write connection service for a model
  139. *
  140. * @param \ManaPHP\Mvc\ModelInterface|string $model
  141. * @param string $connectionService
  142. *
  143. * @return static
  144. */
  145. public function setWriteConnectionService($model, $connectionService)
  146. {
  147. $modelName = is_string($model) ? $model : get_class($model);
  148. $this->_writeConnectionServices[$modelName] = $connectionService;
  149. return $this;
  150. }
  151. /**
  152. * Sets read connection service for a model
  153. *
  154. * @param \ManaPHP\Mvc\ModelInterface|string $model
  155. * @param string $connectionService
  156. *
  157. * @return static
  158. */
  159. public function setReadConnectionService($model, $connectionService)
  160. {
  161. $modelName = is_string($model) ? $model : get_class($model);
  162. $this->_readConnectionServices[$modelName] = $connectionService;
  163. return $this;
  164. }
  165. /**
  166. * Returns the connection to write data related to a model
  167. *
  168. * @param \ManaPHP\Mvc\ModelInterface|string $model
  169. *
  170. * @return \ManaPHP\DbInterface
  171. */
  172. public function getWriteConnection($model)
  173. {
  174. $serviceName = $this->getWriteConnectionService($model);
  175. return $this->_dependencyInjector->getShared($serviceName);
  176. }
  177. /**
  178. * Returns the connection to read data related to a model
  179. *
  180. * @param \ManaPHP\Mvc\ModelInterface|string $model
  181. *
  182. * @return \ManaPHP\DbInterface
  183. */
  184. public function getReadConnection($model)
  185. {
  186. $serviceName = $this->getReadConnectionService($model);
  187. return $this->_dependencyInjector->getShared($serviceName);
  188. }
  189. /**
  190. * Returns the connection service name used to read data related to a model
  191. *
  192. * @param \ManaPHP\Mvc\ModelInterface|string $model
  193. *
  194. * @return string
  195. */
  196. public function getReadConnectionService($model)
  197. {
  198. $modelName = is_string($model) ? $model : get_class($model);
  199. return isset($this->_readConnectionServices[$modelName]) ? $this->_readConnectionServices[$modelName] : 'db';
  200. }
  201. /**
  202. * Returns the connection service name used to write data related to a model
  203. *
  204. * @param \ManaPHP\Mvc\ModelInterface|string $model
  205. *
  206. * @return string
  207. */
  208. public function getWriteConnectionService($model)
  209. {
  210. $modelName = is_string($model) ? $model : get_class($model);
  211. return isset($this->_writeConnectionServices[$modelName]) ? $this->_writeConnectionServices[$modelName] : 'db';
  212. }
  213. /**
  214. * Creates a \ManaPHP\Mvc\Model\Query\Builder
  215. *
  216. * @param string $params
  217. *
  218. * @return \ManaPHP\Mvc\Model\Query\BuilderInterface
  219. * @throws \ManaPHP\Mvc\Model\Exception|\ManaPHP\Db\ConditionParser\Exception
  220. */
  221. public function createBuilder($params = null)
  222. {
  223. $this->_builder = $this->_dependencyInjector->get('ManaPHP\Mvc\Model\Query\Builder',
  224. [$params, $this->_dependencyInjector]);
  225. return $this->_builder;
  226. }
  227. /**
  228. * Returns the latest query created or executed in the models manager
  229. *
  230. * @return \ManaPHP\Mvc\Model\QueryInterface
  231. */
  232. public function getLastQuery()
  233. {
  234. return $this->_builder->getSql();
  235. }
  236. }
  237. }