Skip to content

Commit

Permalink
More flexible AwareTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
ftdebugger committed Oct 22, 2013
1 parent 336ca83 commit cdc7fcf
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 31 deletions.
65 changes: 45 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The recommended way to install is through composer.
```json
{
"require": {
"enlitepro/enlite-monolog": "1.0.*"
"enlitepro/enlite-monolog": "~1.1"
}
}
```
Expand All @@ -23,11 +23,15 @@ Add `EnliteMonolog` to your `config/application.config.php` to enable module.

```php
// usage over service locator
$serviceLocator->get('EnliteMonologService')->addDebug('hellow world');
$serviceLocator->get('EnliteMonologService')->addDebug('hello world');

use EnliteMonolog\Service\MonologServiceAwareInterface,
EnliteMonolog\Service\MonologServiceAwareTrait;

// usage in your services
class MyService implements EnliteMonolog\Service\MonologServiceAwareInterface {
use EnliteMonolog\Service\MonologServiceAwareTrait;
class MyService implements MonologServiceAwareInterface
{
use MonologServiceAwareTrait;

public function whatever()
{
Expand All @@ -41,23 +45,44 @@ By default it write logs to `data/logs/application.log`. If you want change this

```php
'EnliteMonolog' => array(
// Logger name
// 'name' => 'EnliteMonolog',

// Handlers, it can be service locator alias(string) or config(array)
'handlers' => array(
// by config
'default' => array(
'name' => 'Monolog\Handler\StreamHandler',
'args' => array(
'path' => 'data/log/application.log',
'level' => \Monolog\Logger::DEBUG,
'bubble' => true
)
),
'EnliteMonologService' => array(
// Logger name
// 'name' => 'EnliteMonolog',

// Handlers, it can be service locator alias(string) or config(array)
'handlers' => array(
// by config
'default' => array(
'name' => 'Monolog\Handler\StreamHandler',
'args' => array(
'path' => 'data/log/application.log',
'level' => \Monolog\Logger::DEBUG,
'bubble' => true
)
),

// by service locator
'MyMonologHandler'
)
),

// you can specify another logger
// for example ChromePHPHandler

// by service locator
'MyMonologHandler'
'MyChromeLogger' => array(
'name' => 'MyName',
'handlers' => array(
array(
'name' => 'Monolog\Handler\ChromePHPHandler',
)
)
)
),
```

now you can use it

```php
$serviceLocator->get('EnliteMonologService')->addDebug('hello world');
$serviceLocator->get('MyChromeLogger')->addInfo('hello world');
```
9 changes: 7 additions & 2 deletions src/EnliteMonolog/Service/MonologServiceAwareTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ trait MonologServiceAwareTrait
*/
protected $monologService;

/**
* @var string
*/
protected $monologLoggerName = 'EnliteMonologService';

/**
* @param Logger $monologService
*/
Expand All @@ -35,12 +40,12 @@ public function getMonologService()
{
if (null === $this->monologService) {
if ($this instanceof ServiceLocatorAwareInterface || method_exists($this, 'getServiceLocator')) {
$this->monologService = $this->getServiceLocator()->get('EnliteMonologService');
$this->monologService = $this->getServiceLocator()->get($this->monologLoggerName);
} else {
if (property_exists($this, 'serviceLocator')
&& $this->monologService instanceof ServiceLocatorInterface
) {
$this->monologService = $this->serviceLocator->get('EnliteMonologService');
$this->monologService = $this->serviceLocator->get($this->monologLoggerName);
} else {
throw new RuntimeException('Service locator not found');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public function testGetConfigWithNoneConfig()
{
$factory = new MonologServiceAbstractFactory();
$serviceLocator = new ServiceManager();
$serviceLocator->setService('config', []);
$serviceLocator->setService('config', array());

$this->assertEquals([], $factory->getConfig($serviceLocator));
$this->assertEquals(array(), $factory->getConfig($serviceLocator));
}

public function testGetConfigWithConfig()
Expand All @@ -29,22 +29,22 @@ public function testGetConfigWithConfig()
'config',
array(
'EnliteMonolog' => array(
'EnliteMonolog' => []
'EnliteMonolog' => array()
)
)
);

$this->assertEquals(['EnliteMonolog' => []], $factory->getConfig($serviceLocator));
$this->assertEquals(array('EnliteMonolog' => array()), $factory->getConfig($serviceLocator));
}

public function testGetConfigWithAlreadyFetchConfig()
{
$factory = new MonologServiceAbstractFactory();
$factory->setConfig(['a' => 'b']);
$factory->setConfig(array('a' => 'b'));
$serviceLocator = new ServiceManager();


$this->assertEquals(['a' => 'b'], $factory->getConfig($serviceLocator));
$this->assertEquals(array('a' => 'b'), $factory->getConfig($serviceLocator));
}

public function testCanCreateServiceWithName()
Expand All @@ -55,7 +55,7 @@ public function testCanCreateServiceWithName()
'config',
array(
'EnliteMonolog' => array(
'default' => []
'default' => array()
)
)
);
Expand All @@ -71,12 +71,12 @@ public function testCreateServiceWithName()
'config',
array(
'EnliteMonolog' => array(
'default' => []
'default' => array()
)
)
);

$service = $factory->createServiceWithName($serviceLocator, 'default', 'default');
$factory->createServiceWithName($serviceLocator, 'default', 'default');

}

Expand Down

0 comments on commit cdc7fcf

Please sign in to comment.