Skip to content

Commit bbb5032

Browse files
author
Denis-Florin Rendler
committed
Initial commit
0 parents  commit bbb5032

37 files changed

Lines changed: 1769 additions & 0 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
.phpunit.result.cache
3+
composer.lock

ContextualInterface.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace KoderHut\OnelogBundle;
4+
5+
/**
6+
* Interface ContextualInterface
7+
*
8+
* @author Denis-Florin Rendler <connect@rendler.me>
9+
*/
10+
interface ContextualInterface
11+
{
12+
/**
13+
* Set the context for the logger
14+
*
15+
* @param array $context
16+
*
17+
* @return mixed
18+
*/
19+
public function setContext(array $context);
20+
21+
/**
22+
* Retrieve the context of this class
23+
*
24+
* @return array
25+
*/
26+
public function getContext(): array;
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace KoderHut\OnelogBundle\DependencyInjection\Compiler;
4+
5+
use KoderHut\OnelogBundle\OneLog;
6+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
use Symfony\Component\DependencyInjection\Reference;
9+
10+
/**
11+
* Class LoggerWrapPass
12+
*
13+
* @author Denis-Florin Rendler <connect@rendler.me>
14+
*/
15+
class LoggerWrapPass implements CompilerPassInterface
16+
{
17+
/**
18+
* @inheritDoc
19+
*/
20+
public function process(ContainerBuilder $container)
21+
{
22+
if (null === ($loggerId = $container->getParameter('onelog.logger_service'))) {
23+
return;
24+
}
25+
26+
$oneLogDefinition = $container->findDefinition(OneLog::class);
27+
$oneLogDefinition->replaceArgument(0, new Reference($loggerId));
28+
}
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace KoderHut\OnelogBundle\DependencyInjection\Compiler;
4+
5+
use KoderHut\OnelogBundle\OneLog;
6+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
7+
use Symfony\Component\DependencyInjection\ContainerBuilder;
8+
use Symfony\Component\DependencyInjection\Reference;
9+
10+
/**
11+
* Class RegisterMonologChannels
12+
*
13+
* @author Denis-Florin Rendler <connect@rendler.me>
14+
*/
15+
class RegisterMonologChannels implements CompilerPassInterface
16+
{
17+
/**
18+
* @inheritDoc
19+
*/
20+
public function process(ContainerBuilder $container)
21+
{
22+
if (!$container->getParameter('onelog.register_monolog_channels') || !$container->hasDefinition($container->getParameter('onelog.logger_service'))) {
23+
return;
24+
}
25+
26+
$onelogDefinition = $container->findDefinition(OneLog::class);
27+
$monologChannels = preg_grep('/monolog\.logger\..*/', $container->getServiceIds());
28+
29+
foreach ($monologChannels as $channelId) {
30+
$onelogDefinition->addMethodCall('registerLogger', [new Reference($channelId)]);
31+
}
32+
}
33+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace KoderHut\OnelogBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6+
use Symfony\Component\Config\Definition\ConfigurationInterface;
7+
8+
/**
9+
* Class OneLog
10+
*
11+
* @author Denis-Florin Rendler <connect@rendler.me>
12+
*/
13+
class Configuration implements ConfigurationInterface
14+
{
15+
/**
16+
* @inheritDoc
17+
*/
18+
public function getConfigTreeBuilder()
19+
{
20+
$treeBuilder = new TreeBuilder();
21+
22+
$root = $treeBuilder->root('onelog');
23+
24+
$root
25+
->children()
26+
->scalarNode('logger_service')
27+
->example('logger_service: monolog')
28+
->info('The logger service to wrap. This will be used as the default logger')
29+
->isRequired()
30+
->treatNullLike('logger')
31+
->defaultValue('logger')
32+
->end()
33+
->booleanNode('register_global')
34+
->info('Register the OneLog class in the global namespace')
35+
->defaultFalse()
36+
->treatNullLike(false)
37+
->end()
38+
->booleanNode('register_monolog_channels')
39+
->info('Register the Monolog channels as OneLog properties')
40+
->defaultFalse()
41+
->treatNullLike(false)
42+
->end()
43+
->end();
44+
45+
return $treeBuilder;
46+
}
47+
48+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace KoderHut\OnelogBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\FileLocator;
6+
use Symfony\Component\DependencyInjection\ContainerBuilder;
7+
use Symfony\Component\DependencyInjection\Extension\Extension;
8+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
9+
10+
/**
11+
* Class OnelogExtension
12+
*
13+
* @author Denis-Florin Rendler <connect@rendler.me>
14+
*/
15+
class OnelogExtension extends Extension
16+
{
17+
/**
18+
* @inheritDoc
19+
*/
20+
public function load(array $configs, ContainerBuilder $container)
21+
{
22+
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
23+
$loader->load('onelog.xml');
24+
25+
$configuration = new Configuration();
26+
$config = $this->processConfiguration($configuration, $configs);
27+
28+
$container->setParameter('onelog.logger_service', $config['logger_service']);
29+
$container->setParameter('onelog.register_global', $config['register_global']);
30+
$container->setParameter('onelog.register_monolog_channels', $config['register_monolog_channels']);
31+
}
32+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace KoderHut\OnelogBundle\Exceptions;
4+
5+
use KoderHut\OnelogBundle\ContextualInterface;
6+
use KoderHut\OnelogBundle\Helper\ContextualTrait;
7+
8+
/**
9+
* Class ClassAlreadyRegistered
10+
*
11+
* @author Denis-Florin Rendler <connect@rendler.me>
12+
*/
13+
class ClassAlreadyRegistered extends \LogicException implements ContextualInterface
14+
{
15+
use ContextualTrait;
16+
17+
/**
18+
* ClassAlreadyRegistered constructor.
19+
*
20+
* @param string $message
21+
* @param array $context
22+
*/
23+
public function __construct(string $message, array $context = [])
24+
{
25+
parent::__construct($message);
26+
27+
$this->setContext($context);
28+
}
29+
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace KoderHut\OnelogBundle\Exceptions;
4+
5+
use KoderHut\OnelogBundle\ContextualInterface;
6+
use KoderHut\OnelogBundle\Helper\ContextualTrait;
7+
8+
/**
9+
* Class LoggerNotFound
10+
*
11+
* @author Denis-Florin Rendler <connect@rendler.me>
12+
*/
13+
class LoggerNotFoundException extends \RuntimeException implements ContextualInterface
14+
{
15+
use ContextualTrait;
16+
17+
/**
18+
* LoggerNotFoundException constructor.
19+
*
20+
* @param string $message
21+
* @param array $context
22+
*/
23+
public function __construct(string $message, array $context = [])
24+
{
25+
parent::__construct($message);
26+
27+
$this->setContext($context);
28+
}
29+
}

Helper/ContextualTrait.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace KoderHut\OnelogBundle\Helper;
4+
5+
/**
6+
* Trait ContextualTrait
7+
*
8+
* @author Denis-Florin Rendler <connect@rendler.me>
9+
*/
10+
trait ContextualTrait
11+
{
12+
/**
13+
* @var array
14+
*/
15+
private $contextualData = [];
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
public function setContext(array $context): self
21+
{
22+
$this->contextualData = $context;
23+
24+
return $this;
25+
}
26+
27+
/**
28+
* @inheritdoc
29+
*/
30+
public function getContext(): array
31+
{
32+
return $this->contextualData;
33+
}
34+
}

Helper/GlobalNamespaceRegister.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace KoderHut\OnelogBundle\Helper;
4+
5+
use KoderHut\OnelogBundle\Exceptions\ClassAlreadyRegistered;
6+
7+
/**
8+
* Class GlobalNamespaceRegister
9+
*
10+
* @author Denis-Florin Rendler <connect@rendler.me>
11+
*/
12+
class GlobalNamespaceRegister
13+
{
14+
/**
15+
* Register a class in another namespace
16+
*
17+
* @param string $alias
18+
* @param string|object $class
19+
*
20+
* @return bool
21+
*/
22+
public static function register(string $alias, $class): bool
23+
{
24+
if (is_object($class)) {
25+
$class = get_class($class);
26+
}
27+
28+
if ($alias === $class || class_exists($alias)) {
29+
throw new ClassAlreadyRegistered('A class is already registered for this namespace.', [
30+
'class_alias' => $alias
31+
]);
32+
}
33+
34+
return class_alias($class, $alias, true);
35+
}
36+
}

0 commit comments

Comments
 (0)