From 9db41a0949bdaa66185f1bdc9bd6cf89fb2cab51 Mon Sep 17 00:00:00 2001 From: Junaid Farooq Date: Wed, 13 Nov 2019 11:51:09 +0530 Subject: [PATCH 01/12] Added Symfony 4 compatibility and refactored code Replaced Symfony\Bundle\FrameworkBundle\Controller\Controller with Symfony\Bundle\FrameworkBundle\Controller\AbstractController Added proper return type and type hints Updated php doc-blocks --- Controller/EditInPlaceController.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Controller/EditInPlaceController.php b/Controller/EditInPlaceController.php index 60571c62..5e2a2c86 100644 --- a/Controller/EditInPlaceController.php +++ b/Controller/EditInPlaceController.php @@ -11,7 +11,7 @@ namespace Translation\Bundle\Controller; -use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Translation\Bundle\Exception\MessageValidationException; @@ -22,15 +22,16 @@ /** * @author Damien Alexandre */ -class EditInPlaceController extends Controller +class EditInPlaceController extends AbstractController { /** + * @param Request $request * @param string $configName * @param string $locale * * @return Response */ - public function editAction(Request $request, $configName, $locale) + public function editAction(Request $request, string $configName, string $locale): Response { try { $messages = $this->getMessages($request, $locale, ['Edit']); @@ -53,13 +54,15 @@ public function editAction(Request $request, $configName, $locale) /** * Get and validate messages from the request. * + * @param Request $request * @param string $locale + * @param array $validationGroups * * @return MessageInterface[] * * @throws MessageValidationException */ - private function getMessages(Request $request, $locale, array $validationGroups = []) + private function getMessages(Request $request, string $locale, array $validationGroups = []) { $json = $request->getContent(); $data = \json_decode($json, true); From 0b6c5b5924349270b32679860820ed6c1804acaf Mon Sep 17 00:00:00 2001 From: Junaid Date: Tue, 19 Nov 2019 23:15:21 +0530 Subject: [PATCH 02/12] + Replaced Controller with AbstractController in the controllers + Removed $this-get calls in the controller and injected appropriate dependencies via constructor + cs-fixes + Added proper type-hints and return types + Added ext-json in the composer.json file --- Controller/EditInPlaceController.php | 41 +++++--- Controller/SymfonyProfilerController.php | 86 +++++++--------- Controller/WebUIController.php | 124 ++++++++++++----------- composer.json | 3 +- 4 files changed, 130 insertions(+), 124 deletions(-) diff --git a/Controller/EditInPlaceController.php b/Controller/EditInPlaceController.php index 5e2a2c86..5aeea9c3 100644 --- a/Controller/EditInPlaceController.php +++ b/Controller/EditInPlaceController.php @@ -14,7 +14,10 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Validator\Validator\ValidatorInterface; use Translation\Bundle\Exception\MessageValidationException; +use Translation\Bundle\Service\CacheClearer; +use Translation\Bundle\Service\StorageManager; use Translation\Bundle\Service\StorageService; use Translation\Common\Model\Message; use Translation\Common\Model\MessageInterface; @@ -25,12 +28,27 @@ class EditInPlaceController extends AbstractController { /** - * @param Request $request - * @param string $configName - * @param string $locale - * - * @return Response + * @var StorageManager + */ + private $storageManager; + + /** + * @var CacheClearer + */ + private $cacheClearer; + + /** + * @var ValidatorInterface */ + private $validator; + + public function __construct(StorageManager $storageManager, CacheClearer $cacheClearer, ValidatorInterface $validator) + { + $this->storageManager = $storageManager; + $this->cacheClearer = $cacheClearer; + $this->validator = $validator; + } + public function editAction(Request $request, string $configName, string $locale): Response { try { @@ -40,13 +58,13 @@ public function editAction(Request $request, string $configName, string $locale) } /** @var StorageService $storage */ - $storage = $this->get('php_translation.storage_manager')->getStorage($configName); + $storage = $this->storageManager->getStorage($configName); + foreach ($messages as $message) { $storage->update($message); } - $cacheClearer = $this->get('php_translation.cache_clearer'); - $cacheClearer->clearAndWarmUp($locale); + $this->cacheClearer->clearAndWarmUp($locale); return new Response(); } @@ -54,10 +72,6 @@ public function editAction(Request $request, string $configName, string $locale) /** * Get and validate messages from the request. * - * @param Request $request - * @param string $locale - * @param array $validationGroups - * * @return MessageInterface[] * * @throws MessageValidationException @@ -67,14 +81,13 @@ private function getMessages(Request $request, string $locale, array $validation $json = $request->getContent(); $data = \json_decode($json, true); $messages = []; - $validator = $this->get('validator'); foreach ($data as $key => $value) { list($domain, $translationKey) = \explode('|', $key); $message = new Message($translationKey, $domain, $locale, $value); + $errors = $this->validator->validate($message, null, $validationGroups); - $errors = $validator->validate($message, null, $validationGroups); if (\count($errors) > 0) { throw MessageValidationException::create(); } diff --git a/Controller/SymfonyProfilerController.php b/Controller/SymfonyProfilerController.php index 6ccdde35..f340f39b 100644 --- a/Controller/SymfonyProfilerController.php +++ b/Controller/SymfonyProfilerController.php @@ -11,9 +11,10 @@ namespace Translation\Bundle\Controller; -use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Profiler\Profiler; use Symfony\Component\Translation\DataCollectorTranslator; use Symfony\Component\VarDumper\Cloner\Data; use Translation\Bundle\Model\SfProfilerMessage; @@ -23,14 +24,25 @@ /** * @author Tobias Nyholm */ -class SymfonyProfilerController extends Controller +class SymfonyProfilerController extends AbstractController { /** - * @param string $token - * - * @return Response + * @var StorageService + */ + private $storageService; + + /** + * @var Profiler */ - public function editAction(Request $request, $token) + private $profiler; + + public function __construct(StorageService $storageService, Profiler $profiler) + { + $this->storageService = $storageService; + $this->profiler = $profiler; + } + + public function editAction(Request $request, string $token): Response { if (!$this->getParameter('php_translation.toolbar.allow_edit')) { return new Response('You are not allowed to edit the translations.'); @@ -41,11 +53,9 @@ public function editAction(Request $request, $token) } $message = $this->getMessage($request, $token); - /** @var StorageService $storage */ - $storage = $this->get('php_translation.storage'); if ($request->isMethod('GET')) { - $translation = $storage->syncAndFetchMessage($message->getLocale(), $message->getDomain(), $message->getKey()); + $translation = $this->storageService->syncAndFetchMessage($message->getLocale(), $message->getDomain(), $message->getKey()); return $this->render('@Translation/SymfonyProfiler/edit.html.twig', [ 'message' => $translation, @@ -55,26 +65,19 @@ public function editAction(Request $request, $token) //Assert: This is a POST request $message->setTranslation($request->request->get('translation')); - $storage->update($message->convertToMessage()); + $this->storageService->update($message->convertToMessage()); return new Response($message->getTranslation()); } - /** - * @param string $token - * - * @return Response - */ - public function syncAction(Request $request, $token) + public function syncAction(Request $request, string $token): Response { if (!$request->isXmlHttpRequest()) { return $this->redirectToRoute('_profiler', ['token' => $token]); } - /** @var StorageService $storage */ - $storage = $this->get('php_translation.storage'); $sfMessage = $this->getMessage($request, $token); - $message = $storage->syncAndFetchMessage($sfMessage->getLocale(), $sfMessage->getDomain(), $sfMessage->getKey()); + $message = $this->storageService->syncAndFetchMessage($sfMessage->getLocale(), $sfMessage->getDomain(), $sfMessage->getKey()); if (null !== $message) { return new Response($message->getTranslation()); @@ -83,20 +86,13 @@ public function syncAction(Request $request, $token) return new Response('Asset not found', 404); } - /** - * @param $token - * - * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response - */ - public function syncAllAction(Request $request, $token) + public function syncAllAction(Request $request, string $token): Response { if (!$request->isXmlHttpRequest()) { return $this->redirectToRoute('_profiler', ['token' => $token]); } - /** @var StorageService $storage */ - $storage = $this->get('php_translation.storage'); - $storage->sync(); + $this->storageService->sync(); return new Response('Started synchronization of all translations'); } @@ -105,12 +101,8 @@ public function syncAllAction(Request $request, $token) * Save the selected translation to resources. * * @author Damien Alexandre (damienalexandre) - * - * @param string $token - * - * @return Response */ - public function createAssetsAction(Request $request, $token) + public function createAssetsAction(Request $request, string $token): Response { if (!$request->isXmlHttpRequest()) { return $this->redirectToRoute('_profiler', ['token' => $token]); @@ -122,29 +114,23 @@ public function createAssetsAction(Request $request, $token) } $uploaded = []; - /** @var StorageService $storage */ - $storage = $this->get('php_translation.storage'); + foreach ($messages as $message) { - $storage->create($message); + $this->storageService->create($message); $uploaded[] = $message; } return new Response(\sprintf('%s new assets created!', \count($uploaded))); } - /** - * @param string $token - * - * @return SfProfilerMessage - */ - private function getMessage(Request $request, $token) + private function getMessage(Request $request, string $token): SfProfilerMessage { - $profiler = $this->get('profiler'); - $profiler->disable(); + $this->profiler->disable(); $messageId = $request->request->get('message_id', $request->query->get('message_id')); - $profile = $profiler->loadProfile($token); + $profile = $this->profiler->loadProfile($token); + if (null === $dataCollector = $profile->getCollector('translation')) { throw $this->createNotFoundException('No collector with name "translation" was found.'); } @@ -158,6 +144,7 @@ private function getMessage(Request $request, $token) if (!isset($collectorMessages[$messageId])) { throw $this->createNotFoundException(\sprintf('No message with key "%s" was found.', $messageId)); } + $message = SfProfilerMessage::create($collectorMessages[$messageId]); if (DataCollectorTranslator::MESSAGE_EQUALS_FALLBACK === $message->getState()) { @@ -169,21 +156,18 @@ private function getMessage(Request $request, $token) } /** - * @param string $token - * * @return MessageInterface[] */ - protected function getSelectedMessages(Request $request, $token) + protected function getSelectedMessages(Request $request, string $token) { - $profiler = $this->get('profiler'); - $profiler->disable(); + $this->profiler->disable(); $selected = $request->request->get('selected'); if (!$selected || 0 == \count($selected)) { return []; } - $profile = $profiler->loadProfile($token); + $profile = $this->profiler->loadProfile($token); $dataCollector = $profile->getCollector('translation'); $messages = $dataCollector->getMessages(); diff --git a/Controller/WebUIController.php b/Controller/WebUIController.php index e8118e41..fd954f14 100644 --- a/Controller/WebUIController.php +++ b/Controller/WebUIController.php @@ -11,15 +11,20 @@ namespace Translation\Bundle\Controller; -use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Intl\Intl; use Symfony\Component\Intl\Locales; use Symfony\Component\Translation\MessageCatalogue; +use Symfony\Component\Validator\Validator\ValidatorInterface; +use Translation\Bundle\Catalogue\CatalogueFetcher; +use Translation\Bundle\Catalogue\CatalogueManager; use Translation\Bundle\Exception\MessageValidationException; use Translation\Bundle\Model\CatalogueMessage; +use Translation\Bundle\Service\ConfigurationManager; +use Translation\Bundle\Service\StorageManager; use Translation\Bundle\Service\StorageService; use Translation\Common\Exception\StorageException; use Translation\Common\Model\Message; @@ -28,25 +33,59 @@ /** * @author Tobias Nyholm */ -class WebUIController extends Controller +class WebUIController extends AbstractController { + /** + * @var StorageManager + */ + private $storageManager; + + /** + * @var ConfigurationManager + */ + private $configurationManager; + + /** + * @var CatalogueManager + */ + private $catalogueManager; + + /** + * @var CatalogueFetcher + */ + private $catalogueFetcher; + + /** + * @var ValidatorInterface + */ + private $validator; + + public function __construct( + StorageManager $storageManager, + ConfigurationManager $configurationManager, + CatalogueManager $catalogueManager, + CatalogueFetcher $catalogueFetcher, + ValidatorInterface $validator + ) { + $this->storageManager = $storageManager; + $this->configurationManager = $configurationManager; + $this->catalogueManager = $catalogueManager; + $this->catalogueFetcher = $catalogueFetcher; + $this->validator = $validator; + } + /** * Show a dashboard for the configuration. - * - * @param string|null $configName - * - * @return Response */ - public function indexAction($configName = null) + public function indexAction(string $configName = null): Response { if (!$this->getParameter('php_translation.webui.enabled')) { return new Response('You are not allowed here. Check you config. ', 400); } - $configManager = $this->get('php_translation.configuration_manager'); - $config = $configManager->getConfiguration($configName); + $config = $this->configurationManager->getConfiguration($configName); $localeMap = $this->getLocale2LanguageMap(); - $catalogues = $this->get('php_translation.catalogue_fetcher')->getCatalogues($config); + $catalogues = $this->catalogueFetcher->getCatalogues($config); $catalogueSize = []; $maxDomainSize = []; @@ -79,66 +118,52 @@ public function indexAction($configName = null) 'maxCatalogueSize' => $maxCatalogueSize, 'localeMap' => $localeMap, 'configName' => $config->getName(), - 'configNames' => $configManager->getNames(), + 'configNames' => $this->configurationManager->getNames(), ]); } /** * Show a catalogue. - * - * @param string $configName - * @param string $locale - * @param string $domain - * - * @return Response */ - public function showAction($configName, $locale, $domain) + public function showAction(string $configName, string $locale, string $domain): Response { if (!$this->getParameter('php_translation.webui.enabled')) { return new Response('You are not allowed here. Check you config. ', 400); } - $configManager = $this->get('php_translation.configuration_manager'); - $config = $configManager->getConfiguration($configName); + + $config = $this->configurationManager->getConfiguration($configName); // Get a catalogue manager and load it with all the catalogues - $catalogueManager = $this->get('php_translation.catalogue_manager'); - $catalogueManager->load($this->get('php_translation.catalogue_fetcher')->getCatalogues($config)); + $this->catalogueManager->load($this->catalogueFetcher->getCatalogues($config)); /** @var CatalogueMessage[] $messages */ - $messages = $catalogueManager->getMessages($locale, $domain); + $messages = $this->catalogueManager->getMessages($locale, $domain); \usort($messages, function (CatalogueMessage $a, CatalogueMessage $b) { return \strcmp($a->getKey(), $b->getKey()); }); return $this->render('@Translation/WebUI/show.html.twig', [ 'messages' => $messages, - 'domains' => $catalogueManager->getDomains(), + 'domains' => $this->catalogueManager->getDomains(), 'currentDomain' => $domain, 'locales' => $this->getParameter('php_translation.locales'), 'currentLocale' => $locale, 'configName' => $config->getName(), - 'configNames' => $configManager->getNames(), + 'configNames' => $this->configurationManager->getNames(), 'allow_create' => $this->getParameter('php_translation.webui.allow_create'), 'allow_delete' => $this->getParameter('php_translation.webui.allow_delete'), 'file_base_path' => $this->getParameter('php_translation.webui.file_base_path'), ]); } - /** - * @param string $configName - * @param string $locale - * @param string $domain - * - * @return Response - */ - public function createAction(Request $request, $configName, $locale, $domain) + public function createAction(Request $request, string $configName, string $locale, string $domain): Response { if (!$this->getParameter('php_translation.webui.enabled') || !$this->getParameter('php_translation.webui.allow_create')) { return new Response('You are not allowed to create. Check you config. ', 400); } /** @var StorageService $storage */ - $storage = $this->get('php_translation.storage_manager')->getStorage($configName); + $storage = $this->storageManager->getStorage($configName); try { $message = $this->getMessageFromRequest($request); @@ -160,14 +185,7 @@ public function createAction(Request $request, $configName, $locale, $domain) ]); } - /** - * @param string $configName - * @param string $locale - * @param string $domain - * - * @return Response - */ - public function editAction(Request $request, $configName, $locale, $domain) + public function editAction(Request $request, string $configName, string $locale, string $domain): Response { if (!$this->getParameter('php_translation.webui.enabled')) { return new Response('You are not allowed here. Check you config. ', 400); @@ -183,20 +201,13 @@ public function editAction(Request $request, $configName, $locale, $domain) } /** @var StorageService $storage */ - $storage = $this->get('php_translation.storage_manager')->getStorage($configName); + $storage = $this->storageManager->getStorage($configName); $storage->update($message); return new Response('Translation updated'); } - /** - * @param string $configName - * @param string $locale - * @param string $domain - * - * @return Response - */ - public function deleteAction(Request $request, $configName, $locale, $domain) + public function deleteAction(Request $request, string $configName, string $locale, string $domain): Response { if (!$this->getParameter('php_translation.webui.enabled') || !$this->getParameter('php_translation.webui.allow_delete')) { return new Response('You are not allowed to create. Check you config. ', 400); @@ -212,16 +223,13 @@ public function deleteAction(Request $request, $configName, $locale, $domain) } /** @var StorageService $storage */ - $storage = $this->get('php_translation.storage_manager')->getStorage($configName); + $storage = $this->storageManager->getStorage($configName); $storage->delete($locale, $domain, $message->getKey()); return new Response('Message was deleted'); } - /** - * @return MessageInterface - */ - private function getMessageFromRequest(Request $request) + private function getMessageFromRequest(Request $request): MessageInterface { $json = $request->getContent(); $data = \json_decode($json, true); @@ -238,7 +246,7 @@ private function getMessageFromRequest(Request $request) * * @return array locale => language */ - private function getLocale2LanguageMap() + private function getLocale2LanguageMap(): array { $configuredLocales = $this->getParameter('php_translation.locales'); $names = \class_exists(Locales::class) @@ -257,7 +265,7 @@ private function getLocale2LanguageMap() */ private function validateMessage(MessageInterface $message, array $validationGroups) { - $errors = $this->get('validator')->validate($message, null, $validationGroups); + $errors = $this->validator->validate($message, null, $validationGroups); if (\count($errors) > 0) { throw MessageValidationException::create(); } diff --git a/composer.json b/composer.json index 248ad2e1..1123d112 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ "php-translation/symfony-storage": "^1.0", "php-translation/extractor": "^1.6", "nyholm/nsa": "^1.1", - "twig/twig": "^1.42 || ^2.11" + "twig/twig": "^1.42 || ^2.11", + "ext-json": "*" }, "require-dev": { "symfony/phpunit-bridge": "^4.2", From 2223f92bd8c4ccf8788da9df992923a09784abc8 Mon Sep 17 00:00:00 2001 From: Junaid Date: Wed, 20 Nov 2019 15:49:58 +0530 Subject: [PATCH 03/12] + Registered controllers as services --- Resources/config/controller.yml | 9 +++++++++ Resources/config/services.yml | 3 +++ composer.json | 3 ++- 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 Resources/config/controller.yml diff --git a/Resources/config/controller.yml b/Resources/config/controller.yml new file mode 100644 index 00000000..1e1bb476 --- /dev/null +++ b/Resources/config/controller.yml @@ -0,0 +1,9 @@ +services: + _defaults: + autowire: true + autoconfigure: true + public: true + + Translation\Bundle\Controller\: + resource: '../../Controller' + tags: ['controller.service_arguments'] diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 6d374777..6121ead5 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -1,3 +1,6 @@ +imports: + - { resource: controller.yml } + services: php_translation.catalogue_fetcher: public: true diff --git a/composer.json b/composer.json index 1123d112..c21acfcb 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,8 @@ "matthiasnoback/symfony-dependency-injection-test": "^3.1", "matthiasnoback/symfony-config-test": "^4.0", "nyholm/psr7": "^1.1", - "nyholm/symfony-bundle-test": "^1.4" + "nyholm/symfony-bundle-test": "^1.4", + "phpunit/phpunit": "^8.4" }, "suggest": { "php-http/httplug-bundle": "To easier configure your httplug clients." From fa9c8d7c78ccf8247bc9cd51487f0c75663b79d7 Mon Sep 17 00:00:00 2001 From: Junaid Date: Wed, 20 Nov 2019 15:51:28 +0530 Subject: [PATCH 04/12] + Removed accidental commit --- composer.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/composer.json b/composer.json index c21acfcb..1123d112 100644 --- a/composer.json +++ b/composer.json @@ -40,8 +40,7 @@ "matthiasnoback/symfony-dependency-injection-test": "^3.1", "matthiasnoback/symfony-config-test": "^4.0", "nyholm/psr7": "^1.1", - "nyholm/symfony-bundle-test": "^1.4", - "phpunit/phpunit": "^8.4" + "nyholm/symfony-bundle-test": "^1.4" }, "suggest": { "php-http/httplug-bundle": "To easier configure your httplug clients." From f6cebf591146bb978adaf34b515b89197d3ef5a8 Mon Sep 17 00:00:00 2001 From: Junaid Date: Thu, 21 Nov 2019 00:53:41 +0530 Subject: [PATCH 05/12] + Now importing the new service file `controllers.yml` in TranslationExtension + Renamed controller.yml to controllers.yml + Removed a few non-existent class usages --- Catalogue/CatalogueFetcher.php | 3 +-- DependencyInjection/TranslationExtension.php | 11 ++++------- Resources/config/controller.yml | 9 --------- Resources/config/controllers.yml | 18 ++++++++++++++++++ Resources/config/services.yml | 3 --- 5 files changed, 23 insertions(+), 21 deletions(-) delete mode 100644 Resources/config/controller.yml create mode 100644 Resources/config/controllers.yml diff --git a/Catalogue/CatalogueFetcher.php b/Catalogue/CatalogueFetcher.php index 47f892fb..98a32f9d 100644 --- a/Catalogue/CatalogueFetcher.php +++ b/Catalogue/CatalogueFetcher.php @@ -12,7 +12,6 @@ namespace Translation\Bundle\Catalogue; use Nyholm\NSA; -use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader as SymfonyTranslationLoader; use Symfony\Component\Translation\MessageCatalogue; use Symfony\Component\Translation\Reader\TranslationReaderInterface; use Translation\Bundle\Model\Configuration; @@ -35,7 +34,7 @@ final class CatalogueFetcher private $reader; /** - * @param SymfonyTranslationLoader|TranslationLoader|TranslationReaderInterface $reader + * @param TranslationLoader|TranslationReaderInterface $reader */ public function __construct($reader) { diff --git a/DependencyInjection/TranslationExtension.php b/DependencyInjection/TranslationExtension.php index ef49d209..e1a0936c 100644 --- a/DependencyInjection/TranslationExtension.php +++ b/DependencyInjection/TranslationExtension.php @@ -15,7 +15,6 @@ use Symfony\Component\DependencyInjection\Alias; use Symfony\Component\DependencyInjection\ChildDefinition; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\DefinitionDecorator; use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; use Symfony\Component\DependencyInjection\Loader; use Symfony\Component\DependencyInjection\Reference; @@ -32,6 +31,7 @@ class TranslationExtension extends Extension { /** * {@inheritdoc} + * @throws \Exception */ public function load(array $configs, ContainerBuilder $container) { @@ -41,6 +41,7 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('services.yml'); $loader->load('extractors.yml'); + $loader->load('controllers.yml'); // Add major version to extractor $container->getDefinition('php_translation.extractor.php.visitor.FormTypeChoices') @@ -227,15 +228,11 @@ public function getAlias() * * @param $parent * - * @return ChildDefinition|DefinitionDecorator + * @return ChildDefinition */ private function createChildDefinition($parent) { - if (\class_exists('Symfony\Component\DependencyInjection\ChildDefinition')) { - return new ChildDefinition($parent); - } - - return new DefinitionDecorator($parent); + return new ChildDefinition($parent); } /** diff --git a/Resources/config/controller.yml b/Resources/config/controller.yml deleted file mode 100644 index 1e1bb476..00000000 --- a/Resources/config/controller.yml +++ /dev/null @@ -1,9 +0,0 @@ -services: - _defaults: - autowire: true - autoconfigure: true - public: true - - Translation\Bundle\Controller\: - resource: '../../Controller' - tags: ['controller.service_arguments'] diff --git a/Resources/config/controllers.yml b/Resources/config/controllers.yml new file mode 100644 index 00000000..5a4c99aa --- /dev/null +++ b/Resources/config/controllers.yml @@ -0,0 +1,18 @@ +services: + Translation\Bundle\Controller\EditInPlaceController: + public: true + tags: ['controller.service_arguments'] + arguments: + ['@php_translation.storage_manager', '@php_translation.cache_clearer', '@validator'] + + Translation\Bundle\Controller\SymfonyProfilerController: + public: true + tags: ['controller.service_arguments'] + arguments: + ['@php_translation.storage.abstract', '@profiler'] + + Translation\Bundle\Controller\WebUIController: + public: true + tags: ['controller.service_arguments'] + arguments: + ['@php_translation.storage_manager', '@php_translation.configuration_manager', '@php_translation.catalogue_manager', '@php_translation.catalogue_fetcher', '@validator'] diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 6121ead5..6d374777 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -1,6 +1,3 @@ -imports: - - { resource: controller.yml } - services: php_translation.catalogue_fetcher: public: true From 5a1dfcf02305d15b508028d8e0c1f93566af3369 Mon Sep 17 00:00:00 2001 From: Junaid Date: Thu, 21 Nov 2019 01:06:08 +0530 Subject: [PATCH 06/12] + CS-fix --- Catalogue/CatalogueFetcher.php | 7 +------ DependencyInjection/TranslationExtension.php | 15 +++++---------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/Catalogue/CatalogueFetcher.php b/Catalogue/CatalogueFetcher.php index 98a32f9d..6ed697cb 100644 --- a/Catalogue/CatalogueFetcher.php +++ b/Catalogue/CatalogueFetcher.php @@ -79,12 +79,7 @@ public function getCatalogues(Configuration $config, array $locales = []) return $catalogues; } - /** - * @param string $domain - * - * @return bool - */ - private function isValidDomain(Configuration $config, $domain) + private function isValidDomain(Configuration $config, string $domain): bool { $blacklist = $config->getBlacklistDomains(); $whitelist = $config->getWhitelistDomains(); diff --git a/DependencyInjection/TranslationExtension.php b/DependencyInjection/TranslationExtension.php index e1a0936c..5b500724 100644 --- a/DependencyInjection/TranslationExtension.php +++ b/DependencyInjection/TranslationExtension.php @@ -11,6 +11,7 @@ namespace Translation\Bundle\DependencyInjection; +use Symfony\Component\Config\Definition\ConfigurationInterface; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\Alias; use Symfony\Component\DependencyInjection\ChildDefinition; @@ -31,6 +32,7 @@ class TranslationExtension extends Extension { /** * {@inheritdoc} + * * @throws \Exception */ public function load(array $configs, ContainerBuilder $container) @@ -218,19 +220,12 @@ private function enableFallbackAutoTranslator(ContainerBuilder $container, array /** * {@inheritdoc} */ - public function getAlias() + public function getAlias(): string { return 'translation'; } - /** - * To avoid BC break for Symfony 3.3+. - * - * @param $parent - * - * @return ChildDefinition - */ - private function createChildDefinition($parent) + private function createChildDefinition(string $parent): ChildDefinition { return new ChildDefinition($parent); } @@ -238,7 +233,7 @@ private function createChildDefinition($parent) /** * {@inheritdoc} */ - public function getConfiguration(array $config, ContainerBuilder $container) + public function getConfiguration(array $config, ContainerBuilder $container): ConfigurationInterface { return new Configuration($container); } From a48fb015029091f8bff4f33ec17813f930544c89 Mon Sep 17 00:00:00 2001 From: Romain Monteil Date: Wed, 30 Oct 2019 15:45:20 +0100 Subject: [PATCH 07/12] Add I/O type hinting, use null coalescing operator --- Command/ExtractCommand.php | 1 + Command/StatusCommand.php | 1 + Model/CatalogueMessage.php | 2 +- Service/Importer.php | 1 + Service/StorageService.php | 2 ++ Translator/EditInPlaceTranslator.php | 4 ++-- 6 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Command/ExtractCommand.php b/Command/ExtractCommand.php index a34ca551..9f00569d 100644 --- a/Command/ExtractCommand.php +++ b/Command/ExtractCommand.php @@ -91,6 +91,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { $configName = $input->getArgument('configuration'); + if (null === $config = $this->configurationManager->getConfiguration($configName)) { throw new \InvalidArgumentException(\sprintf('No configuration found for "%s"', $configName)); } diff --git a/Command/StatusCommand.php b/Command/StatusCommand.php index db3b37ad..bbeb20b5 100644 --- a/Command/StatusCommand.php +++ b/Command/StatusCommand.php @@ -72,6 +72,7 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { $configName = $input->getArgument('configuration'); + if (null === $config = $this->configurationManager->getConfiguration($configName)) { throw new \InvalidArgumentException(\sprintf('No configuration found for "%s"', $configName)); } diff --git a/Model/CatalogueMessage.php b/Model/CatalogueMessage.php index e0caf5c2..dd081ee1 100644 --- a/Model/CatalogueMessage.php +++ b/Model/CatalogueMessage.php @@ -59,7 +59,7 @@ public function __construct(CatalogueManager $catalogueManager, string $locale, $this->message = $message; } - public function setMetadata(Metadata $metadata): void + public function setMetadata(?Metadata $metadata): void { $this->metadata = $metadata; } diff --git a/Service/Importer.php b/Service/Importer.php index 7d83057e..8a939054 100644 --- a/Service/Importer.php +++ b/Service/Importer.php @@ -58,6 +58,7 @@ public function __construct(Extractor $extractor, Environment $twig, string $def } /** + * @param Finder $finder * @param MessageCatalogue[] $catalogues * @param array $config { * diff --git a/Service/StorageService.php b/Service/StorageService.php index 5453664a..f139ba14 100644 --- a/Service/StorageService.php +++ b/Service/StorageService.php @@ -80,6 +80,8 @@ public function download(): void /** * Synchronize translations with remote. + * + * @param string $direction */ public function sync(string $direction = self::DIRECTION_DOWN): void { diff --git a/Translator/EditInPlaceTranslator.php b/Translator/EditInPlaceTranslator.php index 62cd8467..4d543242 100644 --- a/Translator/EditInPlaceTranslator.php +++ b/Translator/EditInPlaceTranslator.php @@ -30,12 +30,12 @@ final class EditInPlaceTranslator implements TranslatorInterface, TranslatorBagI private $translator; /** - * @var ActivatorInterface + * @var \Translation\Bundle\EditInPlace\ActivatorInterface */ private $activator; /** - * @var RequestStack + * @var \Symfony\Component\HttpFoundation\RequestStack */ private $requestStack; From b2597def673f3106a0b6000227800276558fc6a0 Mon Sep 17 00:00:00 2001 From: Richard van Laak Date: Tue, 5 Nov 2019 11:24:00 +0100 Subject: [PATCH 08/12] documentation fixes for phpstan lvl 2 --- DependencyInjection/CompilerPass/ExtractorPass.php | 4 ++++ Translator/EditInPlaceTranslator.php | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/DependencyInjection/CompilerPass/ExtractorPass.php b/DependencyInjection/CompilerPass/ExtractorPass.php index 030f0f3f..1be67ad2 100644 --- a/DependencyInjection/CompilerPass/ExtractorPass.php +++ b/DependencyInjection/CompilerPass/ExtractorPass.php @@ -54,6 +54,10 @@ private function getExtractors(ContainerBuilder $container): array return $extractors; } + /** + * @param ContainerBuilder $container + * @param Definition[] $extractors + */ private function addVisitors(ContainerBuilder $container, array $extractors): void { $services = $container->findTaggedServiceIds('php_translation.visitor'); diff --git a/Translator/EditInPlaceTranslator.php b/Translator/EditInPlaceTranslator.php index 4d543242..62cd8467 100644 --- a/Translator/EditInPlaceTranslator.php +++ b/Translator/EditInPlaceTranslator.php @@ -30,12 +30,12 @@ final class EditInPlaceTranslator implements TranslatorInterface, TranslatorBagI private $translator; /** - * @var \Translation\Bundle\EditInPlace\ActivatorInterface + * @var ActivatorInterface */ private $activator; /** - * @var \Symfony\Component\HttpFoundation\RequestStack + * @var RequestStack */ private $requestStack; From 8395971471353e6e89c205eaa9b1f484a1d51252 Mon Sep 17 00:00:00 2001 From: Romain Monteil Date: Fri, 8 Nov 2019 10:38:42 +0100 Subject: [PATCH 09/12] Throw Exception if no configuration found, update I/O type hinting --- Command/ExtractCommand.php | 1 - Command/StatusCommand.php | 1 - Controller/WebUIController.php | 7 ++++++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/Command/ExtractCommand.php b/Command/ExtractCommand.php index 9f00569d..a34ca551 100644 --- a/Command/ExtractCommand.php +++ b/Command/ExtractCommand.php @@ -91,7 +91,6 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { $configName = $input->getArgument('configuration'); - if (null === $config = $this->configurationManager->getConfiguration($configName)) { throw new \InvalidArgumentException(\sprintf('No configuration found for "%s"', $configName)); } diff --git a/Command/StatusCommand.php b/Command/StatusCommand.php index bbeb20b5..db3b37ad 100644 --- a/Command/StatusCommand.php +++ b/Command/StatusCommand.php @@ -72,7 +72,6 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { $configName = $input->getArgument('configuration'); - if (null === $config = $this->configurationManager->getConfiguration($configName)) { throw new \InvalidArgumentException(\sprintf('No configuration found for "%s"', $configName)); } diff --git a/Controller/WebUIController.php b/Controller/WebUIController.php index 6ba13554..280e20f9 100644 --- a/Controller/WebUIController.php +++ b/Controller/WebUIController.php @@ -177,7 +177,12 @@ public function createAction(Request $request, string $configName, string $local try { $storage->create($message); } catch (StorageException $e) { - throw new BadRequestHttpException(\sprintf('Key "%s" does already exist for "%s" on domain "%s".', $message->getKey(), $locale, $domain), $e); + throw new BadRequestHttpException(\sprintf( + 'Key "%s" does already exist for "%s" on domain "%s".', + $message->getKey(), + $locale, + $domain + ), $e); } return $this->render('@Translation/WebUI/create.html.twig', [ From 89d1df33f6f82b10fb2fc66cf091db0c870c8f73 Mon Sep 17 00:00:00 2001 From: Romain Monteil Date: Fri, 8 Nov 2019 11:15:17 +0100 Subject: [PATCH 10/12] Fix CS --- Service/StorageService.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/Service/StorageService.php b/Service/StorageService.php index f139ba14..5453664a 100644 --- a/Service/StorageService.php +++ b/Service/StorageService.php @@ -80,8 +80,6 @@ public function download(): void /** * Synchronize translations with remote. - * - * @param string $direction */ public function sync(string $direction = self::DIRECTION_DOWN): void { From 74234e3826f9ac6f2901682d22736f314b60dab3 Mon Sep 17 00:00:00 2001 From: Junaid Date: Thu, 21 Nov 2019 01:06:08 +0530 Subject: [PATCH 11/12] + CS-fix --- DependencyInjection/TranslationExtension.php | 1 - 1 file changed, 1 deletion(-) diff --git a/DependencyInjection/TranslationExtension.php b/DependencyInjection/TranslationExtension.php index 5decb364..125dbd93 100644 --- a/DependencyInjection/TranslationExtension.php +++ b/DependencyInjection/TranslationExtension.php @@ -226,7 +226,6 @@ public function getAlias(): string return 'translation'; } - private function createChildDefinition(string $parent): ChildDefinition { return new ChildDefinition($parent); From e61d7994983d67f4843ef91002b06f0c7ca2786a Mon Sep 17 00:00:00 2001 From: Junaid Date: Thu, 28 Nov 2019 11:27:26 +0530 Subject: [PATCH 12/12] + CS-fix + Minor code refactor --- Catalogue/CatalogueManager.php | 17 +++++++++-------- Controller/WebUIController.php | 7 +------ .../CompilerPass/ExtractorPass.php | 1 - Service/Importer.php | 9 ++++----- 4 files changed, 14 insertions(+), 20 deletions(-) diff --git a/Catalogue/CatalogueManager.php b/Catalogue/CatalogueManager.php index 8c9a3135..c0177dd7 100644 --- a/Catalogue/CatalogueManager.php +++ b/Catalogue/CatalogueManager.php @@ -17,7 +17,7 @@ use Translation\Bundle\Model\Metadata; /** - * A manager that handle loaded catalogues. + * A manager that handles loaded catalogues. * * @author Tobias Nyholm */ @@ -123,11 +123,7 @@ public function findMessages(array $config = []): array return $messages; } - /** - * @param string $domain - * @param string $key - */ - public function getTranslations($domain, $key): array + public function getTranslations(string $domain, string $key): array { $translations = []; foreach ($this->catalogues as $locale => $catalogue) { @@ -139,8 +135,13 @@ public function getTranslations($domain, $key): array return $translations; } - private function createMessage(MessageCatalogueInterface $catalogue, string $locale, string $domain, string $key, string $text): CatalogueMessage - { + private function createMessage( + MessageCatalogueInterface $catalogue, + string $locale, + string $domain, + string $key, + string $text + ): CatalogueMessage { $catalogueMessage = new CatalogueMessage($this, $locale, $domain, $key, $text); if ($catalogue instanceof MetadataAwareInterface) { diff --git a/Controller/WebUIController.php b/Controller/WebUIController.php index 280e20f9..6ba13554 100644 --- a/Controller/WebUIController.php +++ b/Controller/WebUIController.php @@ -177,12 +177,7 @@ public function createAction(Request $request, string $configName, string $local try { $storage->create($message); } catch (StorageException $e) { - throw new BadRequestHttpException(\sprintf( - 'Key "%s" does already exist for "%s" on domain "%s".', - $message->getKey(), - $locale, - $domain - ), $e); + throw new BadRequestHttpException(\sprintf('Key "%s" does already exist for "%s" on domain "%s".', $message->getKey(), $locale, $domain), $e); } return $this->render('@Translation/WebUI/create.html.twig', [ diff --git a/DependencyInjection/CompilerPass/ExtractorPass.php b/DependencyInjection/CompilerPass/ExtractorPass.php index 1be67ad2..d131fc49 100644 --- a/DependencyInjection/CompilerPass/ExtractorPass.php +++ b/DependencyInjection/CompilerPass/ExtractorPass.php @@ -55,7 +55,6 @@ private function getExtractors(ContainerBuilder $container): array } /** - * @param ContainerBuilder $container * @param Definition[] $extractors */ private function addVisitors(ContainerBuilder $container, array $extractors): void diff --git a/Service/Importer.php b/Service/Importer.php index 8a939054..d47e3f2a 100644 --- a/Service/Importer.php +++ b/Service/Importer.php @@ -58,14 +58,13 @@ public function __construct(Extractor $extractor, Environment $twig, string $def } /** - * @param Finder $finder * @param MessageCatalogue[] $catalogues * @param array $config { * - * @var array $blacklist_domains Blacklist the domains we should exclude. Cannot be used with whitelist. - * @var array $whitelist_domains Whitelist the domains we should include. Cannot be used with blacklist. - * @var string $project_root The project root will be removed from the source location. - * } + * @var array $whitelist_domains Whitelist the domains we should include. Cannot be used with blacklist. + * @var string $project_root The project root will be removed from the source location. + * } + * @var array $blacklist_domains Blacklist the domains we should exclude. Cannot be used with whitelist. */ public function extractToCatalogues(Finder $finder, array $catalogues, array $config = []): ImportResult {