ModelAwareTrait.php 3.4 KB

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