diff --git a/bin/cli b/bin/cli index 1aa55f63..0aca7dd0 100644 --- a/bin/cli +++ b/bin/cli @@ -1,11 +1,14 @@ #!/usr/bin/env php get(Application::class); $command = count($_SERVER['argv']) > 1 ? $_SERVER['argv'][1] : ''; $request = ServerRequestFactory::fromGlobals() diff --git a/cli-config.php b/cli-config.php new file mode 100644 index 00000000..be9b8b82 --- /dev/null +++ b/cli-config.php @@ -0,0 +1,11 @@ +get(EntityManager::class); + +return ConsoleRunner::createHelperSet($em); diff --git a/composer.json b/composer.json index 486334ef..1d9e2ce0 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,8 @@ "zendframework/zend-servicemanager": "^3.0", "zendframework/zend-expressive-twigrenderer": "^1.0", "doctrine/orm": "^2.5", - "guzzlehttp/guzzle": "^6.2" + "guzzlehttp/guzzle": "^6.2", + "acelaya/zsm-annotated-services": "^0.2.0" }, "require-dev": { "phpunit/phpunit": "^4.8", diff --git a/config/autoload/database.local.php.dist b/config/autoload/database.local.php.dist new file mode 100644 index 00000000..f90c4a2e --- /dev/null +++ b/config/autoload/database.local.php.dist @@ -0,0 +1,15 @@ + [ + 'driver' => 'pdo_mysql', + 'user' => '', + 'password' => '', + 'dbname' => '', + 'charset' => 'utf-8', + 'driverOptions' => [ + PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' + ], + ] + +]; diff --git a/config/autoload/services.global.php b/config/autoload/services.global.php index 8f2a8816..f3ac64bd 100644 --- a/config/autoload/services.global.php +++ b/config/autoload/services.global.php @@ -1,16 +1,22 @@ [ 'invokables' => [ - Helper\ServerUrlHelper::class => Helper\ServerUrlHelper::class, - Router\RouterInterface::class => Router\AuraRouter::class, + Helper\ServerUrlHelper::class, + Router\AuraRouter::class, ], 'factories' => [ Application::class => Container\ApplicationFactory::class, @@ -19,11 +25,22 @@ return [ Helper\UrlHelper::class => Helper\UrlHelperFactory::class, Helper\ServerUrlMiddleware::class => Helper\ServerUrlMiddlewareFactory::class, Helper\UrlHelperMiddleware::class => Helper\UrlHelperMiddlewareFactory::class, + Helper\ServerUrlHelper::class => InvokableFactory::class, + Router\RouterInterface::class => InvokableFactory::class, // View 'Zend\Expressive\FinalHandler' => Container\TemplatedErrorHandlerFactory::class, - Template\TemplateRendererInterface::class => Zend\Expressive\Twig\TwigRendererFactory::class, + Template\TemplateRendererInterface::class => Twig\TwigRendererFactory::class, + + // Services + EntityManager::class => EntityManagerFactory::class, + GuzzleHttp\Client::class => InvokableFactory::class, + UrlShortener::class => AnnotatedFactory::class, ], + 'aliases' => [ + 'em' => EntityManager::class, + 'httpClient' => GuzzleHttp\Client::class, + ] ], ]; diff --git a/config/app.php b/config/container.php similarity index 74% rename from config/app.php rename to config/container.php index 08b73799..332023f8 100644 --- a/config/app.php +++ b/config/container.php @@ -4,11 +4,10 @@ use Zend\ServiceManager\ServiceManager; chdir(dirname(__DIR__)); -require __DIR__ . '/../vendor/autoload.php'; +require 'vendor/autoload.php'; // Build container $config = require __DIR__ . '/config.php'; $container = new ServiceManager($config['services']); $container->setService('config', $config); - -return $container->get(Application::class); +return $container; diff --git a/data/.gitignore b/data/cache/.gitignore similarity index 100% rename from data/.gitignore rename to data/cache/.gitignore diff --git a/data/proxies/.gitignore b/data/proxies/.gitignore new file mode 100644 index 00000000..d6b7ef32 --- /dev/null +++ b/data/proxies/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore diff --git a/public/index.php b/public/index.php index 16af56e5..6b310943 100644 --- a/public/index.php +++ b/public/index.php @@ -1,6 +1,9 @@ get(Application::class); $app->run(); diff --git a/src/Factory/EntityManagerFactory.php b/src/Factory/EntityManagerFactory.php new file mode 100644 index 00000000..8716b3f8 --- /dev/null +++ b/src/Factory/EntityManagerFactory.php @@ -0,0 +1,43 @@ +get('config'); + $isDevMode = isset($globalConfig['debug']) ? ((bool) $globalConfig['debug']) : false; + $cache = $container->has(Cache::class) ? $container->get(Cache::class) : new ArrayCache(); + $dbConfig = isset($globalConfig['database']) ? $globalConfig['database'] : []; + + return EntityManager::create($dbConfig, Setup::createAnnotationMetadataConfiguration( + ['src/Entity'], + $isDevMode, + 'data/proxies', + $cache, + false + )); + } +} diff --git a/src/Service/UrlShortener.php b/src/Service/UrlShortener.php index 55953691..3fee0cf3 100644 --- a/src/Service/UrlShortener.php +++ b/src/Service/UrlShortener.php @@ -5,6 +5,7 @@ use Acelaya\UrlShortener\Entity\ShortUrl; use Acelaya\UrlShortener\Exception\InvalidShortCodeException; use Acelaya\UrlShortener\Exception\InvalidUrlException; use Acelaya\UrlShortener\Exception\RuntimeException; +use Acelaya\ZsmAnnotatedServices\Annotation\Inject; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\ORMException; use GuzzleHttp\ClientInterface; @@ -28,6 +29,14 @@ class UrlShortener implements UrlShortenerInterface */ private $chars; + /** + * UrlShortener constructor. + * @param ClientInterface $httpClient + * @param EntityManagerInterface $em + * @param string $chars + * + * @Inject({"httpClient", "em"}) + */ public function __construct( ClientInterface $httpClient, EntityManagerInterface $em, diff --git a/tests/Factory/EntityManagerFactoryTest.php b/tests/Factory/EntityManagerFactoryTest.php new file mode 100644 index 00000000..4b9bd82f --- /dev/null +++ b/tests/Factory/EntityManagerFactoryTest.php @@ -0,0 +1,38 @@ +factory = new EntityManagerFactory(); + } + + /** + * @test + */ + public function serviceIsCreated() + { + $sm = new ServiceManager(['services' => [ + 'config' => [ + 'debug' => true, + 'database' => [ + 'driver' => 'pdo_sqlite', + ], + ], + ]]); + + $em = $this->factory->__invoke($sm, EntityManager::class); + $this->assertInstanceOf(EntityManager::class, $em); + } +}