forked from koderhut/onelog-bundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Denis-Florin Rendler
committed
Jul 24, 2018
0 parents
commit bbb5032
Showing
37 changed files
with
1,769 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
vendor/ | ||
.phpunit.result.cache | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace KoderHut\OnelogBundle; | ||
|
||
/** | ||
* Interface ContextualInterface | ||
* | ||
* @author Denis-Florin Rendler <[email protected]> | ||
*/ | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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)]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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); | ||
} | ||
} |
Oops, something went wrong.