Skip to content

Commit

Permalink
Feat update to sf4 (#21)
Browse files Browse the repository at this point in the history
Update bundle code and tests to work with php > 7.1. min sf versionis now 4.0
  • Loading branch information
Deamon authored Oct 29, 2018
1 parent c397149 commit 2ccb1b4
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
54 changes: 43 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions DependencyInjection/DeamonLoggerExtraExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
31 changes: 16 additions & 15 deletions Processors/Monolog/DeamonLoggerExtraWebProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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;
Expand All @@ -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)) {
Expand All @@ -153,19 +154,19 @@ 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;
}

/**
* Add channel info to ease the log interpretation.
*/
private function addChannelInfo()
private function addChannelInfo(): void
{
$this->addInfo('global_channel', $this->record['channel']);

Expand All @@ -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;
Expand All @@ -194,39 +195,39 @@ 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];
}

/**
* @param DeamonLoggerExtraContext $loggerExtraContext
*/
public function setLoggerExtraContext(DeamonLoggerExtraContext $loggerExtraContext)
public function setLoggerExtraContext(DeamonLoggerExtraContext $loggerExtraContext): void
{
$this->loggerExtraContext = $loggerExtraContext;
}

/**
* @param string $environment
*/
public function setEnvironment($environment)
public function setEnvironment(string $environment): void
{
$this->environment = $environment;
}

/**
* @param TokenStorageInterface $tokenStorage
*/
public function setTokenStorage(TokenStorageInterface $tokenStorage)
public function setTokenStorage(TokenStorageInterface $tokenStorage): void
{
$this->tokenStorage = $tokenStorage;
}

/**
* @param RequestStack $requestStack
*/
public function setRequestStack(RequestStack $requestStack)
public function setRequestStack(RequestStack $requestStack): void
{
$this->requestStack = $requestStack;
}
Expand Down
47 changes: 23 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.

Expand All @@ -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.
Expand All @@ -78,23 +76,23 @@ 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"
```

## 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:
Expand All @@ -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: ~
```
15 changes: 5 additions & 10 deletions Resources/config/processors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<parameters>
<parameter key="deamon.logger_extra.processors.web_processor.class">Deamon\LoggerExtraBundle\Processors\Monolog\DeamonLoggerExtraWebProcessor</parameter>
</parameters>
<services>
<service id="deamon.logger_extra.processors.web_processor" class="%deamon.logger_extra.processors.web_processor.class%">
<tag name="monolog.processor" handler="default_info"/>
<tag name="monolog.processor" handler="default_notice"/>
<argument />
<service id="deamon.logger_extra.processors.web_processor" class="Deamon\LoggerExtraBundle\Processors\Monolog\DeamonLoggerExtraWebProcessor">
<call method="setLoggerExtraContext"><argument type="service" id="deamon.logger_extra.context" /></call>
<call method="setEnvironment"><argument type="string">%kernel.environment%</argument></call>
<call method="setTokenStorage"><argument type="service" id="security.token_storage" /></call>
<call method="setRequestStack"><argument type="service" id="request_stack" /></call>
<call method="setEnvironment"><argument type="string" on-invalid="ignore">%kernel.environment%</argument></call>
<call method="setTokenStorage"><argument type="service" id="security.token_storage" on-invalid="ignore"/></call>
<call method="setRequestStack"><argument type="service" id="request_stack" on-invalid="ignore"/></call>
</service>
<service id="Deamon\LoggerExtraBundle\Processors\Monolog\DeamonLoggerExtraWebProcessor" alias="deamon.logger_extra.processors.web_processor" />
</services>
</container>
8 changes: 2 additions & 6 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<parameters>
<parameter key="deamon.logger_extra.context.class">Deamon\LoggerExtraBundle\Services\DeamonLoggerExtraContext</parameter>
</parameters>
<services>
<service id="deamon.logger_extra.context" class="%deamon.logger_extra.context.class%">
<argument />
</service>
<service id="deamon.logger_extra.context" class="Deamon\LoggerExtraBundle\Services\DeamonLoggerExtraContext" />
<service id="Deamon\LoggerExtraBundle\Services\DeamonLoggerExtraContext" alias="deamon.logger_extra.context" />
</services>
</container>
Loading

0 comments on commit 2ccb1b4

Please sign in to comment.