ModelAwareTrait.php 4.5 KB

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