From 2ccb1b412aabebd8eafdb71fd07a11e607f29f3f Mon Sep 17 00:00:00 2001 From: Bastien DURAND Date: Mon, 29 Oct 2018 10:48:27 +0100 Subject: [PATCH] Feat update to sf4 (#21) Update bundle code and tests to work with php > 7.1. min sf versionis now 4.0 --- .scrutinizer.yml | 2 +- .travis.yml | 54 +++++++++++++++---- DependencyInjection/Configuration.php | 4 +- .../DeamonLoggerExtraExtension.php | 6 +-- .../Monolog/DeamonLoggerExtraWebProcessor.php | 31 +++++------ README.md | 47 ++++++++-------- Resources/config/processors.xml | 15 ++---- Resources/config/services.xml | 8 +-- Services/DeamonLoggerExtraContext.php | 12 +++-- .../DeamonLoggerExtraExtensionTest.php | 5 +- .../DeamonLoggerExtraWebProcessorTest.php | 1 + composer.json | 21 ++++---- phpunit.xml.dist | 1 + 13 files changed, 121 insertions(+), 86 deletions(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index d40a81e..dd4918c 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -5,7 +5,7 @@ build: tests: override: - - command: 'vendor/bin/phpunit --coverage-clover=clover-coverage.xml' + command: 'vendor/bin/simple-phpunit --coverage-clover=clover-coverage.xml' coverage: file: 'clover-coverage.xml' format: 'clover' diff --git a/.travis.yml b/.travis.yml index 19d99aa..8d01196 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,18 +1,50 @@ language: php - sudo: false +cache: + directories: + - $HOME/.composer/cache/files + - $HOME/symfony-bridge/.phpunit + +env: + global: + - PHPUNIT_FLAGS="-v" + - SYMFONY_PHPUNIT_DIR="$HOME/symfony-bridge/.phpunit" matrix: - include: - - php: 5.6 - env: deps=low - - php: 5.6 - - php: 7 - - php: 7.1 fast_finish: true + include: + # Minimum supported dependencies with the latest and oldest PHP version + - php: 7.2 + env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak_vendors" + - php: 7.1 + env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" SYMFONY_DEPRECATIONS_HELPER="weak_vendors" + + # Test the latest stable release + - php: 7.1 + - php: 7.2 + env: COVERAGE=true PHPUNIT_FLAGS="-v --coverage-text" + + # Latest commit to master + - php: 7.2 + env: STABILITY="dev" + + allow_failures: + # Dev-master is allowed to fail. + - env: STABILITY="dev" + +before_install: +- if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi +- if ! [ -z "$STABILITY" ]; then composer config minimum-stability ${STABILITY}; fi; +- if ! [ -v "$DEPENDENCIES" ]; then composer require --no-update ${DEPENDENCIES}; fi; -before_script: - - if [ "$deps" == "low" ]; then composer update --prefer-source --prefer-lowest --prefer-stable; fi - - if [ "$deps" != "low" ]; then composer install --prefer-source; fi +install: +# To be removed when this issue will be resolved: https://github.com/composer/composer/issues/5355 +- if [[ "$COMPOSER_FLAGS" == *"--prefer-lowest"* ]]; then composer update --prefer-dist --no-interaction --prefer-stable --quiet; fi +- composer update ${COMPOSER_FLAGS} --prefer-dist --no-interaction +- ./vendor/bin/simple-phpunit install -script: ./vendor/bin/phpunit +script: +- composer validate --strict --no-check-lock +# simple-phpunit is the PHPUnit wrapper provided by the PHPUnit Bridge component and +# it helps with testing legacy code and deprecations (composer require symfony/phpunit-bridge) +- ./vendor/bin/simple-phpunit $PHPUNIT_FLAGS diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 708f3b2..0be9aa0 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -15,7 +15,7 @@ class Configuration implements ConfigurationInterface /** * {@inheritdoc} */ - public function getConfigTreeBuilder() + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder(); $rootNode = $treeBuilder->root('deamon_logger_extra'); @@ -39,7 +39,7 @@ public function getConfigTreeBuilder() ->arrayNode('config')->isRequired()->addDefaultsIfNotSet() ->children() ->scalarNode('channel_prefix')->defaultNull()->end() - ->scalarNode('user_class')->defaultValue('\Symfony\Component\Security\Core\User\UserInterface')->end() + ->scalarNode('user_class')->defaultValue(null)->end() ->arrayNode('user_methods') ->useAttributeAsKey('value') ->normalizeKeys(false) diff --git a/DependencyInjection/DeamonLoggerExtraExtension.php b/DependencyInjection/DeamonLoggerExtraExtension.php index d6d05e3..fabdf80 100644 --- a/DependencyInjection/DeamonLoggerExtraExtension.php +++ b/DependencyInjection/DeamonLoggerExtraExtension.php @@ -17,7 +17,7 @@ class DeamonLoggerExtraExtension extends Extension /** * {@inheritdoc} */ - public function load(array $configs, ContainerBuilder $container) + public function load(array $configs, ContainerBuilder $container): void { $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); @@ -27,11 +27,11 @@ public function load(array $configs, ContainerBuilder $container) $loader->load('processors.xml'); $definition = $container->getDefinition('deamon.logger_extra.context'); - $definition->replaceArgument(0, $config['application']['name']); + $definition->addArgument($config['application']['name']); $definition->addArgument($config['application']['locale']); $definition = $container->getDefinition('deamon.logger_extra.processors.web_processor'); - $definition->replaceArgument(0, $config['config']); + $definition->addArgument($config['config']); $definition->clearTag('monolog.processor'); foreach ($config['handlers'] as $handler) { diff --git a/Processors/Monolog/DeamonLoggerExtraWebProcessor.php b/Processors/Monolog/DeamonLoggerExtraWebProcessor.php index 932354d..ad303bc 100644 --- a/Processors/Monolog/DeamonLoggerExtraWebProcessor.php +++ b/Processors/Monolog/DeamonLoggerExtraWebProcessor.php @@ -9,6 +9,7 @@ use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; +use Symfony\Component\Security\Core\User\UserInterface; class DeamonLoggerExtraWebProcessor extends BaseWebProcessor { @@ -53,7 +54,7 @@ class DeamonLoggerExtraWebProcessor extends BaseWebProcessor */ private $record; - public function __construct(array $config = null) + public function __construct(?array $config = null) { parent::__construct(); $this->channelPrefix = $config['channel_prefix']; @@ -82,7 +83,7 @@ public function __invoke(array $record) /** * Add extra info about the context of the generated log. */ - private function addContextInfo() + private function addContextInfo(): void { if (null !== $this->environment) { $this->addInfo('env', $this->environment); @@ -99,7 +100,7 @@ private function addContextInfo() /** * Add extra info about the request generating the log. */ - private function addRequestInfo() + private function addRequestInfo(): void { if (null !== $this->requestStack) { $request = $this->requestStack->getCurrentRequest(); @@ -116,7 +117,7 @@ private function addRequestInfo() /** * Add extra info on the user generating the log. */ - private function addUserInfo() + private function addUserInfo(): void { if (!$this->configShowExtraInfo('user')) { return; @@ -139,9 +140,9 @@ private function addUserInfo() /** * append method result of user object. * - * @param $user + * @param UserInterface $user */ - private function appendUserMethodInfo($user) + private function appendUserMethodInfo(UserInterface $user): void { foreach ($this->userMethods as $name => $method) { if (method_exists($user, $method)) { @@ -153,11 +154,11 @@ private function appendUserMethodInfo($user) /** * Check if passed token is an instance of TokenInterface and an instance of config UserClass. * - * @param $token + * @param TokenInterface|null $token * * @return bool */ - private function isUserInstanceValid($token) + private function isUserInstanceValid(?TokenInterface $token): bool { return $token instanceof TokenInterface && $token->getUser() instanceof $this->userClass; } @@ -165,7 +166,7 @@ private function isUserInstanceValid($token) /** * Add channel info to ease the log interpretation. */ - private function addChannelInfo() + private function addChannelInfo(): void { $this->addInfo('global_channel', $this->record['channel']); @@ -180,7 +181,7 @@ private function addChannelInfo() * @param string $key * @param mixed $value */ - private function addInfo($key, $value) + private function addInfo(string $key, $value): void { if ($this->configShowExtraInfo($key) && $value !== null) { $this->record['extra'][$key] = $value; @@ -194,7 +195,7 @@ private function addInfo($key, $value) * * @return bool */ - private function configShowExtraInfo($extraInfo) + private function configShowExtraInfo(string $extraInfo): bool { return isset($this->displayConfig[$extraInfo]) && $this->displayConfig[$extraInfo]; } @@ -202,7 +203,7 @@ private function configShowExtraInfo($extraInfo) /** * @param DeamonLoggerExtraContext $loggerExtraContext */ - public function setLoggerExtraContext(DeamonLoggerExtraContext $loggerExtraContext) + public function setLoggerExtraContext(DeamonLoggerExtraContext $loggerExtraContext): void { $this->loggerExtraContext = $loggerExtraContext; } @@ -210,7 +211,7 @@ public function setLoggerExtraContext(DeamonLoggerExtraContext $loggerExtraConte /** * @param string $environment */ - public function setEnvironment($environment) + public function setEnvironment(string $environment): void { $this->environment = $environment; } @@ -218,7 +219,7 @@ public function setEnvironment($environment) /** * @param TokenStorageInterface $tokenStorage */ - public function setTokenStorage(TokenStorageInterface $tokenStorage) + public function setTokenStorage(TokenStorageInterface $tokenStorage): void { $this->tokenStorage = $tokenStorage; } @@ -226,7 +227,7 @@ public function setTokenStorage(TokenStorageInterface $tokenStorage) /** * @param RequestStack $requestStack */ - public function setRequestStack(RequestStack $requestStack) + public function setRequestStack(RequestStack $requestStack): void { $this->requestStack = $requestStack; } diff --git a/README.md b/README.md index d69ad69..76cdfcc 100644 --- a/README.md +++ b/README.md @@ -4,8 +4,8 @@ DeamonLoggerExtra Bundle [![Build Status](https://travis-ci.org/FrDeamon/logger-extra-bundle.svg?branch=master&style=flat)](https://travis-ci.org/FrDeamon/logger-extra-bundle) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/FrDeamon/logger-extra-bundle/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/FrDeamon/logger-extra-bundle/?branch=master) [![Code Coverage](https://scrutinizer-ci.com/g/FrDeamon/logger-extra-bundle/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/FrDeamon/logger-extra-bundle/?branch=master) -![symfony version](https://img.shields.io/badge/symfony->=2.7,%20>=3.0-blue.svg) -![php version](https://img.shields.io/badge/php->=5.6.0,%20>=7-blue.svg) +![symfony version](https://img.shields.io/badge/symfony->=4.0-blue.svg) +![php version](https://img.shields.io/badge/php->=7.1-blue.svg) [![SensioLabsInsight](https://insight.sensiolabs.com/projects/5a913c84-a190-40f7-9e46-3c2052692fcd/big.png)](https://insight.sensiolabs.com/projects/5a913c84-a190-40f7-9e46-3c2052692fcd) @@ -37,14 +37,19 @@ Installation ---------------- You need to add a package to your dependency list : - +``` // composer.json - "deamon/logger-extra-bundle": "^2.0" + "deamon/logger-extra-bundle": "^4.0" +``` Then enable the bundle into your kernel - - // app/AppKernel.php - new Deamon\LoggerExtraBundle\DeamonLoggerExtraBundle(), +``` + // config/bundles.php + return [ + // ... + App\Acme\TestBundle\AcmeTestBundle::class => ['all' => true], + ]; +``` Finally you need to configure the bundle. @@ -57,18 +62,11 @@ Given this config sample of a project: // app/config/config.yml monolog: handlers: - default_info: - type: gelf - publisher: - hostname: "%graylog_host%" - level: INFO - channels: [!request, !security, !app, !monitoring, !deprecation, !php] - default_notice: - type: gelf - publisher: - hostname: "%graylog_host%" - level: NOTICE - channels: [request, security, app, php] + main: + type: stream + path: "%kernel.logs_dir%/%kernel.environment%.log" + level: debug + channels: ["!event"] ``` With this example of monolog config, you can configure this bundle to only add extra info on `default_info` handler. @@ -78,7 +76,7 @@ With this example of monolog config, you can configure this bundle to only add e deamon_logger_extra: application: name: "loc-deamonfront" - handlers: [default_info] + handlers: [main] config: channel_prefix: "v0.1" ``` @@ -86,15 +84,15 @@ deamon_logger_extra: ## Config reference ``` -// app/config/config.yml +// config/packages/deamon_logger_extra.yaml deamon_logger_extra: application: name: "loc-deamonfront" # default to null locale: "fr" # default to null - handlers: [default_info] # the only required field + handlers: [main] # the only required field config: channel_prefix: "v0.1" # default to null - user_class: "\Symfony\Component\Security\Core\User\UserInterface" # default value + user_class: "\\Symfony\\\Component\\Security\\Core\\User\\UserInterface" # default to null user_methods: user_name: getUsername # default value display: @@ -112,8 +110,9 @@ deamon_logger_extra: ## Minimal configuration ``` +// config/packages/deamon_logger_extra.yaml deamon_logger_extra: application: ~ - handlers: 'default_info' + handlers: 'main' config: ~ ``` diff --git a/Resources/config/processors.xml b/Resources/config/processors.xml index 1af1517..f9585b9 100644 --- a/Resources/config/processors.xml +++ b/Resources/config/processors.xml @@ -4,18 +4,13 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - - Deamon\LoggerExtraBundle\Processors\Monolog\DeamonLoggerExtraWebProcessor - - - - - + - %kernel.environment% - - + %kernel.environment% + + + diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 4df1903..1edffb9 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -4,12 +4,8 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - - Deamon\LoggerExtraBundle\Services\DeamonLoggerExtraContext - - - - + + diff --git a/Services/DeamonLoggerExtraContext.php b/Services/DeamonLoggerExtraContext.php index 564f127..894047b 100644 --- a/Services/DeamonLoggerExtraContext.php +++ b/Services/DeamonLoggerExtraContext.php @@ -4,21 +4,27 @@ class DeamonLoggerExtraContext { + /** + * @var string|null + */ private $locale; + /** + * @var string|null + */ private $applicationName; - public function __construct($applicationName, $locale = null) + public function __construct(string $applicationName, ?string $locale = null) { $this->applicationName = $applicationName; $this->locale = $locale; } - public function getLocale() + public function getLocale(): ?string { return $this->locale; } - public function getApplicationName() + public function getApplicationName(): ?string { return $this->applicationName; } diff --git a/Tests/DependencyInjection/DeamonLoggerExtraExtensionTest.php b/Tests/DependencyInjection/DeamonLoggerExtraExtensionTest.php index d164dc7..b06761f 100644 --- a/Tests/DependencyInjection/DeamonLoggerExtraExtensionTest.php +++ b/Tests/DependencyInjection/DeamonLoggerExtraExtensionTest.php @@ -3,9 +3,10 @@ namespace DependencyInjection; use Deamon\LoggerExtraBundle\DependencyInjection\DeamonLoggerExtraExtension; +use PHPUnit\Framework\TestCase; use Symfony\Component\DependencyInjection\ContainerBuilder; -class DeamonLoggerExtraExtensionTest extends \PHPUnit_Framework_TestCase +class DeamonLoggerExtraExtensionTest extends TestCase { /** * @var DeamonLoggerExtraExtension @@ -63,7 +64,7 @@ public function testDefaultValue() $defaultConfigValues = [ 'channel_prefix' => null, - 'user_class' => '\Symfony\Component\Security\Core\User\UserInterface', + 'user_class' => null, 'user_methods' => [ 'user_name' => 'getUsername', ], diff --git a/Tests/Processors/Monolog/DeamonLoggerExtraWebProcessorTest.php b/Tests/Processors/Monolog/DeamonLoggerExtraWebProcessorTest.php index 41bbb9e..8a06810 100644 --- a/Tests/Processors/Monolog/DeamonLoggerExtraWebProcessorTest.php +++ b/Tests/Processors/Monolog/DeamonLoggerExtraWebProcessorTest.php @@ -217,6 +217,7 @@ protected function assertArrayHasKeyAndEquals($key, $array, $value) * @param array $context * * @return array Record + * @throws \Psr\Log\InvalidArgumentException */ protected function getRecord($level = Logger::WARNING, $message = 'test', $context = array()) { diff --git a/composer.json b/composer.json index 59bff57..9b8af49 100644 --- a/composer.json +++ b/composer.json @@ -17,16 +17,16 @@ "Log" ], "require": { - "php": ">=5.6", - "symfony/security": "^2.7|^3.0", - "symfony/dependency-injection": "^2.7|^3.0", - "symfony/monolog-bridge": "^2.7|^3.0", - "symfony/http-foundation": "^2.7|^3.0", - "symfony/http-kernel": "^2.7|^3.0", - "symfony/config": "^2.7|^3.0" + "php": ">=7.1", + "symfony/security": "^4.0", + "symfony/dependency-injection": "^4.0", + "symfony/monolog-bridge": "^4.0", + "symfony/http-kernel": "^4.0", + "symfony/http-foundation": "^4.0", + "symfony/config": "^4.0" }, "require-dev": { - "phpunit/phpunit": "^5.4.3" + "symfony/phpunit-bridge": "^4.0" }, "autoload": { "psr-4": { @@ -35,9 +35,12 @@ }, "extra": { "branch-alias": { - "dev-master": "2.1-dev" + "dev-master": "4.0-dev" } }, + "suggest": { + "graylog2/gelf-php": "For the possibility to send monolog messages to a graylog server such as ELK" + }, "support": { "issues": "https://github.com/FrDeamon/logger-extra-bundle/issues", "source": "https://github.com/FrDeamon/logger-extra-bundle" diff --git a/phpunit.xml.dist b/phpunit.xml.dist index bc48ef2..09c50e3 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -18,6 +18,7 @@ +