Skip to content

Commit

Permalink
adf
Browse files Browse the repository at this point in the history
  • Loading branch information
theahmadzai committed Apr 7, 2017
1 parent d866c23 commit 658d5f6
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 22 deletions.
58 changes: 44 additions & 14 deletions Immortal/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,6 @@ class Container implements ContainerInterface
private $parameters;
private $serviceStore;

private $defaultSettings = [
'setting1' => 'value1',
'setting2' => 'value2',
'setting3' => 'value3',
];

private $settings;

public function __construct(array $services = [], array $parameters = [])
{
$this->services = $services;
Expand All @@ -28,7 +20,7 @@ public function __construct(array $services = [], array $parameters = [])
public function get($name)
{
if (!$this->has($name)) {
throw new ServiceNotFoundException('Service not found: ' . $name);
throw new NotFoundException('Service not found: ' . $name);
}

if (!isset($this->serviceStore[$name])) {
Expand All @@ -49,7 +41,7 @@ public function getParameter($name)
$context = $this->parameters;
while (null !== ($token = array_shift($tokens))) {
if (!isset($context[$token])) {
throw new ParameterNotFoundException('Parameter not found: ' . $name);
throw new NotFoundException('Parameter not found: ' . $name);
}
$context = $context[$token];
}
Expand All @@ -60,7 +52,7 @@ public function hasParameter($name)
{
try {
$this->getParameter($name);
} catch (ParameterNotFoundException $exception) {
} catch (NotFoundException $exception) {
return false;
}
return true;
Expand All @@ -80,12 +72,50 @@ private function createService()

$entry['lock'] = true;

$arguments = isset($entry['arguments']) ? $this->resolveArguments($name, $entry['arguments']) : [];

$reflector = new \ReflectionClass($entry['class']);
$service = $reflector->newInstanceArgs($arguments);

if (isset($entry['calls'])) {
$this->initializeService($service, $name, $entry['calls']);
}

return $services;
}

private function resolveArguments($name, array $argumentDefinitions)
{
$arguments = [];

foreach ($argumentDefinitions as $argumentDefinition) {
if ($argumentDefinition instanceof ServiceReference) {
$argumentServiceName = $argumentDefinition->getName();
$arguments[] = $this->get($argumentServiceName);
} elseif ($argumentDefinition instanceof ParameterReference) {
$argumentParameterName = $argumentDefinition->getName();
$arguments[] = $this->getParameter($argumentParameterName);
} else {
$arguments[] = $argumentDefinition;
}
}

return $arguments;
}

public function defaultServices($userSettings)
private function initializeService($service, $name, array $callDefinitions)
{
$defaultSettings = $this->defaultSettings;
$this->settings = array_merge($defaultSettings, $userSettings);
foreach ($callDefinitions as $callDefinition) {
if (!is_array($callDefinition) || !isset($callDefinition['method'])) {
throw new ContainerException($name . ' service calls must be arrays containing a \'method\' key');
} elseif (!is_callable([$service, $callDefinition['method']])) {
throw new ContainerException($name . ' service asks for call to uncallable method: ' . $callDefinition['method']);
}

$arguments = isset($callDefinition['arguments']) ? $this->resolveArguments($name, $callDefinition['arguments']) : [];

call_user_func_array([$service, $callDefinition['method']], $arguments);
}
}

public function __get($name)
Expand Down
4 changes: 2 additions & 2 deletions Immortal/Container/Exception/ContainerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Immortal\Container\Exception;

use Immortal\Exception\ExceptionHandler;
use Psr\Container\ContainerExceptionInterface as PsrContainerException;

class ContainerException extends ExceptionHandler
class ContainerException extends ExceptionHandler implements PsrContainerException
{

}
10 changes: 10 additions & 0 deletions Immortal/Container/Exception/NotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Immortal\Container\Exception;

use Immortal\Exception\ExceptionHandler;
use Psr\Container\NotFoundExceptionInterface as PsrNotFoundException;

class NotFoundException extends ExceptionHandler implements PsrNotFoundException
{
}
Empty file.
7 changes: 3 additions & 4 deletions Immortal/Container/Interfaces/ContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace Immortal\Container\Interfaces;

interface ContainerInterface
{
public function get($id);
use Psr\Container\ContainerInterface as PsrContainerInterface;

public function has($id);
interface ContainerInterface extends PsrContainerInterface
{
}
18 changes: 18 additions & 0 deletions Immortal/Container/Reference/AbstractReference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Immortal\Container\Reference;

abstract class AbstractReference
{
private $name;

public function __construct($name)
{
$this->name = $name;
}

public function getName()
{
return $this->name;
}
}
7 changes: 7 additions & 0 deletions Immortal/Container/Reference/ParameterReference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Immortal\Container\Reference;

class ParameterReference extends AbstractReference
{
}
7 changes: 7 additions & 0 deletions Immortal/Container/Reference/ServiceReference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Immortal\Container\Reference;

class ServiceReference extends AbstractReference
{
}
8 changes: 8 additions & 0 deletions Immortal/Container/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Immortal\Container\Service;

class Service
{

}
1 change: 1 addition & 0 deletions Immortal/Router/Exception/RoutingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
1 change: 1 addition & 0 deletions Immortal/Router/Exception/ServiceNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
1 change: 1 addition & 0 deletions Immortal/Router/Exception/UrlException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
1 change: 0 additions & 1 deletion Immortal/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class Router extends AbstractRouter

public function __construct()
{

}

public function get(string $pattern, Closure $function): Router
Expand Down
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@
"source": "https://github.com/theahmadzai/immortal-framework"
},
"require": {
"php": ">=5.6.0"
"php": ">=5.6.0",
"psr/log": "@stable",
"psr/cache": "@stable",
"psr/http-message": "@stable",
"psr/container": "@stable",
"psr/link": "@stable",
"psr/simple-cache": "@stable"
},
"require-dev": {
"phpunit/phpunit": "@stable",
Expand Down

0 comments on commit 658d5f6

Please sign in to comment.