Skip to content

Commit

Permalink
Manager::getInstance() for automatic adapter selection, improvements …
Browse files Browse the repository at this point in the history
…on environment detection. Now depends on Respect\Env a little bit
  • Loading branch information
Alexandre Gomes Gaigalas committed Oct 5, 2010
1 parent a31dc6c commit 330967c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 5 deletions.
6 changes: 3 additions & 3 deletions library/Respect/Daemon/Adapters/Upstart.php
Expand Up @@ -10,28 +10,28 @@
use Respect\Daemon\Exceptions\InvalidAdapterException;
use Respect\Env\Wrapper;

//Overrides native PHP functions in this namespace, just on test conditions
Wrapper::evil(__NAMESPACE__);

class Upstart implements AdapterInterface
{

protected $configDir;

public function __construct($configDir='/etc/init')
public function __construct()
{
if (!static::runsOnEnvironment())
throw new InvalidAdapterException(
'Upstart isnt present on this system'
);
$this->setConfigDir($configDir);
}

public static function runsOnEnvironment()
{
$uname = php_uname();
if (false === stripos($uname, 'linux'))
return false;
return false !== stripos(shell_exe('initctl --version'), 'upstart');
return false !== stripos(shell_exec('initctl --version'), 'upstart');
}

public function getConfigDir()
Expand Down
29 changes: 28 additions & 1 deletion library/Respect/Daemon/Manager.php
Expand Up @@ -2,7 +2,34 @@

namespace Respect\Daemon;

use DirectoryIterator;

class Manager
{


public static function getAdapter($name=null)
{
$adapterNs = 'Respect\Daemon\Adapters\\';
if (!is_null($name)) {
$name = $adaptersNs . ucfirst($name);
return new $name;
} else {
$adaptersDir = __DIR__ . DIRECTORY_SEPARATOR . 'Adapters';
foreach (new DirectoryIterator($adaptersDir) as $adapterFile) {
if ($adapterFile->isFile()) {
$adapterName = $adapterFile->getBasename('.php');
$adapterClassName = $adapterNs . $adapterName;
$validEnv = call_user_func(
array(
$adapterClassName,
'runsOnEnvironment'
)
);
if ($validEnv)
return new $adapterClassName;
}
}
}
}

}
39 changes: 38 additions & 1 deletion tests/library/Respect/Adapters/UpstartTest.php
Expand Up @@ -5,6 +5,7 @@
use Respect\Daemon\Job;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use Respect\Env\Wrapper;

class UpstartTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -15,11 +16,13 @@ protected function setUp()
{
$configDir = sys_get_temp_dir() . '/RespectEnvUnitTesting';
@mkdir($configDir);
$this->object = new Upstart($configDir);
$this->object = new Upstart();
$this->object->setConfigDir($configDir);
}

protected function tearDown()
{
Wrapper::set("raw");
$dir = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$this->object->getConfigDir()
Expand All @@ -36,6 +39,40 @@ protected function tearDown()
rmdir($this->object->getConfigDir());
}

public function testGetInstance()
{
Wrapper::set("custom");
Wrapper::getCurrent()->setShellCallback(
function() {
return 'upstart';
}
);
$r = \Respect\Daemon\Manager::getAdapter();
$this->assertType('Respect\Daemon\Adapters\Upstart', $r);
}

public function testRunsOnEnvironment()
{
Wrapper::set("custom");
Wrapper::getCurrent()->setShellCallback(
function() {
return 'upstart';
}
);
$this->assertTrue(Upstart::runsOnEnvironment());
}

public function testRunsOnEnvironmentFalse()
{
Wrapper::set("custom");
Wrapper::getCurrent()->setShellCallback(
function() {
return 'command not found';
}
);
$this->assertFalse(Upstart::runsOnEnvironment());
}

public function testRegister()
{
$job = new Job;
Expand Down

0 comments on commit 330967c

Please sign in to comment.