TaskRegistry.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Console;
  16. use Cake\Core\App;
  17. use Cake\Error;
  18. use Cake\Utility\ObjectRegistry;
  19. /**
  20. * Registry for Tasks. Provides features
  21. * for lazily loading tasks.
  22. */
  23. class TaskRegistry extends ObjectRegistry {
  24. /**
  25. * Shell to use to set params to tasks.
  26. *
  27. * @var Shell
  28. */
  29. protected $_Shell;
  30. /**
  31. * Constructor
  32. *
  33. * @param Shell $Shell
  34. */
  35. public function __construct(Shell $Shell) {
  36. $this->_Shell = $Shell;
  37. }
  38. /**
  39. * Resolve a task classname.
  40. *
  41. * Part of the template method for Cake\Utility\ObjectRegistry::load()
  42. *
  43. * @param string $class Partial classname to resolve.
  44. * @return string|false Either the correct classname or false.
  45. */
  46. protected function _resolveClassName($class) {
  47. return App::classname($class, 'Console/Command/Task', 'Task');
  48. }
  49. /**
  50. * Throws an exception when a task is missing.
  51. *
  52. * Part of the template method for Cake\Utility\ObjectRegistry::load()
  53. *
  54. * @param string $class The classname that is missing.
  55. * @param string $plugin The plugin the task is missing in.
  56. * @throws \Cake\Error\MissingTaskException
  57. */
  58. protected function _throwMissingClassError($class, $plugin) {
  59. throw new Error\MissingTaskException([
  60. 'class' => $class,
  61. 'plugin' => $plugin
  62. ]);
  63. }
  64. /**
  65. * Create the task instance.
  66. *
  67. * Part of the template method for Cake\Utility\ObjectRegistry::load()
  68. *
  69. * @param string $class The classname to create.
  70. * @param string $alias The alias of the task.
  71. * @return Component The constructed task class.
  72. */
  73. protected function _create($class, $alias) {
  74. return new $class(
  75. $this->_Shell->stdout,
  76. $this->_Shell->stderr,
  77. $this->_Shell->stdin
  78. );
  79. }
  80. }