From 5eefaf30719e86fcbff4519611c7432b8525a9e0 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 19 Jul 2016 16:30:48 +0200 Subject: [PATCH 01/13] Added config manager package --- composer.json | 6 +++-- config/autoload/zend-expressive.global.php | 2 +- config/config.php | 28 +++++++--------------- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/composer.json b/composer.json index 374fed26..9965ebe6 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { - "name": "acelaya/url-shortener", + "name": "shlinkio/shlink", "type": "project", - "homepage": "https://github.com/acelaya/url-shortener", + "homepage": "http://shlink.io", "license": "MIT", "authors": [ { @@ -19,6 +19,8 @@ "zendframework/zend-stdlib": "^2.7", "zendframework/zend-servicemanager": "^3.0", "zendframework/zend-paginator": "^2.6", + "zendframework/zend-config": "^2.6", + "mtymek/expressive-config-manager": "^0.4", "doctrine/orm": "^2.5", "guzzlehttp/guzzle": "^6.2", "acelaya/zsm-annotated-services": "^0.2.0", diff --git a/config/autoload/zend-expressive.global.php b/config/autoload/zend-expressive.global.php index db5e3f38..b2102394 100644 --- a/config/autoload/zend-expressive.global.php +++ b/config/autoload/zend-expressive.global.php @@ -3,7 +3,7 @@ return [ 'debug' => false, - 'config_cache_enabled' => false, + 'config_cache_enabled' => true, 'zend-expressive' => [ 'error_handler' => [ diff --git a/config/config.php b/config/config.php index a3d0e7ac..f835a11c 100644 --- a/config/config.php +++ b/config/config.php @@ -1,6 +1,6 @@ getMergedConfig(); +}); From 95d0beea3c254cc1a5f29f627297aad36fc3feb6 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 19 Jul 2016 16:50:02 +0200 Subject: [PATCH 02/13] Created CLI module --- composer.json | 11 +++++++++-- config/autoload/services.global.php | 2 +- cli-config.php => config/cli-config.php | 2 +- config/config.php | 2 ++ .../CLI/config/cli.config.php | 2 +- .../CLI/src}/Command/GenerateShortcodeCommand.php | 2 +- .../CLI/src}/Command/GetVisitsCommand.php | 2 +- .../CLI/src}/Command/ListShortcodesCommand.php | 2 +- .../CLI/src}/Command/ResolveUrlCommand.php | 2 +- module/CLI/src/Config/ConfigProvider.php | 13 +++++++++++++ .../CLI/src}/Factory/ApplicationFactory.php | 2 +- phpcs.xml | 3 +-- 12 files changed, 33 insertions(+), 12 deletions(-) rename cli-config.php => config/cli-config.php (83%) rename config/autoload/cli.global.php => module/CLI/config/cli.config.php (87%) rename {src/CLI => module/CLI/src}/Command/GenerateShortcodeCommand.php (98%) rename {src/CLI => module/CLI/src}/Command/GetVisitsCommand.php (98%) rename {src/CLI => module/CLI/src}/Command/ListShortcodesCommand.php (98%) rename {src/CLI => module/CLI/src}/Command/ResolveUrlCommand.php (98%) create mode 100644 module/CLI/src/Config/ConfigProvider.php rename {src/CLI => module/CLI/src}/Factory/ApplicationFactory.php (96%) diff --git a/composer.json b/composer.json index 9965ebe6..91f340c1 100644 --- a/composer.json +++ b/composer.json @@ -2,6 +2,7 @@ "name": "shlinkio/shlink", "type": "project", "homepage": "http://shlink.io", + "description": "A PHP-based URL shortener application with analytics and management", "license": "MIT", "authors": [ { @@ -36,12 +37,18 @@ }, "autoload": { "psr-4": { - "Acelaya\\UrlShortener\\": "src" + "Acelaya\\UrlShortener\\": "src", + "Shlinkio\\Shlink\\CLI\\": "module/CLI/src", + "Shlinkio\\Shlink\\Rest\\": "module/Rest/src", + "Shlinkio\\Shlink\\Core\\": "module/Core/src" } }, "autoload-dev": { "psr-4": { - "AcelayaTest\\UrlShortener\\": "tests" + "AcelayaTest\\UrlShortener\\": "tests", + "ShlinkioTest\\Shlink\\CLI\\": "module/CLI/test", + "ShlinkioTest\\Shlink\\Rest\\": "module/Rest/test", + "ShlinkioTest\\Shlink\\Core\\": "module/Core/test" } }, "scripts": { diff --git a/config/autoload/services.global.php b/config/autoload/services.global.php index 4d6cc40e..3277159e 100644 --- a/config/autoload/services.global.php +++ b/config/autoload/services.global.php @@ -1,5 +1,5 @@ get(EntityManager::class); diff --git a/config/config.php b/config/config.php index f835a11c..b27d8173 100644 --- a/config/config.php +++ b/config/config.php @@ -1,4 +1,5 @@ - src - tests + module config public/index.php From 55f954f50f1688e3413906b382103c8fa4359b40 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 19 Jul 2016 17:07:59 +0200 Subject: [PATCH 03/13] Created Rest module --- .../autoload/middleware-pipeline.global.php | 10 ----- config/autoload/routes.global.php | 33 ---------------- config/autoload/services.global.php | 7 ---- config/config.php | 4 +- module/CLI/src/Config/ConfigProvider.php | 13 ------- module/CLI/src/ConfigProvider.php | 13 +++++++ .../config/middleware-pipeline.global.php | 16 ++++++++ .../Rest/config/rest.config.php | 0 module/Rest/config/routes.global.php | 39 +++++++++++++++++++ module/Rest/config/services.global.php | 22 +++++++++++ .../src/Action}/AbstractRestMiddleware.php | 2 +- .../src/Action}/AuthenticateMiddleware.php | 8 ++-- .../src/Action}/CreateShortcodeMiddleware.php | 4 +- .../Rest/src/Action}/GetVisitsMiddleware.php | 4 +- .../src/Action}/ListShortcodesMiddleware.php | 5 +-- .../Rest/src/Action}/ResolveUrlMiddleware.php | 4 +- module/Rest/src/ConfigProvider.php | 13 +++++++ .../CheckAuthenticationMiddleware.php | 8 ++-- .../src}/Middleware/CrossDomainMiddleware.php | 2 +- .../Rest/src}/Service/RestTokenService.php | 2 +- .../Service/RestTokenServiceInterface.php | 2 +- {src => module/Rest/src}/Util/RestUtils.php | 2 +- 22 files changed, 127 insertions(+), 86 deletions(-) delete mode 100644 module/CLI/src/Config/ConfigProvider.php create mode 100644 module/CLI/src/ConfigProvider.php create mode 100644 module/Rest/config/middleware-pipeline.global.php rename config/autoload/rest.global.php => module/Rest/config/rest.config.php (100%) create mode 100644 module/Rest/config/routes.global.php create mode 100644 module/Rest/config/services.global.php rename {src/Middleware/Rest => module/Rest/src/Action}/AbstractRestMiddleware.php (97%) rename {src/Middleware/Rest => module/Rest/src/Action}/AuthenticateMiddleware.php (89%) rename {src/Middleware/Rest => module/Rest/src/Action}/CreateShortcodeMiddleware.php (96%) rename {src/Middleware/Rest => module/Rest/src/Action}/GetVisitsMiddleware.php (95%) rename {src/Middleware/Rest => module/Rest/src/Action}/ListShortcodesMiddleware.php (93%) rename {src/Middleware/Rest => module/Rest/src/Action}/ResolveUrlMiddleware.php (95%) create mode 100644 module/Rest/src/ConfigProvider.php rename {src => module/Rest/src}/Middleware/CheckAuthenticationMiddleware.php (94%) rename {src => module/Rest/src}/Middleware/CrossDomainMiddleware.php (97%) rename {src => module/Rest/src}/Service/RestTokenService.php (98%) rename {src => module/Rest/src}/Service/RestTokenServiceInterface.php (95%) rename {src => module/Rest/src}/Util/RestUtils.php (96%) diff --git a/config/autoload/middleware-pipeline.global.php b/config/autoload/middleware-pipeline.global.php index fc6f85f0..ca116a95 100644 --- a/config/autoload/middleware-pipeline.global.php +++ b/config/autoload/middleware-pipeline.global.php @@ -1,5 +1,4 @@ 10, ], - 'rest' => [ - 'path' => '/rest', - 'middleware' => [ - Middleware\CheckAuthenticationMiddleware::class, - Middleware\CrossDomainMiddleware::class, - ], - 'priority' => 5, - ], - 'post-routing' => [ 'middleware' => [ Helper\UrlHelperMiddleware::class, diff --git a/config/autoload/routes.global.php b/config/autoload/routes.global.php index 87133f09..40a3d20b 100644 --- a/config/autoload/routes.global.php +++ b/config/autoload/routes.global.php @@ -1,6 +1,5 @@ Routable\RedirectMiddleware::class, 'allowed_methods' => ['GET'], ], - - // Rest - [ - 'name' => 'rest-authenticate', - 'path' => '/rest/authenticate', - 'middleware' => Rest\AuthenticateMiddleware::class, - 'allowed_methods' => ['POST', 'OPTIONS'], - ], - [ - 'name' => 'rest-create-shortcode', - 'path' => '/rest/short-codes', - 'middleware' => Rest\CreateShortcodeMiddleware::class, - 'allowed_methods' => ['POST', 'OPTIONS'], - ], - [ - 'name' => 'rest-resolve-url', - 'path' => '/rest/short-codes/{shortCode}', - 'middleware' => Rest\ResolveUrlMiddleware::class, - 'allowed_methods' => ['GET', 'OPTIONS'], - ], - [ - 'name' => 'rest-list-shortened-url', - 'path' => '/rest/short-codes', - 'middleware' => Rest\ListShortcodesMiddleware::class, - 'allowed_methods' => ['GET'], - ], - [ - 'name' => 'rest-get-visits', - 'path' => '/rest/visits/{shortCode}', - 'middleware' => Rest\GetVisitsMiddleware::class, - 'allowed_methods' => ['GET', 'OPTIONS'], - ], ], ]; diff --git a/config/autoload/services.global.php b/config/autoload/services.global.php index 3277159e..bbf816d0 100644 --- a/config/autoload/services.global.php +++ b/config/autoload/services.global.php @@ -51,13 +51,6 @@ return [ // Middleware Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class, - Middleware\Rest\AuthenticateMiddleware::class => AnnotatedFactory::class, - Middleware\Rest\CreateShortcodeMiddleware::class => AnnotatedFactory::class, - Middleware\Rest\ResolveUrlMiddleware::class => AnnotatedFactory::class, - Middleware\Rest\GetVisitsMiddleware::class => AnnotatedFactory::class, - Middleware\Rest\ListShortcodesMiddleware::class => AnnotatedFactory::class, - Middleware\CrossDomainMiddleware::class => InvokableFactory::class, - Middleware\CheckAuthenticationMiddleware::class => AnnotatedFactory::class, ], 'aliases' => [ 'em' => EntityManager::class, diff --git a/config/config.php b/config/config.php index b27d8173..850f5e4f 100644 --- a/config/config.php +++ b/config/config.php @@ -1,5 +1,6 @@ [ + 'rest' => [ + 'path' => '/rest', + 'middleware' => [ + Middleware\CheckAuthenticationMiddleware::class, + Middleware\CrossDomainMiddleware::class, + ], + 'priority' => 5, + ], + ], +]; diff --git a/config/autoload/rest.global.php b/module/Rest/config/rest.config.php similarity index 100% rename from config/autoload/rest.global.php rename to module/Rest/config/rest.config.php diff --git a/module/Rest/config/routes.global.php b/module/Rest/config/routes.global.php new file mode 100644 index 00000000..e8abb6fe --- /dev/null +++ b/module/Rest/config/routes.global.php @@ -0,0 +1,39 @@ + [ + [ + 'name' => 'rest-authenticate', + 'path' => '/rest/authenticate', + 'middleware' => Action\AuthenticateMiddleware::class, + 'allowed_methods' => ['POST', 'OPTIONS'], + ], + [ + 'name' => 'rest-create-shortcode', + 'path' => '/rest/short-codes', + 'middleware' => Action\CreateShortcodeMiddleware::class, + 'allowed_methods' => ['POST', 'OPTIONS'], + ], + [ + 'name' => 'rest-resolve-url', + 'path' => '/rest/short-codes/{shortCode}', + 'middleware' => Action\ResolveUrlMiddleware::class, + 'allowed_methods' => ['GET', 'OPTIONS'], + ], + [ + 'name' => 'rest-lActionist-shortened-url', + 'path' => '/rest/short-codes', + 'middleware' => Action\ListShortcodesMiddleware::class, + 'allowed_methods' => ['GET'], + ], + [ + 'name' => 'rest-get-visits', + 'path' => '/rest/visits/{shortCode}', + 'middleware' => Action\GetVisitsMiddleware::class, + 'allowed_methods' => ['GET', 'OPTIONS'], + ], + ], + +]; diff --git a/module/Rest/config/services.global.php b/module/Rest/config/services.global.php new file mode 100644 index 00000000..d0975ec8 --- /dev/null +++ b/module/Rest/config/services.global.php @@ -0,0 +1,22 @@ + [ + 'factories' => [ + Action\AuthenticateMiddleware::class => AnnotatedFactory::class, + Action\CreateShortcodeMiddleware::class => AnnotatedFactory::class, + Action\ResolveUrlMiddleware::class => AnnotatedFactory::class, + Action\GetVisitsMiddleware::class => AnnotatedFactory::class, + Action\ListShortcodesMiddleware::class => AnnotatedFactory::class, + + Middleware\CrossDomainMiddleware::class => InvokableFactory::class, + Middleware\CheckAuthenticationMiddleware::class => AnnotatedFactory::class, + ], + ], + +]; diff --git a/src/Middleware/Rest/AbstractRestMiddleware.php b/module/Rest/src/Action/AbstractRestMiddleware.php similarity index 97% rename from src/Middleware/Rest/AbstractRestMiddleware.php rename to module/Rest/src/Action/AbstractRestMiddleware.php index 1168ff60..9ef870f4 100644 --- a/src/Middleware/Rest/AbstractRestMiddleware.php +++ b/module/Rest/src/Action/AbstractRestMiddleware.php @@ -1,5 +1,5 @@ Date: Tue, 19 Jul 2016 17:12:50 +0200 Subject: [PATCH 04/13] Fixed config files names --- config/autoload/services.global.php | 1 - config/config.php | 2 +- ...ware-pipeline.global.php => middleware-pipeline.config.php} | 0 module/Rest/config/{routes.global.php => routes.config.php} | 0 .../Rest/config/{services.global.php => services.config.php} | 3 +++ 5 files changed, 4 insertions(+), 2 deletions(-) rename module/Rest/config/{middleware-pipeline.global.php => middleware-pipeline.config.php} (100%) rename module/Rest/config/{routes.global.php => routes.config.php} (100%) rename module/Rest/config/{services.global.php => services.config.php} (88%) diff --git a/config/autoload/services.global.php b/config/autoload/services.global.php index bbf816d0..81c5669f 100644 --- a/config/autoload/services.global.php +++ b/config/autoload/services.global.php @@ -40,7 +40,6 @@ return [ Service\UrlShortener::class => AnnotatedFactory::class, Service\VisitsTracker::class => AnnotatedFactory::class, Service\ShortUrlService::class => AnnotatedFactory::class, - Service\RestTokenService::class => AnnotatedFactory::class, Cache::class => CacheFactory::class, // Cli commands diff --git a/config/config.php b/config/config.php index 850f5e4f..0fad2300 100644 --- a/config/config.php +++ b/config/config.php @@ -15,9 +15,9 @@ use Zend\Expressive\ConfigManager\ZendConfigProvider; return call_user_func(function () { $configManager = new ConfigManager([ + new ZendConfigProvider('config/autoload/{{,*.}global,{,*.}local}.php'), CLI\ConfigProvider::class, Rest\ConfigProvider::class, - new ZendConfigProvider('config/autoload/{{,*.}global,{,*.}local}.php') ], 'data/cache/app_config.php'); return $configManager->getMergedConfig(); diff --git a/module/Rest/config/middleware-pipeline.global.php b/module/Rest/config/middleware-pipeline.config.php similarity index 100% rename from module/Rest/config/middleware-pipeline.global.php rename to module/Rest/config/middleware-pipeline.config.php diff --git a/module/Rest/config/routes.global.php b/module/Rest/config/routes.config.php similarity index 100% rename from module/Rest/config/routes.global.php rename to module/Rest/config/routes.config.php diff --git a/module/Rest/config/services.global.php b/module/Rest/config/services.config.php similarity index 88% rename from module/Rest/config/services.global.php rename to module/Rest/config/services.config.php index d0975ec8..aff4cc96 100644 --- a/module/Rest/config/services.global.php +++ b/module/Rest/config/services.config.php @@ -2,12 +2,15 @@ use Acelaya\ZsmAnnotatedServices\Factory\V3\AnnotatedFactory; use Shlinkio\Shlink\Rest\Action; use Shlinkio\Shlink\Rest\Middleware; +use Shlinkio\Shlink\Rest\Service; use Zend\ServiceManager\Factory\InvokableFactory; return [ 'services' => [ 'factories' => [ + Service\RestTokenService::class => AnnotatedFactory::class, + Action\AuthenticateMiddleware::class => AnnotatedFactory::class, Action\CreateShortcodeMiddleware::class => AnnotatedFactory::class, Action\ResolveUrlMiddleware::class => AnnotatedFactory::class, From 7efb3b3a86b81a092f69334f45025dca130118a9 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 19 Jul 2016 17:17:37 +0200 Subject: [PATCH 05/13] Created cli-specific services config file --- config/autoload/services.global.php | 11 +---------- module/CLI/config/services.config.php | 20 ++++++++++++++++++++ src/Service/UrlShortener.php | 2 +- 3 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 module/CLI/config/services.config.php diff --git a/config/autoload/services.global.php b/config/autoload/services.global.php index 81c5669f..bf5b28e4 100644 --- a/config/autoload/services.global.php +++ b/config/autoload/services.global.php @@ -1,5 +1,4 @@ [ 'factories' => [ Expressive\Application::class => Container\ApplicationFactory::class, - Console\Application::class => CLI\Factory\ApplicationFactory::class, // Url helpers Helper\UrlHelper::class => Helper\UrlHelperFactory::class, @@ -42,12 +39,6 @@ return [ Service\ShortUrlService::class => AnnotatedFactory::class, Cache::class => CacheFactory::class, - // Cli commands - CLI\Command\GenerateShortcodeCommand::class => AnnotatedFactory::class, - CLI\Command\ResolveUrlCommand::class => AnnotatedFactory::class, - CLI\Command\ListShortcodesCommand::class => AnnotatedFactory::class, - CLI\Command\GetVisitsCommand::class => AnnotatedFactory::class, - // Middleware Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class, ], @@ -56,7 +47,7 @@ return [ 'httpClient' => GuzzleHttp\Client::class, Router\RouterInterface::class => Router\FastRouteRouter::class, AnnotatedFactory::CACHE_SERVICE => Cache::class, - ] + ], ], ]; diff --git a/module/CLI/config/services.config.php b/module/CLI/config/services.config.php new file mode 100644 index 00000000..5c10f5a9 --- /dev/null +++ b/module/CLI/config/services.config.php @@ -0,0 +1,20 @@ + [ + 'factories' => [ + Console\Application::class => CLI\Factory\ApplicationFactory::class, + + CLI\Command\GenerateShortcodeCommand::class => AnnotatedFactory::class, + CLI\Command\ResolveUrlCommand::class => AnnotatedFactory::class, + CLI\Command\ListShortcodesCommand::class => AnnotatedFactory::class, + CLI\Command\GetVisitsCommand::class => AnnotatedFactory::class, + ], + ], + +]; diff --git a/src/Service/UrlShortener.php b/src/Service/UrlShortener.php index 3c15a0f6..c538110d 100644 --- a/src/Service/UrlShortener.php +++ b/src/Service/UrlShortener.php @@ -44,7 +44,7 @@ class UrlShortener implements UrlShortenerInterface ) { $this->httpClient = $httpClient; $this->em = $em; - $this->chars = $chars; + $this->chars = empty($chars) ? self::DEFAULT_CHARS : $chars; } /** From 8fc88171ee96df00230c3f0d594f7bd2b915dcd2 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 19 Jul 2016 17:27:55 +0200 Subject: [PATCH 06/13] Moved AuthenticationException to Rest module --- composer.json | 6 ++++-- module/Rest/src/Action/AuthenticateMiddleware.php | 2 +- .../Rest/src}/Exception/AuthenticationException.php | 4 +++- module/Rest/src/Service/RestTokenService.php | 2 +- .../Rest/src/Service/RestTokenServiceInterface.php | 2 +- module/Rest/src/Util/RestUtils.php | 13 +++++++------ 6 files changed, 17 insertions(+), 12 deletions(-) rename {src => module/Rest/src}/Exception/AuthenticationException.php (74%) diff --git a/composer.json b/composer.json index 91f340c1..a64197a6 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,8 @@ "Acelaya\\UrlShortener\\": "src", "Shlinkio\\Shlink\\CLI\\": "module/CLI/src", "Shlinkio\\Shlink\\Rest\\": "module/Rest/src", - "Shlinkio\\Shlink\\Core\\": "module/Core/src" + "Shlinkio\\Shlink\\Core\\": "module/Core/src", + "Shlinkio\\Shlink\\Common\\": "module/Common/src" } }, "autoload-dev": { @@ -48,7 +49,8 @@ "AcelayaTest\\UrlShortener\\": "tests", "ShlinkioTest\\Shlink\\CLI\\": "module/CLI/test", "ShlinkioTest\\Shlink\\Rest\\": "module/Rest/test", - "ShlinkioTest\\Shlink\\Core\\": "module/Core/test" + "ShlinkioTest\\Shlink\\Core\\": "module/Core/test", + "ShlinkioTest\\Shlink\\Common\\": "module/Common/test" } }, "scripts": { diff --git a/module/Rest/src/Action/AuthenticateMiddleware.php b/module/Rest/src/Action/AuthenticateMiddleware.php index 97b6ad9f..cbea1360 100644 --- a/module/Rest/src/Action/AuthenticateMiddleware.php +++ b/module/Rest/src/Action/AuthenticateMiddleware.php @@ -1,10 +1,10 @@ Date: Tue, 19 Jul 2016 17:38:41 +0200 Subject: [PATCH 07/13] Created Common module --- config/autoload/services.global.php | 10 -------- config/config.php | 2 ++ .../CLI/src/Command/ListShortcodesCommand.php | 4 +-- module/Common/config/services.config.php | 25 +++++++++++++++++++ module/Common/src/ConfigProvider.php | 13 ++++++++++ .../Common/src}/Factory/CacheFactory.php | 2 +- .../src}/Factory/EntityManagerFactory.php | 2 +- .../Adapter/PaginableRepositoryAdapter.php | 4 +-- .../Paginator/Util/PaginatorUtilsTrait.php | 2 +- .../PaginableRepositoryInterface.php | 2 +- .../Common/src}/Util/StringUtilsTrait.php | 2 +- .../Common/test}/Factory/CacheFactoryTest.php | 4 +-- .../Factory/EntityManagerFactoryTest.php | 4 +-- .../src/Action/ListShortcodesMiddleware.php | 2 +- src/Entity/RestToken.php | 2 +- .../ShortUrlRepositoryInterface.php | 1 + src/Service/ShortUrlService.php | 2 +- 17 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 module/Common/config/services.config.php create mode 100644 module/Common/src/ConfigProvider.php rename {src => module/Common/src}/Factory/CacheFactory.php (96%) rename {src => module/Common/src}/Factory/EntityManagerFactory.php (97%) rename {src => module/Common/src}/Paginator/Adapter/PaginableRepositoryAdapter.php (91%) rename {src => module/Common/src}/Paginator/Util/PaginatorUtilsTrait.php (93%) rename {src => module/Common/src}/Repository/PaginableRepositoryInterface.php (92%) rename {src => module/Common/src}/Util/StringUtilsTrait.php (96%) rename {tests => module/Common/test}/Factory/CacheFactoryTest.php (91%) rename {tests => module/Common/test}/Factory/EntityManagerFactoryTest.php (88%) diff --git a/config/autoload/services.global.php b/config/autoload/services.global.php index bf5b28e4..00304e5f 100644 --- a/config/autoload/services.global.php +++ b/config/autoload/services.global.php @@ -1,11 +1,7 @@ Twig\TwigRendererFactory::class, // Services - EntityManager::class => EntityManagerFactory::class, - GuzzleHttp\Client::class => InvokableFactory::class, Service\UrlShortener::class => AnnotatedFactory::class, Service\VisitsTracker::class => AnnotatedFactory::class, Service\ShortUrlService::class => AnnotatedFactory::class, - Cache::class => CacheFactory::class, // Middleware Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class, ], 'aliases' => [ - 'em' => EntityManager::class, - 'httpClient' => GuzzleHttp\Client::class, Router\RouterInterface::class => Router\FastRouteRouter::class, - AnnotatedFactory::CACHE_SERVICE => Cache::class, ], ], diff --git a/config/config.php b/config/config.php index 0fad2300..5b1fedf9 100644 --- a/config/config.php +++ b/config/config.php @@ -1,5 +1,6 @@ [ + 'factories' => [ + EntityManager::class => EntityManagerFactory::class, + GuzzleHttp\Client::class => InvokableFactory::class, + Cache::class => CacheFactory::class, + ], + 'aliases' => [ + 'em' => EntityManager::class, + 'httpClient' => GuzzleHttp\Client::class, + AnnotatedFactory::CACHE_SERVICE => Cache::class, + ], + ], + +]; diff --git a/module/Common/src/ConfigProvider.php b/module/Common/src/ConfigProvider.php new file mode 100644 index 00000000..9af040c2 --- /dev/null +++ b/module/Common/src/ConfigProvider.php @@ -0,0 +1,13 @@ + Date: Tue, 19 Jul 2016 18:01:39 +0200 Subject: [PATCH 08/13] Created Core module --- composer.json | 2 -- config/autoload/services.global.php | 11 ---------- config/config.php | 2 ++ .../src/Command/GenerateShortcodeCommand.php | 7 +++---- module/CLI/src/Command/GetVisitsCommand.php | 4 ++-- .../CLI/src/Command/ListShortcodesCommand.php | 4 ++-- module/CLI/src/Command/ResolveUrlCommand.php | 6 +++--- .../Common/src}/Entity/AbstractEntity.php | 2 +- .../Core/config/routes.config.php | 4 ++-- module/Core/config/services.config.php | 20 +++++++++++++++++++ .../Core/src/Action}/RedirectMiddleware.php | 10 +++++----- module/Core/src/ConfigProvider.php | 13 ++++++++++++ {src => module/Core/src}/Entity/RestToken.php | 3 ++- {src => module/Core/src}/Entity/ShortUrl.php | 5 +++-- {src => module/Core/src}/Entity/Visit.php | 3 ++- .../Core/src/Exception/ExceptionInterface.php | 6 ++++++ .../Exception/InvalidArgumentException.php | 2 +- .../Exception/InvalidShortCodeException.php | 2 +- .../src}/Exception/InvalidUrlException.php | 2 +- .../Core/src}/Exception/RuntimeException.php | 2 +- .../src}/Repository/ShortUrlRepository.php | 5 ++--- .../ShortUrlRepositoryInterface.php | 2 +- .../Core/src}/Service/ShortUrlService.php | 6 +++--- .../src}/Service/ShortUrlServiceInterface.php | 4 ++-- .../Core/src}/Service/UrlShortener.php | 10 +++++----- .../src}/Service/UrlShortenerInterface.php | 8 ++++---- .../Core/src}/Service/VisitsTracker.php | 9 ++++----- .../src}/Service/VisitsTrackerInterface.php | 4 ++-- .../test}/Service/ShortUrlServiceTest.php | 8 ++++---- .../Core/test}/Service/UrlShortenerTest.php | 7 +++---- .../Core/test}/Service/VisitsTrackerTest.php | 6 +++--- .../src/Action/CreateShortcodeMiddleware.php | 6 +++--- .../Rest/src/Action/GetVisitsMiddleware.php | 6 +++--- .../src/Action/ListShortcodesMiddleware.php | 4 ++-- .../Rest/src/Action/ResolveUrlMiddleware.php | 6 +++--- .../src/Exception/AuthenticationException.php | 2 +- .../CheckAuthenticationMiddleware.php | 2 +- module/Rest/src/Service/RestTokenService.php | 4 ++-- .../src/Service/RestTokenServiceInterface.php | 4 ++-- module/Rest/src/Util/RestUtils.php | 2 +- src/Exception/ExceptionInterface.php | 6 ------ 41 files changed, 121 insertions(+), 100 deletions(-) rename {src => module/Common/src}/Entity/AbstractEntity.php (92%) rename config/autoload/routes.global.php => module/Core/config/routes.config.php (63%) create mode 100644 module/Core/config/services.config.php rename {src/Middleware/Routable => module/Core/src/Action}/RedirectMiddleware.php (91%) create mode 100644 module/Core/src/ConfigProvider.php rename {src => module/Core/src}/Entity/RestToken.php (95%) rename {src => module/Core/src}/Entity/ShortUrl.php (95%) rename {src => module/Core/src}/Entity/Visit.php (97%) create mode 100644 module/Core/src/Exception/ExceptionInterface.php rename {src => module/Core/src}/Exception/InvalidArgumentException.php (71%) rename {src => module/Core/src}/Exception/InvalidShortCodeException.php (90%) rename {src => module/Core/src}/Exception/InvalidUrlException.php (88%) rename {src => module/Core/src}/Exception/RuntimeException.php (68%) rename {src => module/Core/src}/Repository/ShortUrlRepository.php (93%) rename {src => module/Core/src}/Repository/ShortUrlRepositoryInterface.php (83%) rename {src => module/Core/src}/Service/ShortUrlService.php (88%) rename {src => module/Core/src}/Service/ShortUrlServiceInterface.php (70%) rename {src => module/Core/src}/Service/UrlShortener.php (94%) rename {src => module/Core/src}/Service/UrlShortenerInterface.php (74%) rename {src => module/Core/src}/Service/VisitsTracker.php (90%) rename {src => module/Core/src}/Service/VisitsTrackerInterface.php (86%) rename {tests => module/Core/test}/Service/ShortUrlServiceTest.php (86%) rename {tests => module/Core/test}/Service/UrlShortenerTest.php (96%) rename {tests => module/Core/test}/Service/VisitsTrackerTest.php (85%) delete mode 100644 src/Exception/ExceptionInterface.php diff --git a/composer.json b/composer.json index a64197a6..0211ed05 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,6 @@ }, "autoload": { "psr-4": { - "Acelaya\\UrlShortener\\": "src", "Shlinkio\\Shlink\\CLI\\": "module/CLI/src", "Shlinkio\\Shlink\\Rest\\": "module/Rest/src", "Shlinkio\\Shlink\\Core\\": "module/Core/src", @@ -46,7 +45,6 @@ }, "autoload-dev": { "psr-4": { - "AcelayaTest\\UrlShortener\\": "tests", "ShlinkioTest\\Shlink\\CLI\\": "module/CLI/test", "ShlinkioTest\\Shlink\\Rest\\": "module/Rest/test", "ShlinkioTest\\Shlink\\Core\\": "module/Core/test", diff --git a/config/autoload/services.global.php b/config/autoload/services.global.php index 00304e5f..dc99033c 100644 --- a/config/autoload/services.global.php +++ b/config/autoload/services.global.php @@ -1,7 +1,4 @@ Container\TemplatedErrorHandlerFactory::class, Template\TemplateRendererInterface::class => Twig\TwigRendererFactory::class, - - // Services - Service\UrlShortener::class => AnnotatedFactory::class, - Service\VisitsTracker::class => AnnotatedFactory::class, - Service\ShortUrlService::class => AnnotatedFactory::class, - - // Middleware - Middleware\Routable\RedirectMiddleware::class => AnnotatedFactory::class, ], 'aliases' => [ Router\RouterInterface::class => Router\FastRouteRouter::class, diff --git a/config/config.php b/config/config.php index 5b1fedf9..736b8fd4 100644 --- a/config/config.php +++ b/config/config.php @@ -1,6 +1,7 @@ 'long-url-redirect', 'path' => '/{shortCode}', - 'middleware' => Routable\RedirectMiddleware::class, + 'middleware' => RedirectMiddleware::class, 'allowed_methods' => ['GET'], ], ], diff --git a/module/Core/config/services.config.php b/module/Core/config/services.config.php new file mode 100644 index 00000000..cab7ca41 --- /dev/null +++ b/module/Core/config/services.config.php @@ -0,0 +1,20 @@ + [ + 'factories' => [ + // Services + Service\UrlShortener::class => AnnotatedFactory::class, + Service\VisitsTracker::class => AnnotatedFactory::class, + Service\ShortUrlService::class => AnnotatedFactory::class, + + // Middleware + RedirectMiddleware::class => AnnotatedFactory::class, + ], + ], + +]; diff --git a/src/Middleware/Routable/RedirectMiddleware.php b/module/Core/src/Action/RedirectMiddleware.php similarity index 91% rename from src/Middleware/Routable/RedirectMiddleware.php rename to module/Core/src/Action/RedirectMiddleware.php index 85b42eb6..5b1907ca 100644 --- a/src/Middleware/Routable/RedirectMiddleware.php +++ b/module/Core/src/Action/RedirectMiddleware.php @@ -1,13 +1,13 @@ Date: Tue, 19 Jul 2016 18:05:06 +0200 Subject: [PATCH 09/13] Fixed elements broken on module separation --- module/Common/src/Factory/EntityManagerFactory.php | 2 +- module/Core/test/Service/UrlShortenerTest.php | 6 +++--- phpunit.xml.dist | 7 +++++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/module/Common/src/Factory/EntityManagerFactory.php b/module/Common/src/Factory/EntityManagerFactory.php index d6de017c..e42abb40 100644 --- a/module/Common/src/Factory/EntityManagerFactory.php +++ b/module/Common/src/Factory/EntityManagerFactory.php @@ -33,7 +33,7 @@ class EntityManagerFactory implements FactoryInterface $dbConfig = isset($globalConfig['database']) ? $globalConfig['database'] : []; return EntityManager::create($dbConfig, Setup::createAnnotationMetadataConfiguration( - ['src/Entity'], + ['module/Core/src/Entity'], $isDevMode, 'data/proxies', $cache, diff --git a/module/Core/test/Service/UrlShortenerTest.php b/module/Core/test/Service/UrlShortenerTest.php index 0c4bae4d..f67086b5 100644 --- a/module/Core/test/Service/UrlShortenerTest.php +++ b/module/Core/test/Service/UrlShortenerTest.php @@ -65,7 +65,7 @@ class UrlShortenerTest extends TestCase /** * @test - * @expectedException \Acelaya\UrlShortener\Exception\RuntimeException + * @expectedException \Shlinkio\Shlink\Core\Exception\RuntimeException */ public function exceptionIsThrownWhenOrmThrowsException() { @@ -81,7 +81,7 @@ class UrlShortenerTest extends TestCase /** * @test - * @expectedException \Acelaya\UrlShortener\Exception\InvalidUrlException + * @expectedException \Shlinkio\Shlink\Core\Exception\InvalidUrlException */ public function exceptionIsThrownWhenUrlDoesNotExist() { @@ -126,7 +126,7 @@ class UrlShortenerTest extends TestCase /** * @test - * @expectedException \Acelaya\UrlShortener\Exception\InvalidShortCodeException + * @expectedException \Shlinkio\Shlink\Core\Exception\InvalidShortCodeException */ public function invalidCharSetThrowsException() { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0721b191..61fefa13 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,7 +1,10 @@ - - ./tests + + ./module/Common/test + + + ./module/Core/test From f917697b8eb677b72fc6ec85d7abe4ff5d538e11 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 19 Jul 2016 18:19:05 +0200 Subject: [PATCH 10/13] Added first tests to Rest module --- .../Middleware/CrossDomainMiddlewareTest.php | 54 +++++++++++++++++++ phpunit.xml.dist | 3 ++ 2 files changed, 57 insertions(+) create mode 100644 module/Rest/test/Middleware/CrossDomainMiddlewareTest.php diff --git a/module/Rest/test/Middleware/CrossDomainMiddlewareTest.php b/module/Rest/test/Middleware/CrossDomainMiddlewareTest.php new file mode 100644 index 00000000..92656c82 --- /dev/null +++ b/module/Rest/test/Middleware/CrossDomainMiddlewareTest.php @@ -0,0 +1,54 @@ +middleware = new CrossDomainMiddleware(); + } + + /** + * @test + */ + public function anyRequestIncludesTheAllowAccessHeader() + { + $response = $this->middleware->__invoke( + ServerRequestFactory::fromGlobals(), + new Response(), + function ($req, $resp) { + return $resp; + } + ); + + $headers = $response->getHeaders(); + $this->assertArrayHasKey('Access-Control-Allow-Origin', $headers); + $this->assertArrayNotHasKey('Access-Control-Allow-Headers', $headers); + } + + /** + * @test + */ + public function optionsRequestIncludesMoreHeaders() + { + $request = ServerRequestFactory::fromGlobals(['REQUEST_METHOD' => 'OPTIONS']); + + $response = $this->middleware->__invoke($request, new Response(), function ($req, $resp) { + return $resp; + }); + + $headers = $response->getHeaders(); + $this->assertArrayHasKey('Access-Control-Allow-Origin', $headers); + $this->assertArrayHasKey('Access-Control-Allow-Headers', $headers); + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 61fefa13..2c43e876 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -6,6 +6,9 @@ ./module/Core/test + + ./module/Rest/test + From 923abdf4d2c88eb6797350e2b9bb5a4e99abc20f Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 19 Jul 2016 18:28:21 +0200 Subject: [PATCH 11/13] Added first tests for CLI module --- .../test/Factory/ApplicationFactoryTest.php | 60 +++++++++++++++++++ phpunit.xml.dist | 3 + 2 files changed, 63 insertions(+) create mode 100644 module/CLI/test/Factory/ApplicationFactoryTest.php diff --git a/module/CLI/test/Factory/ApplicationFactoryTest.php b/module/CLI/test/Factory/ApplicationFactoryTest.php new file mode 100644 index 00000000..1c486e55 --- /dev/null +++ b/module/CLI/test/Factory/ApplicationFactoryTest.php @@ -0,0 +1,60 @@ +factory = new ApplicationFactory(); + } + + /** + * @test + */ + public function serviceIsCreated() + { + $instance = $this->factory->__invoke($this->createServiceManager(), ''); + $this->assertInstanceOf(Application::class, $instance); + } + + /** + * @test + */ + public function allCommandsWhichAreServicesAreAdded() + { + $sm = $this->createServiceManager([ + 'commands' => [ + 'foo', + 'bar', + 'baz', + ], + ]); + $sm->setService('foo', $this->prophesize(Command::class)->reveal()); + $sm->setService('baz', $this->prophesize(Command::class)->reveal()); + + /** @var Application $instance */ + $instance = $this->factory->__invoke($sm, ''); + $this->assertInstanceOf(Application::class, $instance); + $this->assertCount(2, $instance->all()); + } + + protected function createServiceManager($config = []) + { + return new ServiceManager(['services' => [ + 'config' => [ + 'cli' => $config, + ], + ]]); + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 2c43e876..65d36865 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,6 +9,9 @@ ./module/Rest/test + + ./module/CLI/test + From 39598d8608c34af4ec47b1622777b80c9f624bbc Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 19 Jul 2016 18:32:59 +0200 Subject: [PATCH 12/13] Moved templates and templates config to Core module --- config/autoload/templates.global.php | 6 ------ config/autoload/zend-expressive.global.php | 8 +------- module/Core/config/templates.config.php | 11 +++++++++++ module/Core/config/zend-expressive.config.php | 12 ++++++++++++ .../Core/templates/core}/error/404.html.twig | 2 +- .../Core/templates/core}/error/error.html.twig | 2 +- .../Core/templates/core}/layout/default.html.twig | 0 7 files changed, 26 insertions(+), 15 deletions(-) create mode 100644 module/Core/config/templates.config.php create mode 100644 module/Core/config/zend-expressive.config.php rename {templates => module/Core/templates/core}/error/404.html.twig (89%) rename {templates => module/Core/templates/core}/error/error.html.twig (91%) rename {templates => module/Core/templates/core}/layout/default.html.twig (100%) diff --git a/config/autoload/templates.global.php b/config/autoload/templates.global.php index 2beb9918..f102baa0 100644 --- a/config/autoload/templates.global.php +++ b/config/autoload/templates.global.php @@ -2,12 +2,6 @@ return [ - 'templates' => [ - 'paths' => [ - 'templates' - ], - ], - 'twig' => [ 'cache_dir' => 'data/cache/twig', 'extensions' => [ diff --git a/config/autoload/zend-expressive.global.php b/config/autoload/zend-expressive.global.php index b2102394..aa2e9d3b 100644 --- a/config/autoload/zend-expressive.global.php +++ b/config/autoload/zend-expressive.global.php @@ -1,14 +1,8 @@ false, + 'debug' => false, 'config_cache_enabled' => true, - 'zend-expressive' => [ - 'error_handler' => [ - 'template_404' => 'error/404.html.twig', - 'template_error' => 'error/error.html.twig', - ], - ], ]; diff --git a/module/Core/config/templates.config.php b/module/Core/config/templates.config.php new file mode 100644 index 00000000..da3623c1 --- /dev/null +++ b/module/Core/config/templates.config.php @@ -0,0 +1,11 @@ + [ + 'paths' => [ + 'module/Core/templates', + ], + ], + +]; diff --git a/module/Core/config/zend-expressive.config.php b/module/Core/config/zend-expressive.config.php new file mode 100644 index 00000000..c5fefe8f --- /dev/null +++ b/module/Core/config/zend-expressive.config.php @@ -0,0 +1,12 @@ + [ + 'error_handler' => [ + 'template_404' => 'core/error/404.html.twig', + 'template_error' => 'core/error/error.html.twig', + ], + ], + +]; diff --git a/templates/error/404.html.twig b/module/Core/templates/core/error/404.html.twig similarity index 89% rename from templates/error/404.html.twig rename to module/Core/templates/core/error/404.html.twig index 0e591e2a..0c28f29d 100644 --- a/templates/error/404.html.twig +++ b/module/Core/templates/core/error/404.html.twig @@ -1,4 +1,4 @@ -{% extends 'layout/default.html.twig' %} +{% extends 'core/layout/default.html.twig' %} {% block title %}URL Not Found{% endblock %} diff --git a/templates/error/error.html.twig b/module/Core/templates/core/error/error.html.twig similarity index 91% rename from templates/error/error.html.twig rename to module/Core/templates/core/error/error.html.twig index cd54354e..c93b3b7e 100644 --- a/templates/error/error.html.twig +++ b/module/Core/templates/core/error/error.html.twig @@ -1,4 +1,4 @@ -{% extends 'layout/default.html.twig' %} +{% extends 'core/layout/default.html.twig' %} {% block title %}{{ status }} {{ reason }}{% endblock %} diff --git a/templates/layout/default.html.twig b/module/Core/templates/core/layout/default.html.twig similarity index 100% rename from templates/layout/default.html.twig rename to module/Core/templates/core/layout/default.html.twig From 7ca52ecff9b9c2f685cb18d7a20e2f55f8c2e184 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Tue, 19 Jul 2016 18:33:45 +0200 Subject: [PATCH 13/13] Removed wrong use statement from old namespace --- module/CLI/config/services.config.php | 1 - module/Common/config/services.config.php | 1 - 2 files changed, 2 deletions(-) diff --git a/module/CLI/config/services.config.php b/module/CLI/config/services.config.php index 5c10f5a9..839e92f8 100644 --- a/module/CLI/config/services.config.php +++ b/module/CLI/config/services.config.php @@ -1,5 +1,4 @@