Created the console application via a factory

This commit is contained in:
Alejandro Celaya 2016-07-05 23:25:39 +02:00
parent 9ce5e255f1
commit 96478f3400
6 changed files with 64 additions and 14 deletions

View file

@ -1,16 +1,10 @@
#!/usr/bin/env php
<?php
use Acelaya\UrlShortener\CliCommands\GenerateShortcodeCommand;
use Acelaya\UrlShortener\CliCommands\ResolveUrlCommand;
use Interop\Container\ContainerInterface;
use Symfony\Component\Console\Application as CliApp;
/** @var ContainerInterface $container */
$container = include __DIR__ . '/../config/container.php';
$app = new CliApp();
$app->addCommands([
$container->get(GenerateShortcodeCommand::class),
$container->get(ResolveUrlCommand::class),
]);
$app = $container->get(CliApp::class);
$app->run();

View file

@ -0,0 +1,13 @@
<?php
use Acelaya\UrlShortener\CLI\Command;
return [
'cli' => [
'commands' => [
Command\GenerateShortcodeCommand::class,
Command\ResolveUrlCommand::class,
]
],
];

View file

@ -1,5 +1,5 @@
<?php
use Acelaya\UrlShortener\CliCommands;
use Acelaya\UrlShortener\CLI;
use Acelaya\UrlShortener\Factory\CacheFactory;
use Acelaya\UrlShortener\Factory\EntityManagerFactory;
use Acelaya\UrlShortener\Middleware;
@ -7,7 +7,8 @@ use Acelaya\UrlShortener\Service;
use Acelaya\ZsmAnnotatedServices\Factory\V3\AnnotatedFactory;
use Doctrine\Common\Cache\Cache;
use Doctrine\ORM\EntityManager;
use Zend\Expressive\Application;
use Symfony\Component\Console;
use Zend\Expressive;
use Zend\Expressive\Container;
use Zend\Expressive\Helper;
use Zend\Expressive\Router;
@ -19,7 +20,8 @@ return [
'services' => [
'factories' => [
Application::class => Container\ApplicationFactory::class,
Expressive\Application::class => Container\ApplicationFactory::class,
Console\Application::class => CLI\Factory\ApplicationFactory::class,
// Url helpers
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
@ -42,8 +44,8 @@ return [
Cache::class => CacheFactory::class,
// Cli commands
CliCommands\GenerateShortcodeCommand::class => AnnotatedFactory::class,
CliCommands\ResolveUrlCommand::class => AnnotatedFactory::class,
CLI\Command\GenerateShortcodeCommand::class => AnnotatedFactory::class,
CLI\Command\ResolveUrlCommand::class => AnnotatedFactory::class,
// Middleware
Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class,

View file

@ -1,5 +1,5 @@
<?php
namespace Acelaya\UrlShortener\CliCommands;
namespace Acelaya\UrlShortener\CLI\Command;
use Acelaya\UrlShortener\Exception\InvalidUrlException;
use Acelaya\UrlShortener\Service\UrlShortener;

View file

@ -1,5 +1,5 @@
<?php
namespace Acelaya\UrlShortener\CliCommands;
namespace Acelaya\UrlShortener\CLI\Command;
use Acelaya\UrlShortener\Exception\InvalidShortCodeException;
use Acelaya\UrlShortener\Service\UrlShortener;

View file

@ -0,0 +1,41 @@
<?php
namespace Acelaya\UrlShortener\CLI\Factory;
use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use Symfony\Component\Console\Application as CliApp;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\FactoryInterface;
class ApplicationFactory implements FactoryInterface
{
/**
* Create an object
*
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @return object
* @throws ServiceNotFoundException if unable to resolve the service.
* @throws ServiceNotCreatedException if an exception is raised when
* creating a service.
* @throws ContainerException if any other error occurs
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$config = $container->get('config')['cli'];
$app = new CliApp();
$commands = isset($config['commands']) ? $config['commands'] : [];
foreach ($commands as $command) {
if (! $container->has($command)) {
continue;
}
$app->add($container->get($command));
}
return $app;
}
}