1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Magallanes package. |
4
|
|
|
* |
5
|
|
|
* (c) Andrés Montañez <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Mage; |
12
|
|
|
|
13
|
|
|
use Mage\Command\AbstractCommand; |
14
|
|
|
use Mage\Runtime\Runtime; |
15
|
|
|
use Symfony\Component\Finder\Finder; |
16
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
17
|
|
|
use Monolog\Logger; |
18
|
|
|
use Monolog\Handler\StreamHandler; |
19
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
20
|
|
|
use Symfony\Component\Console\Event\ConsoleErrorEvent; |
21
|
|
|
use Symfony\Component\Console\ConsoleEvents; |
22
|
|
|
use Symfony\Component\Console\Application; |
23
|
|
|
use Symfony\Component\Yaml\Parser; |
24
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
25
|
|
|
use ReflectionClass; |
26
|
|
|
use Mage\Runtime\Exception\RuntimeException; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The Console Application for launching the Mage command in a standalone instance |
30
|
|
|
* |
31
|
|
|
* @author Andrés Montañez <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class MageApplication extends Application |
34
|
|
|
{ |
35
|
|
|
protected $runtime; |
36
|
|
|
protected $file; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $file The YAML file from which to read the configuration |
40
|
|
|
*/ |
41
|
|
|
public function __construct($file) |
42
|
|
|
{ |
43
|
|
|
parent::__construct('Magallanes', Mage::VERSION); |
44
|
|
|
|
45
|
|
|
$this->file = $file; |
46
|
|
|
$dispatcher = new EventDispatcher(); |
47
|
|
|
$this->setDispatcher($dispatcher); |
48
|
|
|
|
49
|
|
|
$dispatcher->addListener(ConsoleEvents::ERROR, function (ConsoleErrorEvent $event) { |
50
|
|
|
$output = $event->getOutput(); |
51
|
|
|
$command = $event->getCommand(); |
52
|
|
|
$output->writeln(sprintf('Oops, exception thrown while running command <info>%s</info>', $command->getName())); |
53
|
|
|
$exitCode = $event->getExitCode(); |
54
|
|
|
$event->setError(new \LogicException('Caught exception', $exitCode, $event->getError())); |
55
|
|
|
}); |
56
|
|
|
|
57
|
|
|
$this->runtime = $this->instantiateRuntime(); |
58
|
|
|
$this->loadBuiltInCommands(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Configure the Magallanes Application |
63
|
|
|
* |
64
|
|
|
* @throws RuntimeException |
65
|
|
|
*/ |
66
|
|
|
public function configure() |
67
|
|
|
{ |
68
|
|
|
if (!file_exists($this->file) || !is_readable($this->file)) { |
69
|
|
|
throw new RuntimeException(sprintf('The file "%s" does not exists or is not readable.', $this->file)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
try { |
73
|
|
|
$parser = new Parser(); |
74
|
|
|
$config = $parser->parse(file_get_contents($this->file)); |
75
|
|
|
} catch (ParseException $exception) { |
76
|
|
|
throw new RuntimeException(sprintf('Error parsing the file "%s".', $this->file)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (array_key_exists('magephp', $config) && is_array($config['magephp'])) { |
80
|
|
|
$logger = null; |
81
|
|
|
if (array_key_exists('log_dir', $config['magephp']) && file_exists($config['magephp']['log_dir']) && is_dir($config['magephp']['log_dir'])) { |
82
|
|
|
$logfile = sprintf('%s/%s.log', $config['magephp']['log_dir'], date('Ymd_His')); |
83
|
|
|
$config['magephp']['log_file'] = $logfile; |
84
|
|
|
|
85
|
|
|
$logger = new Logger('magephp'); |
86
|
|
|
$logger->pushHandler(new StreamHandler($logfile)); |
87
|
|
|
|
88
|
|
|
} elseif (array_key_exists('log_dir', $config['magephp']) && !is_dir($config['magephp']['log_dir'])) { |
89
|
|
|
throw new RuntimeException(sprintf('The configured log_dir "%s" does not exists or is not a directory.', $config['magephp']['log_dir'])); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->runtime->setConfiguration($config['magephp']); |
93
|
|
|
$this->runtime->setLogger($logger); |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
throw new RuntimeException(sprintf('The file "%s" does not have a valid Magallanes configuration.', $this->file)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Loads the BuiltIn Commands |
102
|
|
|
*/ |
103
|
|
View Code Duplication |
protected function loadBuiltInCommands() |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
$finder = new Finder(); |
106
|
|
|
$finder->files()->in(__DIR__ . '/Command/BuiltIn')->name('*Command.php'); |
107
|
|
|
|
108
|
|
|
/** @var SplFileInfo $file */ |
109
|
|
|
foreach ($finder as $file) { |
110
|
|
|
$class = substr('\\Mage\\Command\\BuiltIn\\' . str_replace('/', '\\', $file->getRelativePathname()), 0, -4); |
111
|
|
|
if (class_exists($class)) { |
112
|
|
|
$reflex = new ReflectionClass($class); |
113
|
|
|
if ($reflex->isInstantiable()) { |
114
|
|
|
$command = new $class(); |
115
|
|
|
if ($command instanceof AbstractCommand) { |
116
|
|
|
$command->setRuntime($this->runtime); |
117
|
|
|
$this->add($command); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Gets the Runtime instance to use |
126
|
|
|
* |
127
|
|
|
* @return Runtime |
128
|
|
|
*/ |
129
|
|
|
protected function instantiateRuntime() |
130
|
|
|
{ |
131
|
|
|
return new Runtime(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the Runtime instance |
136
|
|
|
* |
137
|
|
|
* @return Runtime |
138
|
|
|
*/ |
139
|
|
|
public function getRuntime() |
140
|
|
|
{ |
141
|
|
|
return $this->runtime; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.