ModelAwareTrait.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Datasource;
  16. use Cake\Datasource\Exception\MissingModelException;
  17. use UnexpectedValueException;
  18. /**
  19. * Provides functionality for loading table classes
  20. * and other repositories onto properties of the host object.
  21. *
  22. * Example users of this trait are Cake\Controller\Controller and
  23. * Cake\Console\Shell.
  24. */
  25. trait ModelAwareTrait
  26. {
  27. /**
  28. * This object's primary model class name. Should be a plural form.
  29. * CakePHP will not inflect the name.
  30. *
  31. * Example: For an object named 'Comments', the modelClass would be 'Comments'.
  32. * Plugin classes should use `Plugin.Comments` style names to correctly load
  33. * models from the correct plugin.
  34. *
  35. * @var string
  36. */
  37. public $modelClass;
  38. /**
  39. * A list of overridden model factory functions.
  40. *
  41. * @var array
  42. */
  43. protected $_modelFactories = [];
  44. /**
  45. * The model type to use.
  46. *
  47. * @var string
  48. */
  49. protected $_modelType = 'Table';
  50. /**
  51. * Set the modelClass and modelKey properties based on conventions.
  52. *
  53. * If the properties are already set they will not be overwritten
  54. *
  55. * @param string $name Class name.
  56. * @return void
  57. */
  58. protected function _setModelClass($name)
  59. {
  60. if (empty($this->modelClass)) {
  61. $this->modelClass = $name;
  62. }
  63. }
  64. /**
  65. * Loads and constructs repository objects required by this object
  66. *
  67. * Typically used to load ORM Table objects as required. Can
  68. * also be used to load other types of repository objects your application uses.
  69. *
  70. * If a repository provider does not return an object a MissingModelException will
  71. * be thrown.
  72. *
  73. * @param string|null $modelClass Name of model class to load. Defaults to $this->modelClass
  74. * @param string|null $modelType The type of repository to load. Defaults to the modelType() value.
  75. * @return \Cake\Datasource\RepositoryInterface The model instance created.
  76. * @throws \Cake\Datasource\Exception\MissingModelException If the model class cannot be found.
  77. * @throws \InvalidArgumentException When using a type that has not been registered.
  78. * @throws \UnexpectedValueException If no model type has been defined
  79. */
  80. public function loadModel($modelClass = null, $modelType = null)
  81. {
  82. if ($modelClass === null) {
  83. $modelClass = $this->modelClass;
  84. }
  85. if ($modelType === null) {
  86. $modelType = $this->getModelType();
  87. if ($modelType === null) {
  88. throw new UnexpectedValueException('No model type has been defined');
  89. }
  90. }
  91. list(, $alias) = pluginSplit($modelClass, true);
  92. if (isset($this->{$alias})) {
  93. return $this->{$alias};
  94. }
  95. if (isset($this->_modelFactories[$modelType])) {
  96. $factory = $this->_modelFactories[$modelType];
  97. }
  98. if (!isset($factory)) {
  99. $factory = FactoryLocator::get($modelType);
  100. }
  101. $this->{$alias} = $factory($modelClass);
  102. if (!$this->{$alias}) {
  103. throw new MissingModelException([$modelClass, $modelType]);
  104. }
  105. return $this->{$alias};
  106. }
  107. /**
  108. * Override a existing callable to generate repositories of a given type.
  109. *
  110. * @param string $type The name of the repository type the factory function is for.
  111. * @param callable $factory The factory function used to create instances.
  112. * @return void
  113. */
  114. public function modelFactory($type, callable $factory)
  115. {
  116. $this->_modelFactories[$type] = $factory;
  117. }
  118. /**
  119. * Get the model type to be used by this class
  120. *
  121. * @return string
  122. */
  123. public function getModelType()
  124. {
  125. return $this->_modelType;
  126. }
  127. /**
  128. * Set the model type to be used by this class
  129. *
  130. * @param string $modelType The model type
  131. *
  132. * @return $this
  133. */
  134. public function setModelType($modelType)
  135. {
  136. $this->_modelType = $modelType;
  137. return $this;
  138. }
  139. /**
  140. * Set or get the model type to be used by this class
  141. *
  142. * @deprecated 3.5.0 Use getModelType()/setModelType() instead.
  143. * @param string|null $modelType The model type or null to retrieve the current
  144. *
  145. * @return string|$this
  146. */
  147. public function modelType($modelType = null)
  148. {
  149. deprecationWarning(
  150. get_called_class() . '::modelType() is deprecated. ' .
  151. 'Use setModelType()/getModelType() instead.'
  152. );
  153. if ($modelType === null) {
  154. return $this->_modelType;
  155. }
  156. $this->_modelType = $modelType;
  157. return $this;
  158. }
  159. }