Overview

Namespaces

  • Minion
    • Service
    • Twig

Classes

  • Minion\Application
  • Minion\Console
  • Minion\Controller
  • Minion\Service\ServiceConfig
  • Minion\Service\ServiceProvider
  • Minion\Twig\AssetExtension
  • Minion\Twig\MiscExtension
  • Minion\Twig\TwigExtensionTagServiceProvider
  • Minion\Twig\UrlExtension
  • Minion\Utils

Interfaces

  • Minion\ControllerInterface
  • Minion\Service\ServiceConfigInterface
  • Minion\Service\ServiceProviderInterface
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Minion package.
  5:  * For the full copyright and license information, please view the LICENSE
  6:  * file that was distributed with this source code.
  7:  *
  8:  * @license MIT License
  9:  */
 10: 
 11: namespace Minion;
 12: 
 13: use Symfony\Component\Console\Command\Command;
 14: use Symfony\Component\Console\Input\InputOption;
 15: use Symfony\Component\HttpFoundation\Request;
 16: 
 17: /**
 18:  * Class Console.
 19:  *
 20:  * @package Minion
 21:  * @author Damian SzczerbiƄski <dszczer@gmail.com>
 22:  */
 23: class Console extends Application
 24: {
 25:     /** @var  string Command path */
 26:     public $commandPath;
 27: 
 28:     /**
 29:      * Console constructor.
 30:      *
 31:      * @param string $appNamespace Application namespace (autodetect, if empty)
 32:      * @param array  $values       Default options
 33:      * @param array  $fixPaths     Path fixes
 34:      *
 35:      * @return Console
 36:      */
 37:     public function __construct($appNamespace = '', array $values = [], array $fixPaths = []) {
 38:         parent::__construct($appNamespace, $values, $fixPaths);
 39: 
 40:         $this->commandPath = isset($fixPaths['commandDir'])
 41:             ? $fixPaths['commandDir']
 42:             : Utils::fixPath($this->getRootDir() . '/src/Command/');
 43: 
 44:         $this->loadCommands($this->getCommandPath());
 45: 
 46:         // load Propel commands
 47:         if($this['minion.usePropel']) {
 48:             $this->loadCommands(Utils::fixPath(($this['environment'] === 'test' ? $this->getRootDir() . '/../'
 49:                     : $this->getRootDir()) . '/vendor/propel/propel/src/Propel/Generator/Command/'), 'propel'
 50:             );
 51:             // replace default config-dir value command option
 52:             /** @var Command $command */
 53:             foreach($this['console']->all('propel') as $command) {
 54:                 $def = $command->getDefinition();
 55:                 $old = $def->getOptions();
 56:                 foreach($old as $i => $o)
 57:                     if($o->getName() === 'config-dir') {
 58:                         unset($old[$i]);
 59:                         break;
 60:                     }
 61:                 $opt = new InputOption('config-dir', null, InputOption::VALUE_REQUIRED,
 62:                     'The directory where the configuration file is placed.', $this->getPackageDir());
 63:                 $def->setOptions(\array_merge($old, [$opt]));
 64:             }
 65:         }
 66:     }
 67: 
 68:     /**
 69:      * Get project command directory path.
 70:      *
 71:      * @return string
 72:      */
 73:     public function getCommandPath() {
 74:         return $this->commandPath;
 75:     }
 76: 
 77:     /**
 78:      * Run console.
 79:      *
 80:      * @param Request $request Request object (not used in commands). Only to match inheritance requirements.
 81:      *
 82:      * @return mixed
 83:      */
 84:     public function run(Request $request = null) {
 85:         $this->boot();
 86: 
 87:         return $this['console']->run();
 88:     }
 89: 
 90:     /**
 91:      * Load all Command files from $path directory.
 92:      *
 93:      * Traverse all files inside specified directory, but loads only those files with suffix 'Command.php'.
 94:      *
 95:      * @param string      $path   Directory with Command files
 96:      * @param string|null $prefix Commands namespace
 97:      *
 98:      * @return void
 99:      */
100:     public function loadCommands($path, $prefix = null) {
101:         // load commands
102:         try {
103:             $iterator = new \RecursiveDirectoryIterator($path,
104:                 \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO
105:             );
106: 
107:             /** @var \RecursiveDirectoryIterator $child */
108:             foreach($iterator->getChildren() as $child)
109:                 if($child->isFile() && \preg_match('/Command.php$/', $child->getBasename())) {
110:                     $namespace = $class = "";
111:                     $gettingNamespace = $gettingClass = false;
112:                     foreach(\token_get_all(\file_get_contents($child->getRealPath())) as $token) {
113:                         if(\is_array($token) && ($token[0] === T_ABSTRACT || $token[0] === T_INTERFACE)) {
114:                             $namespace = $class = '';
115:                             break;
116:                         }
117:                         if(\is_array($token) && $token[0] === T_NAMESPACE)
118:                             $gettingNamespace = true;
119:                         if(\is_array($token) && $token[0] === T_CLASS)
120:                             $gettingClass = true;
121:                         if($gettingNamespace === true)
122:                             if(\is_array($token) && \in_array($token[0], [T_STRING, T_NS_SEPARATOR]))
123:                                 $namespace .= $token[1];
124:                             else if($token === ';')
125:                                 $gettingNamespace = false;
126:                         if($gettingClass === true)
127:                             if(\is_array($token) && $token[0] === T_STRING) {
128:                                 $class = $token[1];
129:                                 break;
130:                             }
131:                     }
132: 
133:                     $className = $namespace ? $namespace . '\\' . $class : $class;
134:                     if(\preg_match('/Command$/', $className) > 0) {
135:                         // make sure file with class is loaded
136:                         require_once $child->getRealPath();
137:                         /** @var Command $command */
138:                         $command = new $className;
139:                         if($prefix !== null)
140:                             $command->setName($prefix . ':' . $command->getName());
141:                         $this['console']->add($command);
142:                     }
143:                 }
144:         } catch(\UnexpectedValueException $ex) {
145:             // do nothing - no commands to load
146:         }
147:     }
148: }
API documentation generated by ApiGen