Skip to content

Commit

Permalink
Implement factories and aware interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ftdebugger committed Oct 18, 2013
1 parent e43800e commit 347d8cc
Show file tree
Hide file tree
Showing 20 changed files with 828 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 @@
.idea
composer.lock
/vendor
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: php

php:
- 5.3
- 5.4
- 5.5

before_script:
- composer self-update
- composer --dev install

script:
- vendor/bin/phpunit
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,63 @@ enlite-monolog
==============

Monolog integration to Zend Framework 2


INSTALL
=======

The recommended way to install is through composer.

```json
{
"require": {
"enlitepro/enlite-monolog": "1.0.*"
}
}
```

USAGE
=====

Add `EnliteMonolog` to your `config/application.config.php` to enable module.

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

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

public function whatever()
{
$this->getMonologService()->addDebug('hello world');
}
}

```

By default it write logs to `data/logs/application.log`. If you want change this behaviour, add your config following:

```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
)
),

// by service locator
'MyMonologHandler'
)
),
```
27 changes: 27 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "enlitepro/enlite-monolog",
"description": "Monolog integration to Zend Framework 2",
"minimum-stability": "dev",
"authors": [
{
"name": "Evgeny Shpilevsky",
"email": "[email protected]"
}
],
"keywords": [
"Zend Framework 2",
"Monolog"
],
"require": {
"monolog/monolog": "~1.6.0",
"zendframework/zendframework": "2.2.4"
},
"require-dev": {
"phpunit/phpunit": "~3.7"
},
"autoload": {
"psr-0": {
"EnliteMonolog": "src"
}
}
}
7 changes: 7 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<phpunit bootstrap="./test/bootstrap.php" colors="true">
<testsuites>
<testsuite name="Test Suite">
<directory>./test/EnliteMonologTest</directory>
</testsuite>
</testsuites>
</phpunit>
38 changes: 38 additions & 0 deletions src/EnliteMonolog/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* @author Evgeny Shpilevsky <[email protected]>
*/

namespace EnliteMonolog;


use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;

class Module implements
AutoloaderProviderInterface,
ConfigProviderInterface
{

/**
* {@inheritdoc}
*/
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
)
);
}

/**
* {@inheritdoc}
*/
public function getConfig()
{
return include __DIR__ . "/config/module.config.php";
}
}
29 changes: 29 additions & 0 deletions src/EnliteMonolog/config/module.config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

return array(
'EnliteMonolog' => array(
// Logger name
// 'name' => 'EnliteMonolog',

// Handlers, it can be service manager alias(string) or config(array)
'handlers' => array(
'default' => array(
'name' => 'Monolog\Handler\StreamHandler',
'args' => array(
'path' => 'data/log/application.log',
'level' => \Monolog\Logger::DEBUG,
'bubble' => true
)
)
)
),
'service_manager' => array(
'factories' => array(
'EnliteMonologService' => 'EnliteMonolog\MonologServiceFactory',
'EnliteMonologOptions' => 'EnliteMonolog\MonologOptionsFactory',
),
'initializers' => array(
'EnliteMonolog\Service\MonologServiceInitializer'
)
)
);
62 changes: 62 additions & 0 deletions src/EnliteMonolog/src/EnliteMonolog/Service/MonologOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* @author Evgeny Shpilevsky <[email protected]>
*/

namespace EnliteMonolog\Service;


use Zend\Stdlib\AbstractOptions;

class MonologOptions extends AbstractOptions
{

/**
* Logger name
*
* @var string
*/
protected $name = 'EnliteMonolog';

/**
* Handlers
*
* @var array
*/
protected $handlers = array();

/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}

/**
* @return string
*/
public function getName()
{
return $this->name;
}

/**
* @param array $handlers
*/
public function setHandlers(array $handlers)
{
$this->handlers = $handlers;
}

/**
* @return array
*/
public function getHandlers()
{
return $this->handlers;
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @author Evgeny Shpilevsky <[email protected]>
*/

namespace EnliteMonolog\Service;


use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class MonologOptionsFactory implements FactoryInterface
{

/**
* {@inheritdoc}
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $serviceLocator->get('config');
return new MonologOptions(isset($config['EnliteMonolog']) ? $config['EnliteMonolog'] : array());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* @author Evgeny Shpilevsky <[email protected]>
*/

namespace EnliteMonolog\Service;


use Monolog\Logger;

interface MonologServiceAwareInterface
{

/**
* @param Logger $monologService
* @return void
*/
public function setMonologService(Logger $monologService);

/**
* @return Logger
*/
public function getMonologService();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* @author Evgeny Shpilevsky <[email protected]>
*/

namespace EnliteMonolog\Service;


use Monolog\Logger;
use RuntimeException;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

trait MonologServiceAwareTrait
{

/**
* @var Logger
*/
protected $monologService;

/**
* @param Logger $monologService
*/
public function setMonologService(Logger $monologService)
{
$this->monologService = $monologService;
}

/**
* @throws \RuntimeException
* @return Logger
*/
public function getMonologService()
{
if (null === $this->monologService) {
if ($this instanceof ServiceLocatorAwareInterface || method_exists($this, 'getServiceLocator')) {
$this->analyseService = $this->getServiceLocator()->get('EnliteMonologService');
} else {
if (property_exists($this, 'serviceLocator')
&& $this->monologService instanceof ServiceLocatorInterface
) {
$this->analyseService = $this->serviceLocator->get('EnliteMonologService');
} else {
throw new RuntimeException('Service locator not found');
}
}
}
return $this->monologService;
}

}
Loading

0 comments on commit 347d8cc

Please sign in to comment.