it was created the More namespace and append its patterns

This commit is contained in:
Antonio Spinelli
2014-04-16 18:12:52 -03:00
parent fc3b6a1608
commit c1ca6b42e2
12 changed files with 114 additions and 111 deletions

View File

@@ -1,8 +1,8 @@
<?php <?php
namespace DesignPatterns\Tests\Delegation; namespace DesignPatterns\More\Delegation;
use DesignPatterns\Delegation; use DesignPatterns\More\Delegation;
/** /**
* DelegationTest tests the delegation pattern * DelegationTest tests the delegation pattern

View File

@@ -1,6 +1,6 @@
<?php <?php
namespace DesignPatterns\Delegation; namespace DesignPatterns\More\Delegation;
/** /**
* Class JuniorDeveloper * Class JuniorDeveloper

View File

@@ -1,6 +1,6 @@
<?php <?php
namespace DesignPatterns\Delegation; namespace DesignPatterns\More\Delegation;
/** /**
* Class TeamLead * Class TeamLead

View File

@@ -1,6 +1,6 @@
<?php <?php
namespace DesignPatterns\Delegation; namespace DesignPatterns\More\Delegation;
// instantiate TeamLead and appoint to assistants JuniorDeveloper // instantiate TeamLead and appoint to assistants JuniorDeveloper
$teamLead = new TeamLead(new JuniorDeveloper()); $teamLead = new TeamLead(new JuniorDeveloper());

View File

@@ -1,8 +1,7 @@
<?php <?php
namespace DesignPatterns\ServiceLocator; namespace DesignPatterns\More\ServiceLocator;
class DatabaseService implements DatabaseServiceInterface class DatabaseService implements DatabaseServiceInterface
{ {
} }

View File

@@ -1,8 +1,7 @@
<?php <?php
namespace DesignPatterns\ServiceLocator; namespace DesignPatterns\More\ServiceLocator;
interface DatabaseServiceInterface interface DatabaseServiceInterface
{ {
} }

View File

@@ -1,8 +1,7 @@
<?php <?php
namespace DesignPatterns\ServiceLocator; namespace DesignPatterns\More\ServiceLocator;
class LogService implements LogServiceInterface class LogService implements LogServiceInterface
{ {
} }

View File

@@ -1,8 +1,7 @@
<?php <?php
namespace DesignPatterns\ServiceLocator; namespace DesignPatterns\More\ServiceLocator;
interface LogServiceInterface interface LogServiceInterface
{ {
} }

View File

@@ -1,6 +1,6 @@
<?php <?php
namespace DesignPatterns\ServiceLocator; namespace DesignPatterns\More\ServiceLocator;
class ServiceLocator implements ServiceLocatorInterface class ServiceLocator implements ServiceLocatorInterface
{ {
@@ -27,17 +27,17 @@ class ServiceLocator implements ServiceLocatorInterface
public function __construct() public function __construct()
{ {
$this->services = array(); $this->services = array();
$this->instantiated = array(); $this->instantiated = array();
$this->shared = array(); $this->shared = array();
} }
/** /**
* Registers a service with specific interface. * Registers a service with specific interface.
* *
* @param string $interface * @param string $interface
* @param string|object $service * @param string|object $service
* @param bool $share * @param bool $share
*/ */
public function add($interface, $service, $share = true) public function add($interface, $service, $share = true)
{ {
@@ -47,11 +47,11 @@ class ServiceLocator implements ServiceLocatorInterface
* in the future even if you will change the service implementation. * in the future even if you will change the service implementation.
*/ */
if(is_object($service) && $share) if (is_object($service) && $share) {
$this->instantiated[$interface] = $service; $this->instantiated[$interface] = $service;
}
$this->services[$interface] = (is_object($service) ? get_class($service) : $service); $this->services[$interface] = (is_object($service) ? get_class($service) : $service);
$this->shared[$interface] = $share; $this->shared[$interface] = $share;
} }
/** /**
@@ -76,8 +76,9 @@ class ServiceLocator implements ServiceLocatorInterface
public function get($interface) public function get($interface)
{ {
// Retrieves the instance if it exists and it is shared // Retrieves the instance if it exists and it is shared
if(isset($this->instantiated[$interface]) && $this->shared[$interface]) if (isset($this->instantiated[$interface]) && $this->shared[$interface]) {
return $this->instantiated[$interface]; return $this->instantiated[$interface];
}
// otherwise gets the service registered. // otherwise gets the service registered.
$service = $this->services[$interface]; $service = $this->services[$interface];
@@ -97,9 +98,9 @@ class ServiceLocator implements ServiceLocatorInterface
$object = new $service(); $object = new $service();
// and saves it if the service must be shared. // and saves it if the service must be shared.
if($this->shared[$interface]) if ($this->shared[$interface]) {
$this->instantiated[$interface] = $object; $this->instantiated[$interface] = $object;
}
return $object; return $object;
} }
} }

View File

@@ -1,6 +1,6 @@
<?php <?php
namespace DesignPatterns\ServiceLocator; namespace DesignPatterns\More\ServiceLocator;
interface ServiceLocatorInterface interface ServiceLocatorInterface
{ {

View File

@@ -0,0 +1,85 @@
<?php
namespace DesignPatterns\More\ServiceLocator;
use DesignPatterns\More\ServiceLocator\DatabaseService;
use DesignPatterns\More\ServiceLocator\LogService;
use DesignPatterns\More\ServiceLocator\ServiceLocator;
use \PHPUnit_Framework_TestCase as TestCase;
class ServiceLocatorTest extends TestCase
{
/**
* @var LogService
*/
private $logService;
/**
* @var DatabaseService
*/
private $databaseService;
/**
* @var ServiceLocator
*/
private $serviceLocator;
public function setUp()
{
$this->serviceLocator = new ServiceLocator();
$this->logService = new LogService();
$this->databaseService = new DatabaseService();
}
public function testHasServices()
{
$this->serviceLocator->add(
'DesignPatterns\More\ServiceLocator\LogServiceInterface',
$this->logService
);
$this->serviceLocator->add(
'DesignPatterns\More\ServiceLocator\DatabaseServiceInterface',
$this->databaseService
);
$this->assertTrue($this->serviceLocator->has('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
$this->assertTrue($this->serviceLocator->has('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
$this->assertFalse($this->serviceLocator->has('DesignPatterns\More\ServiceLocator\FakeServiceInterface'));
}
public function testServicesWithObject()
{
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->logService);
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->databaseService);
$this->assertSame($this->logService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
$this->assertSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
}
public function testServicesWithClass()
{
$this->serviceLocator
->add('DesignPatterns\More\ServiceLocator\LogServiceInterface', get_class($this->logService));
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', get_class($this->databaseService));
$this->assertNotSame($this->logService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
$this->assertInstanceOf('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
$this->assertNotSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
$this->assertInstanceOf('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
}
public function testServicesNotShared()
{
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->logService, false);
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->databaseService, false);
$this->assertNotSame($this->logService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
$this->assertInstanceOf('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
$this->assertNotSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
$this->assertInstanceOf('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
}
}

View File

@@ -1,79 +0,0 @@
<?php
namespace DesignPatterns\Tests\ServiceLocator;
use DesignPatterns\ServiceLocator\DatabaseService;
use DesignPatterns\ServiceLocator\LogService;
use DesignPatterns\ServiceLocator\ServiceLocator;
use \PHPUnit_Framework_TestCase as TestCase;
class ServiceLocatorTest extends TestCase
{
/**
* @var LogService
*/
private $logService;
/**
* @var DatabaseService
*/
private $databaseService;
/**
* @var ServiceLocator
*/
private $serviceLocator;
public function setUp()
{
$this->serviceLocator = new ServiceLocator();
$this->logService = new LogService();
$this->databaseService = new DatabaseService();
}
public function testHasServices()
{
$this->serviceLocator->add('DesignPatterns\ServiceLocator\LogServiceInterface', $this->logService);
$this->serviceLocator->add('DesignPatterns\ServiceLocator\DatabaseServiceInterface', $this->databaseService);
$this->assertTrue($this->serviceLocator->has('DesignPatterns\ServiceLocator\LogServiceInterface'));
$this->assertTrue($this->serviceLocator->has('DesignPatterns\ServiceLocator\DatabaseServiceInterface'));
$this->assertFalse($this->serviceLocator->has('DesignPatterns\ServiceLocator\FakeServiceInterface'));
}
public function testServicesWithObject()
{
$this->serviceLocator->add('DesignPatterns\ServiceLocator\LogServiceInterface', $this->logService);
$this->serviceLocator->add('DesignPatterns\ServiceLocator\DatabaseServiceInterface', $this->databaseService);
$this->assertSame($this->logService, $this->serviceLocator->get('DesignPatterns\ServiceLocator\LogServiceInterface'));
$this->assertSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\ServiceLocator\DatabaseServiceInterface'));
}
public function testServicesWithClass()
{
$this->serviceLocator->add('DesignPatterns\ServiceLocator\LogServiceInterface', get_class($this->logService));
$this->serviceLocator->add('DesignPatterns\ServiceLocator\DatabaseServiceInterface', get_class($this->databaseService));
$this->assertNotSame($this->logService, $this->serviceLocator->get('DesignPatterns\ServiceLocator\LogServiceInterface'));
$this->assertInstanceOf('DesignPatterns\ServiceLocator\LogServiceInterface', $this->serviceLocator->get('DesignPatterns\ServiceLocator\LogServiceInterface'));
$this->assertNotSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\ServiceLocator\DatabaseServiceInterface'));
$this->assertInstanceOf('DesignPatterns\ServiceLocator\DatabaseServiceInterface', $this->serviceLocator->get('DesignPatterns\ServiceLocator\DatabaseServiceInterface'));
}
public function testServicesNotShared()
{
$this->serviceLocator->add('DesignPatterns\ServiceLocator\LogServiceInterface', $this->logService, false);
$this->serviceLocator->add('DesignPatterns\ServiceLocator\DatabaseServiceInterface', $this->databaseService, false);
$this->assertNotSame($this->logService, $this->serviceLocator->get('DesignPatterns\ServiceLocator\LogServiceInterface'));
$this->assertInstanceOf('DesignPatterns\ServiceLocator\LogServiceInterface', $this->serviceLocator->get('DesignPatterns\ServiceLocator\LogServiceInterface'));
$this->assertNotSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\ServiceLocator\DatabaseServiceInterface'));
$this->assertInstanceOf('DesignPatterns\ServiceLocator\DatabaseServiceInterface', $this->serviceLocator->get('DesignPatterns\ServiceLocator\DatabaseServiceInterface'));
}
}