Skip to content

Commit

Permalink
Opportunity use like singletone, part2
Browse files Browse the repository at this point in the history
  • Loading branch information
sapsan4eg committed May 11, 2016
1 parent 3c439b2 commit 0a325ca
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 9 deletions.
15 changes: 13 additions & 2 deletions src/Inject.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,25 @@ public static function instantiation($className, array $parameters = null)
*/
protected static function init(\ReflectionClass $class, $parameters)
{
if (self::$serviceContainer->isSingle($class->getName())) {
if (($object = self::$serviceContainer->getObject($class->getName()))) {
return $object;
}
}

if (false == $class->hasMethod("__construct") || false == $class->getMethod('__construct')->isPublic()) {
$instance = $class->newInstanceWithoutConstructor();
} else {
$instance = $class->newInstanceArgs(self::getParameters($class->getMethod('__construct'), $parameters));
}

return self::fillProperties($instance, $class->getProperties(\ReflectionProperty::IS_PUBLIC));
$object = self::fillProperties($instance, $class->getProperties(\ReflectionProperty::IS_PUBLIC));

if (self::$serviceContainer->isSingle($class->getName())) {
self::$serviceContainer->setObject($class->getName(), $object);
}

return $object;
}

/**
Expand All @@ -74,7 +86,6 @@ public static function method($className, $methodName, array $parameters = null)

throw new InjectException("Inject error: class " . $className . " not exist.");
}

$classCheck = new \ReflectionClass($className);

if ('__construct' == $methodName) {
Expand Down
51 changes: 49 additions & 2 deletions src/ServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,23 @@ class ServiceContainer
protected $objects = [];

/**
* Get class name from container
* @param string $serviceName
* @param string $annotation
* @return null|string
*/
public function getServiceName($serviceName, $annotation = "")
{
return $this->getName($this->getService($serviceName, $annotation));
}

/**
* Return service
* @param $serviceName
* @param string $annotation
* @return null|string|bool
*/
public function getService($serviceName, $annotation = "")
{
if (isset($this->services[$serviceName])) {
if (is_string($this->services[$serviceName])) {
Expand All @@ -40,6 +52,34 @@ public function getServiceName($serviceName, $annotation = "")
return null;
}

/**
* Get name from service
* @param string|array $service
* @return string
*/
protected function getName($service)
{
if (is_string($service)) {
return $service;
}

return $service['name'];
}

public function getSingle($service)
{
if (is_string($service) || empty($service['single']) || true != $service['single']) {
return false;
}

return true;
}

public function isSingle($serviceName)
{
return isset($this->objects[$serviceName]) ? true : false;
}

/**
* Flush all services from ServiceContainer
*/
Expand All @@ -64,8 +104,15 @@ public function bind($interface, $class)
} else {
$classes = [];
foreach ($class as $name => $value) {
if (is_string($name) && !empty($name) && is_string($value) && !empty($value)) {
$classes[$name] = $value;

if (empty($name) || empty($value) || !is_string($name) || (is_array($value) && empty($value['name']))) {
continue;
}

$classes[$name] = $value;

if (!empty($value['single']) && true == $value['single']) {
$this->objects[$value['name']] = null;
}
}

Expand Down
20 changes: 16 additions & 4 deletions tests/InjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ class InjectTest extends PHPUnit_Framework_TestCase
public function setUp()
{
require_once __DIR__ . "/TestClasses/Classes.php";
Inject::bindByArray(["IStart" => ["star" => "Start", "second" => "Starter"], "INext" => "Next"]);
Inject::bindByArray([
"IStart" => ["star" => "Start", "second" => ["name" => "Starter"]],
"INext" => "Next",
"SingleInterface" => ["singles" => ["name" => "Single", "single" => true]],
]);
}

public function testInstantiation()
Expand All @@ -32,8 +36,16 @@ public function testCheckAllInInjected()

public function testCheckInjectParameter()
{
Inject::instantiation("SimpleParameter", ["c" => 1]);
Inject::instantiation("SimpleParameter", ["c" => 1, "d" => new Next()]);
Inject::instantiation("SimpleParameter", ["c" => 1, "d" => 2]);
$this->assertInstanceOf("SimpleParameter", Inject::instantiation("SimpleParameter", ["c" => 1]));
$this->assertInstanceOf("SimpleParameter", Inject::instantiation("SimpleParameter", ["c" => 1, "d" => new Next()]));
$this->assertInstanceOf("SimpleParameter", Inject::instantiation("SimpleParameter", ["c" => 1, "d" => 2]));
}

public function testCheckSingle()
{
$class = Inject::instantiation("SingleInterface", ["id" => 100]);
$this->assertEquals(100, $class->getId());
$class = Inject::instantiation("SingleInterface", ["id" => 200]);
$this->assertEquals(100, $class->getId());
}
}
22 changes: 21 additions & 1 deletion tests/TestClasses/Classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,24 @@ public function __construct($c, \INext $d)
{
$this->d = $d;
}
}
}

interface SingleInterface
{
public function getId();
}

class Single implements SingleInterface
{
protected $id;

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

public function getId()
{
return $this->id;
}
}

0 comments on commit 0a325ca

Please sign in to comment.