magallanes/src/MageApplication.php

173 lines
5.6 KiB
PHP
Raw Normal View History

2016-12-31 07:52:25 +01:00
<?php
2022-04-10 06:20:03 +02:00
2016-12-31 07:52:25 +01:00
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage;
use Mage\Command\AbstractCommand;
use Mage\Runtime\Runtime;
2016-12-31 07:52:25 +01:00
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Console\Event\ConsoleErrorEvent;
use Symfony\Component\Console\ConsoleEvents;
2016-12-31 07:52:25 +01:00
use Symfony\Component\Console\Application;
use Symfony\Component\Yaml\Parser;
use Symfony\Component\Yaml\Exception\ParseException;
2016-12-31 07:52:25 +01:00
use Mage\Runtime\Exception\RuntimeException;
2022-04-11 00:38:43 +02:00
use Symfony\Component\Filesystem\Filesystem;
2016-12-31 07:52:25 +01:00
/**
* The Console Application for launching the Mage command in a standalone instance
*
* @author Andrés Montañez <andresmontanez@gmail.com>
*/
class MageApplication extends Application
{
2022-04-10 06:20:03 +02:00
protected Runtime $runtime;
protected string $file;
2016-12-31 07:52:25 +01:00
2017-01-29 22:25:02 +01:00
/**
* @param string $file The YAML file from which to read the configuration
*/
2022-04-10 06:20:03 +02:00
public function __construct(string $file)
{
2017-01-29 23:25:03 +01:00
parent::__construct('Magallanes', Mage::VERSION);
2017-01-29 22:25:02 +01:00
2017-01-29 23:25:03 +01:00
$this->file = $file;
$dispatcher = new EventDispatcher();
2017-01-29 23:25:03 +01:00
$this->setDispatcher($dispatcher);
$dispatcher->addListener(ConsoleEvents::ERROR, function (ConsoleErrorEvent $event) {
$output = $event->getOutput();
$command = $event->getCommand();
2022-04-10 06:20:03 +02:00
$output->writeln(
sprintf('Oops, exception thrown while running command <info>%s</info>', $command->getName())
);
$exitCode = $event->getExitCode();
$event->setError(new \LogicException('Caught exception', $exitCode, $event->getError()));
});
2017-01-29 23:25:03 +01:00
$this->runtime = $this->instantiateRuntime();
$this->loadBuiltInCommands();
}
2016-12-31 07:52:25 +01:00
/**
* Configure the Magallanes Application
*
* @throws RuntimeException
*/
2022-04-10 06:20:03 +02:00
public function configure(): void
2016-12-31 07:52:25 +01:00
{
2017-01-29 22:25:02 +01:00
if (!file_exists($this->file) || !is_readable($this->file)) {
throw new RuntimeException(sprintf('The file "%s" does not exists or is not readable.', $this->file));
}
try {
$parser = new Parser();
2017-01-29 22:25:02 +01:00
$config = $parser->parse(file_get_contents($this->file));
} catch (ParseException $exception) {
2017-01-29 22:25:02 +01:00
throw new RuntimeException(sprintf('Error parsing the file "%s".', $this->file));
}
2017-01-29 22:10:41 +01:00
if (array_key_exists('magephp', $config) && is_array($config['magephp'])) {
2017-01-01 07:55:09 +01:00
$logger = null;
2022-04-10 06:20:03 +02:00
if (
array_key_exists('log_dir', $config['magephp']) &&
file_exists($config['magephp']['log_dir']) && is_dir($config['magephp']['log_dir'])
) {
2017-01-29 22:10:41 +01:00
$logfile = sprintf('%s/%s.log', $config['magephp']['log_dir'], date('Ymd_His'));
$config['magephp']['log_file'] = $logfile;
2016-12-31 07:52:25 +01:00
$logger = new Logger('magephp');
$logger->pushHandler(new StreamHandler($logfile));
2022-04-11 00:38:43 +02:00
$logLimit = isset($config['magephp']['log_limit']) ? intval($config['magephp']['log_limit']) : 30;
$this->clearOldLogs($config['magephp']['log_dir'], $logLimit);
} elseif (array_key_exists('log_dir', $config['magephp']) && !is_dir($config['magephp']['log_dir'])) {
2022-04-10 06:20:03 +02:00
throw new RuntimeException(
sprintf(
'The configured log_dir "%s" does not exists or is not a directory.',
$config['magephp']['log_dir']
)
);
2016-12-31 07:52:25 +01:00
}
2017-01-01 07:55:09 +01:00
2017-01-29 22:10:41 +01:00
$this->runtime->setConfiguration($config['magephp']);
2017-01-01 07:55:09 +01:00
$this->runtime->setLogger($logger);
2017-01-29 23:25:03 +01:00
return;
2016-12-31 07:52:25 +01:00
}
2022-04-10 06:20:03 +02:00
throw new RuntimeException(
sprintf('The file "%s" does not have a valid Magallanes configuration.', $this->file)
);
2016-12-31 07:52:25 +01:00
}
2022-04-11 00:38:43 +02:00
protected function clearOldLogs(string $logDir, int $logLimit): void
{
$filesystem = new Filesystem();
$finder = new Finder();
$finder
->files()
->followLinks()
->in($logDir)
->name('*.log')
->sortByModifiedTime()
->reverseSorting();
$logs = iterator_to_array($finder);
$logsToRemove = array_slice($logs, $logLimit - 1);
$filesystem->remove($logsToRemove);
}
2016-12-31 07:52:25 +01:00
/**
* Loads the BuiltIn Commands
*/
2022-04-10 06:20:03 +02:00
protected function loadBuiltInCommands(): void
2016-12-31 07:52:25 +01:00
{
$finder = new Finder();
$finder->files()->in(__DIR__ . '/Command/BuiltIn')->name('*Command.php');
/** @var SplFileInfo $file */
foreach ($finder as $file) {
$class = substr('\\Mage\\Command\\BuiltIn\\' . str_replace('/', '\\', $file->getRelativePathname()), 0, -4);
if (class_exists($class)) {
2022-04-10 06:20:03 +02:00
$reflex = new \ReflectionClass($class);
if ($reflex->isInstantiable()) {
$command = new $class();
if ($command instanceof AbstractCommand) {
$command->setRuntime($this->runtime);
$this->add($command);
}
2016-12-31 07:52:25 +01:00
}
}
}
}
2017-01-05 01:46:40 +01:00
/**
* Gets the Runtime instance to use
*/
2022-04-10 06:20:03 +02:00
protected function instantiateRuntime(): Runtime
2017-01-05 01:46:40 +01:00
{
return new Runtime();
}
/**
* Get the Runtime instance
*/
2022-04-10 06:20:03 +02:00
public function getRuntime(): Runtime
2017-01-05 01:46:40 +01:00
{
return $this->runtime;
}
2016-12-31 07:52:25 +01:00
}