Permalink
Browse files

Manager::getInstance() for automatic adapter selection, improvements …

…on environment detection. Now depends on Respect\Env a little bit
  • Loading branch information...
1 parent a31dc6c commit 330967c52776aa555f599dfa7fda02cde5e39aa4 @alganet committed Oct 5, 2010
@@ -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()
@@ -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;
+ }
+ }
+ }
+ }
+
}
@@ -5,6 +5,7 @@
use Respect\Daemon\Job;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
+use Respect\Env\Wrapper;
class UpstartTest extends \PHPUnit_Framework_TestCase
{
@@ -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()
@@ -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;

0 comments on commit 330967c

Please sign in to comment.