Skip to content

Commit

Permalink
Reworking Log / Event system slightly
Browse files Browse the repository at this point in the history
 * Added traits for EventDispatcherAwareTrait and LoggerAwareTrait for
   PHP 5.4+ use.
 * ClosureLogAdapter added
 * All *LogAdapter classes now allow for a `null` log object
 * Moved around the event dispatching methods for Workers

This commit contains general changes preparing for the daemons.
  • Loading branch information
justinrainbow committed Jul 24, 2012
1 parent c105b62 commit 100e8e2
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 39 deletions.
46 changes: 46 additions & 0 deletions src/Presque/Event/EventDispatcherAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Event;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Provides methods to use with the Symfony EventDispatcher component.
*
* @author Justin Rainbow <justin.rainbow@gmail.com>
*/
trait EventDispatcherAwareTrait
{
protected $dispatcher;

/**
* Sets an instance of the EventDispatcherInterface for this object.
* If the `$dispatcher` is null, the object must remove any
* current EventDispatcherInterface assigned to it.
*
* @param EventDispatcherInterface $dispatcher
*/
public function setEventDispatcher(EventDispatcherInterface $dispatcher = null)
{
$this->dispatcher = $dispatcher;
}

/**
* Checks if there is an EventDispatcher available to the current object.
*
* @return Boolean
*/
public function hasEventDispatcher()
{
return null !== $this->dispatcher;
}
}
32 changes: 24 additions & 8 deletions src/Presque/Log/AbstractLogAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,62 +23,78 @@ abstract class AbstractLogAdapter implements LoggerInterface
*/
public function emerg($message, array $context = array())
{
$this->log->emerg($message, $context);
if (null !== $this->log) {
$this->log->emerg($message, $context);
}
}

/**
* {@inheritDoc}
*/
public function alert($message, array $context = array())
{
$this->log->alert($message, $context);
if (null !== $this->log) {
$this->log->alert($message, $context);
}
}

/**
* {@inheritDoc}
*/
public function crit($message, array $context = array())
{
$this->log->crit($message, $context);
if (null !== $this->log) {
$this->log->crit($message, $context);
}
}

/**
* {@inheritDoc}
*/
public function err($message, array $context = array())
{
$this->log->err($message, $context);
if (null !== $this->log) {
$this->log->err($message, $context);
}
}

/**
* {@inheritDoc}
*/
public function warn($message, array $context = array())
{
$this->log->warn($message, $context);
if (null !== $this->log) {
$this->log->warn($message, $context);
}
}

/**
* {@inheritDoc}
*/
public function notice($message, array $context = array())
{
$this->log->notice($message, $context);
if (null !== $this->log) {
$this->log->notice($message, $context);
}
}

/**
* {@inheritDoc}
*/
public function info($message, array $context = array())
{
$this->log->info($message, $context);
if (null !== $this->log) {
$this->log->info($message, $context);
}
}

/**
* {@inheritDoc}
*/
public function debug($message, array $context = array())
{
$this->log->debug($message, $context);
if (null !== $this->log) {
$this->log->debug($message, $context);
}
}
}
105 changes: 105 additions & 0 deletions src/Presque/Log/ClosureLogAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Log;

/**
* Abstract log adapter for Presque
*/
class ClosureLogAdapter implements LoggerInterface
{
protected $log;

public function __construct(\Closure $log = null)
{
$this->log = $log;
}

/**
* {@inheritDoc}
*/
public function emerg($message, array $context = array())
{
if (null !== $this->log) {
call_user_func($this->log, $message, 'emerg', $context);
}
}

/**
* {@inheritDoc}
*/
public function alert($message, array $context = array())
{
if (null !== $this->log) {
call_user_func($this->log, $message, 'alert', $context);
}
}

/**
* {@inheritDoc}
*/
public function crit($message, array $context = array())
{
if (null !== $this->log) {
call_user_func($this->log, $message, 'crit', $context);
}
}

/**
* {@inheritDoc}
*/
public function err($message, array $context = array())
{
if (null !== $this->log) {
call_user_func($this->log, $message, 'err', $context);
}
}

/**
* {@inheritDoc}
*/
public function warn($message, array $context = array())
{
if (null !== $this->log) {
call_user_func($this->log, $message, 'warn', $context);
}
}

/**
* {@inheritDoc}
*/
public function notice($message, array $context = array())
{
if (null !== $this->log) {
call_user_func($this->log, $message, 'notice', $context);
}
}

/**
* {@inheritDoc}
*/
public function info($message, array $context = array())
{
if (null !== $this->log) {
call_user_func($this->log, $message, 'info', $context);
}
}

/**
* {@inheritDoc}
*/
public function debug($message, array $context = array())
{
if (null !== $this->log) {
call_user_func($this->log, $message, 'debug', $context);
}
}
}
33 changes: 33 additions & 0 deletions src/Presque/Log/LoggerAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Presque package.
*
* (c) Justin Rainbow <justin.rainbow@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Presque\Log;

trait LoggerAwareTrait
{
protected $logger;

/**
* {@inheritDoc}
*/
public function setLogger(LoggerInterface $logger = null)
{
$this->logger = $logger;
}

/**
* {@inheritDoc}
*/
public function hasLogger()
{
return null !== $this->logger;
}
}
2 changes: 1 addition & 1 deletion src/Presque/Log/SymfonyLogAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SymfonyLogAdapter extends AbstractLogAdapter
/**
* {@inheritdoc}
*/
public function __construct(SymfonyLoggerInterface $logObject)
public function __construct(SymfonyLoggerInterface $logObject = null)
{
$this->log = $logObject;
}
Expand Down
4 changes: 3 additions & 1 deletion src/Presque/Queue/QueueFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

namespace Presque\Queue;

use Presque\Storage\StorageInterface;

interface QueueFactoryInterface
{
function create($name);
function create($name, StorageInterface $storage = null, $timeout = 10);
}
14 changes: 7 additions & 7 deletions src/Presque/StatusInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

interface StatusInterface
{
CONST SUCCESS = 1;
CONST FAILED = 2;
CONST RUNNING = 4;
CONST EXPIRED = 7;
CONST DYING = 8;
CONST STOPPING = 13;
CONST STOPPED = 14;
CONST SUCCESS = 1;
CONST FAILED = 2;
CONST RUNNING = 4;
CONST DYING = 8;
CONST PAUSED = 16;
CONST STOPPING = 32;
CONST STOPPED = 64;
}
29 changes: 25 additions & 4 deletions src/Presque/Worker/AbstractWorker.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ abstract class AbstractWorker implements WorkerInterface, LoggerAwareInterface,
protected $id;
protected $status;
protected $queues;
protected $eventDispatcher;
protected $dispatcher;
protected $logger;

public function __construct($id = null)
Expand Down Expand Up @@ -93,17 +93,17 @@ public function getQueues()
/**
* {@inheritDoc}
*/
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher = null)
public function setEventDispatcher(EventDispatcherInterface $dispatcher = null)
{
$this->eventDispatcher = $eventDispatcher;
$this->dispatcher = $dispatcher;
}

/**
* {@inheritDoc}
*/
public function hasEventDispatcher()
{
return null !== $this->eventDispatcher;
return null !== $this->dispatcher;
}

/**
Expand All @@ -121,4 +121,25 @@ public function hasLogger()
{
return null !== $this->logger;
}

/**
* Wrapper for dispatching events. The provided `$event` will be returned
* immediately if there is no EventDispatcher available.
*
* If there is an EventDispatcher, the `dispatch` event will be called, and
* the `$event` will still be returned.
*
* @param string $name Event name to dispatch
* @param mixed $event Event payload
*
* @return mixed $event
*/
protected function dispatchEvent($name, $event)
{
if (!$this->hasEventDispatcher()) {
return $event;
}

return $this->dispatcher->dispatch($name, $event);
}
}
Loading

0 comments on commit 100e8e2

Please sign in to comment.