mirror of
https://github.com/shlinkio/shlink.git
synced 2024-10-22 20:25:35 +03:00
Created the console application via a factory
This commit is contained in:
parent
9ce5e255f1
commit
96478f3400
6 changed files with 64 additions and 14 deletions
8
bin/cli
8
bin/cli
|
@ -1,16 +1,10 @@
|
||||||
#!/usr/bin/env php
|
#!/usr/bin/env php
|
||||||
<?php
|
<?php
|
||||||
use Acelaya\UrlShortener\CliCommands\GenerateShortcodeCommand;
|
|
||||||
use Acelaya\UrlShortener\CliCommands\ResolveUrlCommand;
|
|
||||||
use Interop\Container\ContainerInterface;
|
use Interop\Container\ContainerInterface;
|
||||||
use Symfony\Component\Console\Application as CliApp;
|
use Symfony\Component\Console\Application as CliApp;
|
||||||
|
|
||||||
/** @var ContainerInterface $container */
|
/** @var ContainerInterface $container */
|
||||||
$container = include __DIR__ . '/../config/container.php';
|
$container = include __DIR__ . '/../config/container.php';
|
||||||
|
|
||||||
$app = new CliApp();
|
$app = $container->get(CliApp::class);
|
||||||
$app->addCommands([
|
|
||||||
$container->get(GenerateShortcodeCommand::class),
|
|
||||||
$container->get(ResolveUrlCommand::class),
|
|
||||||
]);
|
|
||||||
$app->run();
|
$app->run();
|
||||||
|
|
13
config/autoload/cli.global.php
Normal file
13
config/autoload/cli.global.php
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
<?php
|
||||||
|
use Acelaya\UrlShortener\CLI\Command;
|
||||||
|
|
||||||
|
return [
|
||||||
|
|
||||||
|
'cli' => [
|
||||||
|
'commands' => [
|
||||||
|
Command\GenerateShortcodeCommand::class,
|
||||||
|
Command\ResolveUrlCommand::class,
|
||||||
|
]
|
||||||
|
],
|
||||||
|
|
||||||
|
];
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
use Acelaya\UrlShortener\CliCommands;
|
use Acelaya\UrlShortener\CLI;
|
||||||
use Acelaya\UrlShortener\Factory\CacheFactory;
|
use Acelaya\UrlShortener\Factory\CacheFactory;
|
||||||
use Acelaya\UrlShortener\Factory\EntityManagerFactory;
|
use Acelaya\UrlShortener\Factory\EntityManagerFactory;
|
||||||
use Acelaya\UrlShortener\Middleware;
|
use Acelaya\UrlShortener\Middleware;
|
||||||
|
@ -7,7 +7,8 @@ use Acelaya\UrlShortener\Service;
|
||||||
use Acelaya\ZsmAnnotatedServices\Factory\V3\AnnotatedFactory;
|
use Acelaya\ZsmAnnotatedServices\Factory\V3\AnnotatedFactory;
|
||||||
use Doctrine\Common\Cache\Cache;
|
use Doctrine\Common\Cache\Cache;
|
||||||
use Doctrine\ORM\EntityManager;
|
use Doctrine\ORM\EntityManager;
|
||||||
use Zend\Expressive\Application;
|
use Symfony\Component\Console;
|
||||||
|
use Zend\Expressive;
|
||||||
use Zend\Expressive\Container;
|
use Zend\Expressive\Container;
|
||||||
use Zend\Expressive\Helper;
|
use Zend\Expressive\Helper;
|
||||||
use Zend\Expressive\Router;
|
use Zend\Expressive\Router;
|
||||||
|
@ -19,7 +20,8 @@ return [
|
||||||
|
|
||||||
'services' => [
|
'services' => [
|
||||||
'factories' => [
|
'factories' => [
|
||||||
Application::class => Container\ApplicationFactory::class,
|
Expressive\Application::class => Container\ApplicationFactory::class,
|
||||||
|
Console\Application::class => CLI\Factory\ApplicationFactory::class,
|
||||||
|
|
||||||
// Url helpers
|
// Url helpers
|
||||||
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
|
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
|
||||||
|
@ -42,8 +44,8 @@ return [
|
||||||
Cache::class => CacheFactory::class,
|
Cache::class => CacheFactory::class,
|
||||||
|
|
||||||
// Cli commands
|
// Cli commands
|
||||||
CliCommands\GenerateShortcodeCommand::class => AnnotatedFactory::class,
|
CLI\Command\GenerateShortcodeCommand::class => AnnotatedFactory::class,
|
||||||
CliCommands\ResolveUrlCommand::class => AnnotatedFactory::class,
|
CLI\Command\ResolveUrlCommand::class => AnnotatedFactory::class,
|
||||||
|
|
||||||
// Middleware
|
// Middleware
|
||||||
Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class,
|
Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class,
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Acelaya\UrlShortener\CliCommands;
|
namespace Acelaya\UrlShortener\CLI\Command;
|
||||||
|
|
||||||
use Acelaya\UrlShortener\Exception\InvalidUrlException;
|
use Acelaya\UrlShortener\Exception\InvalidUrlException;
|
||||||
use Acelaya\UrlShortener\Service\UrlShortener;
|
use Acelaya\UrlShortener\Service\UrlShortener;
|
|
@ -1,5 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
namespace Acelaya\UrlShortener\CliCommands;
|
namespace Acelaya\UrlShortener\CLI\Command;
|
||||||
|
|
||||||
use Acelaya\UrlShortener\Exception\InvalidShortCodeException;
|
use Acelaya\UrlShortener\Exception\InvalidShortCodeException;
|
||||||
use Acelaya\UrlShortener\Service\UrlShortener;
|
use Acelaya\UrlShortener\Service\UrlShortener;
|
41
src/CLI/Factory/ApplicationFactory.php
Normal file
41
src/CLI/Factory/ApplicationFactory.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue