1: <?php
  2: 
  3:   4:   5:   6:   7:   8:   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:  19:  20:  21:  22: 
 23: class Console extends Application
 24: {
 25:     
 26:     public $commandPath;
 27: 
 28:      29:  30:  31:  32:  33:  34:  35:  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:         
 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:             
 52:             
 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:  70:  71:  72: 
 73:     public function getCommandPath() {
 74:         return $this->commandPath;
 75:     }
 76: 
 77:      78:  79:  80:  81:  82:  83: 
 84:     public function run(Request $request = null) {
 85:         $this->boot();
 86: 
 87:         return $this['console']->run();
 88:     }
 89: 
 90:      91:  92:  93:  94:  95:  96:  97:  98:  99: 
100:     public function loadCommands($path, $prefix = null) {
101:         
102:         try {
103:             $iterator = new \RecursiveDirectoryIterator($path,
104:                 \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO
105:             );
106: 
107:             
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:                         
136:                         require_once $child->getRealPath();
137:                         
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:             
146:         }
147:     }
148: }