ModelAwareTrait.php 5.4 KB

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