ModelAwareTrait.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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\Model;
  16. use Cake\Model\Exception\MissingModelException;
  17. use InvalidArgumentException;
  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. * This object's primary model class name. Should be a plural form.
  28. * CakePHP will not inflect the name.
  29. *
  30. * Example: For an object named 'Comments', the modelClass would be 'Comments'.
  31. * Plugin classes should use `Plugin.Comments` style names to correctly load
  32. * models from the correct plugin.
  33. *
  34. * @var string
  35. */
  36. public $modelClass;
  37. /**
  38. * A list of model factory functions.
  39. *
  40. * @var array
  41. */
  42. protected $_modelFactories = [];
  43. /**
  44. * Set the modelClass and modelKey properties based on conventions.
  45. *
  46. * If the properties are already set they will not be overwritten
  47. *
  48. * @param string $name Class name.
  49. * @return void
  50. */
  51. protected function _setModelClass($name) {
  52. if (empty($this->modelClass)) {
  53. $this->modelClass = $name;
  54. }
  55. }
  56. /**
  57. * Loads and constructs repository objects required by this object
  58. *
  59. * Typically used to load ORM Table objects as required. Can
  60. * also be used to load other types of repository objects your application uses.
  61. *
  62. * If a repository provider does not return an object a MissingModelException will
  63. * be thrown.
  64. *
  65. * @param string $modelClass Name of model class to load. Defaults to $this->modelClass
  66. * @param string $type The type of repository to load. Defaults to 'Table' which
  67. * delegates to Cake\ORM\TableRegistry.
  68. * @return bool True when single repository found and instance created.
  69. * @throws \Cake\Model\Exception\MissingModelException If the model class cannot be found.
  70. * @throws \InvalidArgumentException When using a type that has not been registered.
  71. */
  72. public function loadModel($modelClass = null, $type = 'Table') {
  73. if ($modelClass === null) {
  74. $modelClass = $this->modelClass;
  75. }
  76. if (isset($this->{$modelClass})) {
  77. return true;
  78. }
  79. list($plugin, $modelClass) = pluginSplit($modelClass, true);
  80. if (!isset($this->_modelFactories[$type])) {
  81. throw new InvalidArgumentException(sprintf(
  82. 'Unknown repository type "%s". Make sure you register a type before trying to use it.',
  83. $type
  84. ));
  85. }
  86. $factory = $this->_modelFactories[$type];
  87. $this->{$modelClass} = $factory($plugin . $modelClass);
  88. if (!$this->{$modelClass}) {
  89. throw new MissingModelException([$modelClass, $type]);
  90. }
  91. return true;
  92. }
  93. /**
  94. * Register a callable to generate repositories of a given type.
  95. *
  96. * @param string $type The name of the repository type the factory function is for.
  97. * @param callable $factory The factory function used to create instances.
  98. * @return void
  99. */
  100. public function modelFactory($type, callable $factory) {
  101. $this->_modelFactories[$type] = $factory;
  102. }
  103. }