-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from dmnc/master
Add support for handlers where constructor params are objects to fix #30
- Loading branch information
Showing
8 changed files
with
157 additions
and
14 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,22 @@ | ||
--- | ||
version: 1 | ||
|
||
disable_existing_loggers: false | ||
|
||
handlers: | ||
sentry: | ||
class: Monolog\Handler\RavenHandler | ||
level: DEBUG | ||
raven_client: | ||
class: Raven_Client | ||
options_or_dsn: https://something:[email protected]/1 | ||
redis: | ||
class: Monolog\Handler\RedisHandler | ||
level: DEBUG | ||
key: cascade | ||
redis: | ||
class: Redis | ||
connect: ['127.0.0.1', 6379] | ||
loggers: | ||
dependency: | ||
handlers: [sentry, redis] |
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,13 @@ | ||
<?php | ||
require_once(realpath(__DIR__.'/../vendor/autoload.php')); | ||
|
||
use Cascade\Cascade; | ||
|
||
// For these to work you will need php-redis and raven/raven | ||
|
||
// You will want to update this file with a valid dsn | ||
$loggerConfigFile = realpath(__DIR__.'/dependency_config.yml'); | ||
|
||
Cascade::fileConfig($loggerConfigFile); | ||
Cascade::getLogger('dependency')->info('Well, that works!'); | ||
Cascade::getLogger('dependency')->error('Maybe not...'); |
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 |
---|---|---|
|
@@ -98,6 +98,25 @@ protected function setClass() | |
unset($this->rawOptions['class']); | ||
} | ||
|
||
/** | ||
* Recursively loads objects into any of the rawOptions that represent | ||
* a class | ||
* | ||
* @author Dom Morgan <[email protected]> | ||
*/ | ||
protected function loadChildClasses() | ||
{ | ||
foreach ($this->rawOptions as &$option) { | ||
if (is_array($option) | ||
&& array_key_exists('class', $option) | ||
&& class_exists($option['class']) | ||
) { | ||
$classLoader = new ClassLoader($option); | ||
$option = $classLoader->load(); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Return option values indexed by name using camelCased keys | ||
* | ||
|
@@ -163,6 +182,8 @@ private function resolveOptions() | |
*/ | ||
public function load() | ||
{ | ||
$this->loadChildClasses(); | ||
|
||
list($constructorResolvedOptions, $extraResolvedOptions) = $this->resolveOptions(); | ||
$instance = $this->reflected->newInstanceArgs($constructorResolvedOptions); | ||
|
||
|
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
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
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
use Cascade\Config\Loader\ClassLoader; | ||
use Cascade\Tests\Fixtures\SampleClass; | ||
use Cascade\Tests\Fixtures\DependentClass; | ||
|
||
/** | ||
* Class ClassLoaderTest | ||
|
@@ -145,4 +146,29 @@ public function testLoad() | |
|
||
$this->assertEquals($expectedInstance, $instance); | ||
} | ||
|
||
/** | ||
* Test a nested class to load | ||
* | ||
* @author Dom Morgan <[email protected]> | ||
*/ | ||
public function testLoadDependency() | ||
{ | ||
$options = array( | ||
'class' => 'Cascade\Tests\Fixtures\DependentClass', | ||
'dependency' => array( | ||
'class' => 'Cascade\Tests\Fixtures\SampleClass', | ||
'mandatory' => 'someValue', | ||
) | ||
); | ||
|
||
$loader = new ClassLoader($options); | ||
$instance = $loader->load(); | ||
|
||
$expectedInstance = new DependentClass( | ||
new SampleClass('someValue') | ||
); | ||
|
||
$this->assertEquals($expectedInstance, $instance); | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
/** | ||
* This file is part of the Monolog Cascade package. | ||
* | ||
* (c) Raphael Antonmattei <[email protected]> | ||
* (c) The Orchard | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
namespace Cascade\Tests\Fixtures; | ||
|
||
/** | ||
* Class SampleClass | ||
* | ||
* @author Raphael Antonmattei <[email protected]> | ||
*/ | ||
class DependentClass | ||
{ | ||
/** | ||
* An object dependency | ||
* @var Cascade\Tests\Fixtures\SampleClass | ||
*/ | ||
private $dependency; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param mixed $mandatory Some mandatory param | ||
* @param string $optionalA Some optional param | ||
*/ | ||
public function __construct( | ||
SampleClass $dependency | ||
) { | ||
$this->setDependency($dependency); | ||
} | ||
|
||
/** | ||
* Set the object dependency | ||
* | ||
* @param Cascade\Tests\Fixtures\SampleClass $dependency Some value | ||
*/ | ||
public function setDependency($dependency) | ||
{ | ||
$this->dependency = $dependency; | ||
} | ||
} |
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