HelperRegistry.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.1.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Console;
  17. use Cake\Console\Exception\MissingHelperException;
  18. use Cake\Core\App;
  19. use Cake\Core\ObjectRegistry;
  20. /**
  21. * Registry for Helpers. Provides features
  22. * for lazily loading helpers.
  23. *
  24. * @extends \Cake\Core\ObjectRegistry<\Cake\Console\Helper>
  25. */
  26. class HelperRegistry extends ObjectRegistry
  27. {
  28. /**
  29. * IO instance.
  30. *
  31. * @var \Cake\Console\ConsoleIo
  32. */
  33. protected $_io;
  34. /**
  35. * Sets The IO instance that should be passed to the shell helpers
  36. *
  37. * @param \Cake\Console\ConsoleIo $io An io instance.
  38. * @return void
  39. */
  40. public function setIo(ConsoleIo $io): void
  41. {
  42. $this->_io = $io;
  43. }
  44. /**
  45. * Resolve a helper classname.
  46. *
  47. * Will prefer helpers defined in Command\Helper over those
  48. * defined in Shell\Helper.
  49. *
  50. * Part of the template method for Cake\Core\ObjectRegistry::load()
  51. *
  52. * @param string $class Partial classname to resolve.
  53. * @return string|null Either the correct class name or null.
  54. * @psalm-return class-string
  55. */
  56. protected function _resolveClassName(string $class): ?string
  57. {
  58. return App::className($class, 'Command/Helper', 'Helper');
  59. }
  60. /**
  61. * Throws an exception when a helper is missing.
  62. *
  63. * Part of the template method for Cake\Core\ObjectRegistry::load()
  64. * and Cake\Core\ObjectRegistry::unload()
  65. *
  66. * @param string $class The classname that is missing.
  67. * @param string|null $plugin The plugin the helper is missing in.
  68. * @return void
  69. * @throws \Cake\Console\Exception\MissingHelperException
  70. */
  71. protected function _throwMissingClassError(string $class, ?string $plugin): void
  72. {
  73. throw new MissingHelperException([
  74. 'class' => $class,
  75. 'plugin' => $plugin,
  76. ]);
  77. }
  78. /**
  79. * Create the helper instance.
  80. *
  81. * Part of the template method for Cake\Core\ObjectRegistry::load()
  82. *
  83. * @param \Cake\Console\Helper|string $class The classname to create.
  84. * @param string $alias The alias of the helper.
  85. * @param array $config An array of settings to use for the helper.
  86. * @return \Cake\Console\Helper The constructed helper class.
  87. * @psalm-suppress MoreSpecificImplementedParamType
  88. */
  89. protected function _create(object|string $class, string $alias, array $config): Helper
  90. {
  91. if (is_object($class)) {
  92. return $class;
  93. }
  94. /** @var \Cake\Console\Helper */
  95. return new $class($this->_io, $config);
  96. }
  97. }