Skip to content

Commit

Permalink
Resolve #31 : Created universal namespace for keeping runtime contain…
Browse files Browse the repository at this point in the history
…er definitions
  • Loading branch information
khelle committed Sep 27, 2016
1 parent 04ae5af commit a172d16
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
40 changes: 30 additions & 10 deletions src/Root/Runtime/Boot/ProcessBoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Kraken\Runtime\Container\Process\ProcessController;
use Kraken\Runtime\RuntimeContainerInterface;
use Kraken\Throwable\Exception\Logic\InstantiationException;
use Kraken\Util\Support\StringSupport;
use Exception;
use ReflectionClass;

class ProcessBoot
Expand All @@ -20,7 +22,7 @@ class ProcessBoot
protected $controllerParams;

/**
* @var string
* @var string|string[]
*/
protected $controllerClass;

Expand All @@ -38,7 +40,10 @@ public function __construct(ProcessController $runtimeController = null)

$this->runtimeController = ($runtimeController !== null) ? $runtimeController : new ProcessController($loader);
$this->controllerParams = [];
$this->controllerClass = '\\%prefix%\\Process\\%name%\\%name%Container';
$this->controllerClass = [
'\\%prefix%\\Process\\Container\\%name%\\%name%Container',
'\\%prefix%\\Runtime\\Container\\%name%\\%name%Container'
];
$this->params = [
'prefix' => 'Kraken',
'name' => 'Undefined'
Expand All @@ -57,12 +62,12 @@ public function __destruct()
}

/**
* @param string $class
* @param string|string[] $class
* @return ProcessBoot
*/
public function controller($class)
{
$this->controllerClass = $class;
$this->controllerClass = (array) $class;

return $this;
}
Expand Down Expand Up @@ -92,16 +97,31 @@ public function params($params)
/**
* @param string $path
* @return RuntimeContainerInterface
* @throws Exception
*/
public function boot($path)
{
$controllerClass = '';
$controllerClassFound = false;

foreach ($this->controllerClass as $controllerClass)
{
$controllerClass = StringSupport::parametrize($controllerClass, $this->params);

if (class_exists($controllerClass))
{
$controllerClassFound = true;
break;
}
}

if (!$controllerClassFound)
{
throw new InstantiationException('Runtime class not found');
}

$controller = (new ReflectionClass($controllerClass))->newInstanceArgs($this->controllerParams);
$datapath = realpath($path);
$controller = (new ReflectionClass(
StringSupport::parametrize($this->controllerClass, $this->params)
))
->newInstanceArgs(
array_merge($this->controllerParams)
);

if (file_exists($datapath . '/bootstrap/' . $controller->getName() . '/bootstrap.php'))
{
Expand Down
40 changes: 30 additions & 10 deletions src/Root/Runtime/Boot/ThreadBoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use Kraken\Runtime\Container\Thread\ThreadController;
use Kraken\Runtime\RuntimeContainerInterface;
use Kraken\Throwable\Exception\Logic\InstantiationException;
use Kraken\Util\Support\StringSupport;
use Exception;
use ReflectionClass;

class ThreadBoot
Expand All @@ -20,7 +22,7 @@ class ThreadBoot
protected $controllerParams;

/**
* @var string
* @var string|string[]
*/
protected $controllerClass;

Expand All @@ -38,7 +40,10 @@ public function __construct(ThreadController $threadController = null)

$this->runtimeController = ($threadController !== null) ? $threadController : new ThreadController($loader);
$this->controllerParams = [];
$this->controllerClass = '\\%prefix%\\Thread\\%name%\\%name%Container';
$this->controllerClass = [
'\\%prefix%\\Thread\\Container\\%name%\\%name%Container',
'\\%prefix%\\Runtime\\Container\\%name%\\%name%Container'
];
$this->params = [
'prefix' => 'Kraken',
'name' => 'Undefined'
Expand All @@ -57,12 +62,12 @@ public function __destruct()
}

/**
* @param string $class
* @param string|string[] $class
* @return ThreadBoot
*/
public function controller($class)
{
$this->controllerClass = $class;
$this->controllerClass = (array) $class;

return $this;
}
Expand Down Expand Up @@ -92,16 +97,31 @@ public function params($params)
/**
* @param string $path
* @return RuntimeContainerInterface
* @throws Exception
*/
public function boot($path)
{
$controllerClass = '';
$controllerClassFound = false;

foreach ($this->controllerClass as $controllerClass)
{
$controllerClass = StringSupport::parametrize($controllerClass, $this->params);

if (class_exists($controllerClass))
{
$controllerClassFound = true;
break;
}
}

if (!$controllerClassFound)
{
throw new InstantiationException('Runtime class not found');
}

$controller = (new ReflectionClass($controllerClass))->newInstanceArgs($this->controllerParams);
$datapath = realpath($path);
$controller = (new ReflectionClass(
StringSupport::parametrize($this->controllerClass, $this->params)
))
->newInstanceArgs(
array_merge($this->controllerParams)
);

if (file_exists($datapath . '/bootstrap/' . $controller->getName() . '/bootstrap.php'))
{
Expand Down

0 comments on commit a172d16

Please sign in to comment.