CommandFactory.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://cakephp.org CakePHP(tm) Project
  11. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  12. */
  13. namespace Cake\Console;
  14. use InvalidArgumentException;
  15. /**
  16. * This is a factory for creating Command and Shell instances.
  17. *
  18. * This factory can be replaced or extended if you need to customize building
  19. * your command and shell objects.
  20. */
  21. class CommandFactory implements CommandFactoryInterface
  22. {
  23. /**
  24. * {@inheritDoc}
  25. */
  26. public function create($className, ConsoleIo $io)
  27. {
  28. if (is_subclass_of($className, Shell::class)) {
  29. return new $className($io);
  30. }
  31. // Command class
  32. $command = new $className();
  33. if (!$command instanceof Command) {
  34. $valid = implode('` or `', [Shell::class, Command::class]);
  35. $message = sprintf('Class `%s` must be an instance of `%s`.', $className, $valid);
  36. throw new InvalidArgumentException($message);
  37. }
  38. return $command;
  39. }
  40. }