Skip to content

Commit

Permalink
Merge pull request #2 from pierredup/logger
Browse files Browse the repository at this point in the history
Update logger to use LoggerAwareTrait
  • Loading branch information
sarelvdwalt authored Oct 13, 2016
2 parents b2025b4 + 4ea12bf commit 2c14852
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 32 deletions.
21 changes: 8 additions & 13 deletions src/SwarmProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Afrihost\SwarmProcess;

use Psr\Log\LoggerInterface;
use Symfony\Component\Process\Process;

class SwarmProcess extends SwarmProcessBase
Expand All @@ -24,17 +23,11 @@ class SwarmProcess extends SwarmProcessBase
/** @var int */
private $runningProcessKeyTracker = 0;

/**
* @return LoggerInterface
*/
public function getLogger()
{
return $this->logger;
}

/**
* Runs all the processes, not going over the maxRunStackSize, and continuing until all processes in the processingStack has run their course.
*
* @param callable $moreWorkToAddCallable
* @param callable $shouldContinueRunningCallable
*/
public function run(callable $moreWorkToAddCallable = null, callable $shouldContinueRunningCallable = null)
{
Expand Down Expand Up @@ -64,7 +57,7 @@ public function tick()
$tmpProcess = array_shift($this->queue);
$tmpProcess->start();
$this->currentRunningStack[++$this->runningProcessKeyTracker] = $tmpProcess;
$this->getLogger()->info('+ Started Process ' . $this->runningProcessKeyTracker . ' [' . $tmpProcess->getCommandLine() . ']');
$this->logger->info('+ Started Process ' . $this->runningProcessKeyTracker . ' [' . $tmpProcess->getCommandLine() . ']');
}

// Loop through the running things to check if they're done:
Expand All @@ -75,7 +68,7 @@ public function tick()
'ExitCode:'.$runningProcess->getExitCode().'('.$runningProcess->getExitCodeText().') '.
'[' . count($this->queue) . ' left in queue]';
unset($this->currentRunningStack[$runningProcessKey]);
$this->getLogger()->info($logMessage);
$this->logger->info($logMessage);
}
}

Expand Down Expand Up @@ -135,7 +128,7 @@ public function pushProcessOnQueue(Process $process)
{
$this->queue[] = $process;

$this->getLogger()->debug('Process pushed on to stack. Stack size: ' . count($this->queue));
$this->logger->debug('Process pushed on to stack. Stack size: ' . count($this->queue));

return $this;
}
Expand All @@ -154,7 +147,9 @@ public function getMaxRunStackSize()
* Set the maximum number of processes that can be run at the same time (concurrently)
*
* @param int $maxRunStackSize
*
* @return SwarmProcess
* @throws \OutOfBoundsException
*/
public function setMaxRunStackSize($maxRunStackSize)
{
Expand All @@ -164,7 +159,7 @@ public function setMaxRunStackSize($maxRunStackSize)

$this->maxRunStackSize = $maxRunStackSize;

$this->getLogger()->debug('$maxRunStackSize changed to ' . $maxRunStackSize);
$this->logger->debug('$maxRunStackSize changed to ' . $maxRunStackSize);

return $this;
}
Expand Down
14 changes: 5 additions & 9 deletions src/SwarmProcessBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,23 @@

namespace Afrihost\SwarmProcess;

use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

abstract class SwarmProcessBase
{
/** @var LoggerInterface */
protected $logger;
use LoggerAwareTrait;

/**
* SwarmProcess constructor.
*
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger = null)
{
if (!is_null($logger)) {
$this->logger = $logger;
} else {
$this->logger = new NullLogger();
}
$this->setLogger($logger ?: new NullLogger());

$this->getLogger()->debug('__construct(ed) SwarmProcess');
$this->logger->debug('__construct(ed) SwarmProcess');
}

}
8 changes: 1 addition & 7 deletions tests/SwarmProcessTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php
use Afrihost\SwarmProcess\SwarmProcess;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

/**
* User: sarel
Expand All @@ -10,15 +8,11 @@
*/
class SwarmProcessTest extends PHPUnit_Framework_TestCase
{
/** @var LoggerInterface */
private $logger;

/** @var SwarmProcess */
private $swarm;

protected function setUp() {
$this->logger = new NullLogger();
$this->swarm = new SwarmProcess($this->logger);
$this->swarm = new SwarmProcess();
}

public function testSetMaxRunStack()
Expand Down
16 changes: 13 additions & 3 deletions tests/SwarmProcessTestNoSetup.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
use Afrihost\SwarmProcess\SwarmProcess;
use Psr\Log\AbstractLogger;
use Psr\Log\NullLogger;

/**
Expand All @@ -14,17 +15,26 @@ class SwarmProcessTestNoSetup extends PHPUnit_Framework_TestCase
*/
public function testLoggerGiven()
{
$given = new NullLogger();
$given = new TestLogger();

$swarm = new SwarmProcess($given);

$this->assertTrue($given === $swarm->getLogger(), 'Logger given at construction not the same as class has internally');
$this->assertSame($given, \PHPUnit_Framework_Assert::getObjectAttribute($swarm, 'logger'), 'Logger given at construction not the same as class has internally');
}

public function testLoggerNotGiven()
{
$swarm = new SwarmProcess();

$this->assertInstanceOf('Psr\Log\NullLogger', $swarm->getLogger(), 'Logger expected when none is given, should be the NullLogger');
$this->assertInstanceOf('Psr\Log\NullLogger', \PHPUnit_Framework_Assert::getObjectAttribute($swarm, 'logger'), 'Logger expected when none is given, should be the NullLogger');
}
}

class TestLogger extends AbstractLogger
{
/**
* {@inheritdoc}
*/
public function log($level, $message, array $context = []) {}
}

0 comments on commit 2c14852

Please sign in to comment.