HelperRegistry.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. * Shell to use to set params to tasks.
  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. $name = App::className($class, 'Command/Helper', 'Helper');
  59. if ($name === null) {
  60. return App::className($class, 'Shell/Helper', 'Helper');
  61. }
  62. return $name;
  63. }
  64. /**
  65. * Throws an exception when a helper is missing.
  66. *
  67. * Part of the template method for Cake\Core\ObjectRegistry::load()
  68. * and Cake\Core\ObjectRegistry::unload()
  69. *
  70. * @param string $class The classname that is missing.
  71. * @param string|null $plugin The plugin the helper is missing in.
  72. * @return void
  73. * @throws \Cake\Console\Exception\MissingHelperException
  74. */
  75. protected function _throwMissingClassError(string $class, ?string $plugin): void
  76. {
  77. throw new MissingHelperException([
  78. 'class' => $class,
  79. 'plugin' => $plugin,
  80. ]);
  81. }
  82. /**
  83. * Create the helper instance.
  84. *
  85. * Part of the template method for Cake\Core\ObjectRegistry::load()
  86. *
  87. * @param string $class The classname to create.
  88. * @param string $alias The alias of the helper.
  89. * @param array $settings An array of settings to use for the helper.
  90. * @return \Cake\Console\Helper The constructed helper class.
  91. * @psalm-suppress MoreSpecificImplementedParamType
  92. */
  93. protected function _create($class, string $alias, array $settings): Helper
  94. {
  95. // phpcs:ignore SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.InvalidFormat
  96. /** @var \Cake\Console\Helper */
  97. return new $class($this->_io, $settings);
  98. }
  99. }