Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis-Florin Rendler committed Jul 24, 2018
0 parents commit bbb5032
Show file tree
Hide file tree
Showing 37 changed files with 1,769 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
.phpunit.result.cache
composer.lock
27 changes: 27 additions & 0 deletions ContextualInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace KoderHut\OnelogBundle;

/**
* Interface ContextualInterface
*
* @author Denis-Florin Rendler <connect@rendler.me>
*/
interface ContextualInterface
{
/**
* Set the context for the logger
*
* @param array $context
*
* @return mixed
*/
public function setContext(array $context);

/**
* Retrieve the context of this class
*
* @return array
*/
public function getContext(): array;
}
29 changes: 29 additions & 0 deletions DependencyInjection/Compiler/LoggerWrapPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

namespace KoderHut\OnelogBundle\DependencyInjection\Compiler;

use KoderHut\OnelogBundle\OneLog;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Class LoggerWrapPass
*
* @author Denis-Florin Rendler <connect@rendler.me>
*/
class LoggerWrapPass implements CompilerPassInterface
{
/**
* @inheritDoc
*/
public function process(ContainerBuilder $container)
{
if (null === ($loggerId = $container->getParameter('onelog.logger_service'))) {
return;
}

$oneLogDefinition = $container->findDefinition(OneLog::class);
$oneLogDefinition->replaceArgument(0, new Reference($loggerId));
}
}
33 changes: 33 additions & 0 deletions DependencyInjection/Compiler/RegisterMonologChannels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types=1);

namespace KoderHut\OnelogBundle\DependencyInjection\Compiler;

use KoderHut\OnelogBundle\OneLog;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Class RegisterMonologChannels
*
* @author Denis-Florin Rendler <connect@rendler.me>
*/
class RegisterMonologChannels implements CompilerPassInterface
{
/**
* @inheritDoc
*/
public function process(ContainerBuilder $container)
{
if (!$container->getParameter('onelog.register_monolog_channels') || !$container->hasDefinition($container->getParameter('onelog.logger_service'))) {
return;
}

$onelogDefinition = $container->findDefinition(OneLog::class);
$monologChannels = preg_grep('/monolog\.logger\..*/', $container->getServiceIds());

foreach ($monologChannels as $channelId) {
$onelogDefinition->addMethodCall('registerLogger', [new Reference($channelId)]);
}
}
}
48 changes: 48 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php declare(strict_types=1);

namespace KoderHut\OnelogBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

/**
* Class OneLog
*
* @author Denis-Florin Rendler <connect@rendler.me>
*/
class Configuration implements ConfigurationInterface
{
/**
* @inheritDoc
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();

$root = $treeBuilder->root('onelog');

$root
->children()
->scalarNode('logger_service')
->example('logger_service: monolog')
->info('The logger service to wrap. This will be used as the default logger')
->isRequired()
->treatNullLike('logger')
->defaultValue('logger')
->end()
->booleanNode('register_global')
->info('Register the OneLog class in the global namespace')
->defaultFalse()
->treatNullLike(false)
->end()
->booleanNode('register_monolog_channels')
->info('Register the Monolog channels as OneLog properties')
->defaultFalse()
->treatNullLike(false)
->end()
->end();

return $treeBuilder;
}

}
32 changes: 32 additions & 0 deletions DependencyInjection/OnelogExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types=1);

namespace KoderHut\OnelogBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

/**
* Class OnelogExtension
*
* @author Denis-Florin Rendler <connect@rendler.me>
*/
class OnelogExtension extends Extension
{
/**
* @inheritDoc
*/
public function load(array $configs, ContainerBuilder $container)
{
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('onelog.xml');

$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter('onelog.logger_service', $config['logger_service']);
$container->setParameter('onelog.register_global', $config['register_global']);
$container->setParameter('onelog.register_monolog_channels', $config['register_monolog_channels']);
}
}
30 changes: 30 additions & 0 deletions Exceptions/ClassAlreadyRegistered.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

namespace KoderHut\OnelogBundle\Exceptions;

use KoderHut\OnelogBundle\ContextualInterface;
use KoderHut\OnelogBundle\Helper\ContextualTrait;

/**
* Class ClassAlreadyRegistered
*
* @author Denis-Florin Rendler <connect@rendler.me>
*/
class ClassAlreadyRegistered extends \LogicException implements ContextualInterface
{
use ContextualTrait;

/**
* ClassAlreadyRegistered constructor.
*
* @param string $message
* @param array $context
*/
public function __construct(string $message, array $context = [])
{
parent::__construct($message);

$this->setContext($context);
}

}
29 changes: 29 additions & 0 deletions Exceptions/LoggerNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types=1);

namespace KoderHut\OnelogBundle\Exceptions;

use KoderHut\OnelogBundle\ContextualInterface;
use KoderHut\OnelogBundle\Helper\ContextualTrait;

/**
* Class LoggerNotFound
*
* @author Denis-Florin Rendler <connect@rendler.me>
*/
class LoggerNotFoundException extends \RuntimeException implements ContextualInterface
{
use ContextualTrait;

/**
* LoggerNotFoundException constructor.
*
* @param string $message
* @param array $context
*/
public function __construct(string $message, array $context = [])
{
parent::__construct($message);

$this->setContext($context);
}
}
34 changes: 34 additions & 0 deletions Helper/ContextualTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types=1);

namespace KoderHut\OnelogBundle\Helper;

/**
* Trait ContextualTrait
*
* @author Denis-Florin Rendler <connect@rendler.me>
*/
trait ContextualTrait
{
/**
* @var array
*/
private $contextualData = [];

/**
* @inheritdoc
*/
public function setContext(array $context): self
{
$this->contextualData = $context;

return $this;
}

/**
* @inheritdoc
*/
public function getContext(): array
{
return $this->contextualData;
}
}
36 changes: 36 additions & 0 deletions Helper/GlobalNamespaceRegister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types=1);

namespace KoderHut\OnelogBundle\Helper;

use KoderHut\OnelogBundle\Exceptions\ClassAlreadyRegistered;

/**
* Class GlobalNamespaceRegister
*
* @author Denis-Florin Rendler <connect@rendler.me>
*/
class GlobalNamespaceRegister
{
/**
* Register a class in another namespace
*
* @param string $alias
* @param string|object $class
*
* @return bool
*/
public static function register(string $alias, $class): bool
{
if (is_object($class)) {
$class = get_class($class);
}

if ($alias === $class || class_exists($alias)) {
throw new ClassAlreadyRegistered('A class is already registered for this namespace.', [
'class_alias' => $alias
]);
}

return class_alias($class, $alias, true);
}
}

0 comments on commit bbb5032

Please sign in to comment.