Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1ca6b4

Browse files
it was created the More namespace and append its patterns
1 parent fc3b6a1 commit c1ca6b4

12 files changed

+114
-111
lines changed

More/Delegation/Test/DelegationTest.php renamed to More/Delegation/DelegationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace DesignPatterns\Tests\Delegation;
3+
namespace DesignPatterns\More\Delegation;
44

5-
use DesignPatterns\Delegation;
5+
use DesignPatterns\More\Delegation;
66

77
/**
88
* DelegationTest tests the delegation pattern

More/Delegation/JuniorDeveloper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace DesignPatterns\Delegation;
3+
namespace DesignPatterns\More\Delegation;
44

55
/**
66
* Class JuniorDeveloper

More/Delegation/TeamLead.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace DesignPatterns\Delegation;
3+
namespace DesignPatterns\More\Delegation;
44

55
/**
66
* Class TeamLead

More/Delegation/Usage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace DesignPatterns\Delegation;
3+
namespace DesignPatterns\More\Delegation;
44

55
// instantiate TeamLead and appoint to assistants JuniorDeveloper
66
$teamLead = new TeamLead(new JuniorDeveloper());
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
22

3-
namespace DesignPatterns\ServiceLocator;
3+
namespace DesignPatterns\More\ServiceLocator;
44

55
class DatabaseService implements DatabaseServiceInterface
66
{
7-
8-
}
7+
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
22

3-
namespace DesignPatterns\ServiceLocator;
3+
namespace DesignPatterns\More\ServiceLocator;
44

55
interface DatabaseServiceInterface
66
{
7-
8-
}
7+
}

More/ServiceLocator/LogService.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
22

3-
namespace DesignPatterns\ServiceLocator;
3+
namespace DesignPatterns\More\ServiceLocator;
44

55
class LogService implements LogServiceInterface
66
{
7-
8-
}
7+
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
22

3-
namespace DesignPatterns\ServiceLocator;
3+
namespace DesignPatterns\More\ServiceLocator;
44

55
interface LogServiceInterface
66
{
7-
8-
}
7+
}

More/ServiceLocator/ServiceLocator.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace DesignPatterns\ServiceLocator;
3+
namespace DesignPatterns\More\ServiceLocator;
44

55
class ServiceLocator implements ServiceLocatorInterface
66
{
@@ -27,17 +27,17 @@ class ServiceLocator implements ServiceLocatorInterface
2727

2828
public function __construct()
2929
{
30-
$this->services = array();
30+
$this->services = array();
3131
$this->instantiated = array();
32-
$this->shared = array();
32+
$this->shared = array();
3333
}
3434

3535
/**
3636
* Registers a service with specific interface.
3737
*
38-
* @param string $interface
38+
* @param string $interface
3939
* @param string|object $service
40-
* @param bool $share
40+
* @param bool $share
4141
*/
4242
public function add($interface, $service, $share = true)
4343
{
@@ -47,11 +47,11 @@ public function add($interface, $service, $share = true)
4747
* in the future even if you will change the service implementation.
4848
*/
4949

50-
if(is_object($service) && $share)
50+
if (is_object($service) && $share) {
5151
$this->instantiated[$interface] = $service;
52-
52+
}
5353
$this->services[$interface] = (is_object($service) ? get_class($service) : $service);
54-
$this->shared[$interface] = $share;
54+
$this->shared[$interface] = $share;
5555
}
5656

5757
/**
@@ -76,8 +76,9 @@ public function has($interface)
7676
public function get($interface)
7777
{
7878
// Retrieves the instance if it exists and it is shared
79-
if(isset($this->instantiated[$interface]) && $this->shared[$interface])
79+
if (isset($this->instantiated[$interface]) && $this->shared[$interface]) {
8080
return $this->instantiated[$interface];
81+
}
8182

8283
// otherwise gets the service registered.
8384
$service = $this->services[$interface];
@@ -97,9 +98,9 @@ public function get($interface)
9798
$object = new $service();
9899

99100
// and saves it if the service must be shared.
100-
if($this->shared[$interface])
101+
if ($this->shared[$interface]) {
101102
$this->instantiated[$interface] = $object;
102-
103+
}
103104
return $object;
104105
}
105-
}
106+
}

More/ServiceLocator/ServiceLocatorInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace DesignPatterns\ServiceLocator;
3+
namespace DesignPatterns\More\ServiceLocator;
44

5-
interface ServiceLocatorInterface
5+
interface ServiceLocatorInterface
66
{
77
/**
88
* Checks if a service is registered.
@@ -21,4 +21,4 @@ public function has($interface);
2121
* @return mixed
2222
*/
2323
public function get($interface);
24-
}
24+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace DesignPatterns\More\ServiceLocator;
4+
5+
use DesignPatterns\More\ServiceLocator\DatabaseService;
6+
use DesignPatterns\More\ServiceLocator\LogService;
7+
use DesignPatterns\More\ServiceLocator\ServiceLocator;
8+
use \PHPUnit_Framework_TestCase as TestCase;
9+
10+
class ServiceLocatorTest extends TestCase
11+
{
12+
/**
13+
* @var LogService
14+
*/
15+
private $logService;
16+
17+
/**
18+
* @var DatabaseService
19+
*/
20+
private $databaseService;
21+
22+
/**
23+
* @var ServiceLocator
24+
*/
25+
private $serviceLocator;
26+
27+
public function setUp()
28+
{
29+
$this->serviceLocator = new ServiceLocator();
30+
31+
$this->logService = new LogService();
32+
$this->databaseService = new DatabaseService();
33+
}
34+
35+
public function testHasServices()
36+
{
37+
$this->serviceLocator->add(
38+
'DesignPatterns\More\ServiceLocator\LogServiceInterface',
39+
$this->logService
40+
);
41+
$this->serviceLocator->add(
42+
'DesignPatterns\More\ServiceLocator\DatabaseServiceInterface',
43+
$this->databaseService
44+
);
45+
46+
$this->assertTrue($this->serviceLocator->has('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
47+
$this->assertTrue($this->serviceLocator->has('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
48+
49+
$this->assertFalse($this->serviceLocator->has('DesignPatterns\More\ServiceLocator\FakeServiceInterface'));
50+
}
51+
52+
public function testServicesWithObject()
53+
{
54+
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->logService);
55+
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->databaseService);
56+
57+
$this->assertSame($this->logService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
58+
$this->assertSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
59+
}
60+
61+
public function testServicesWithClass()
62+
{
63+
$this->serviceLocator
64+
->add('DesignPatterns\More\ServiceLocator\LogServiceInterface', get_class($this->logService));
65+
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', get_class($this->databaseService));
66+
67+
$this->assertNotSame($this->logService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
68+
$this->assertInstanceOf('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
69+
70+
$this->assertNotSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
71+
$this->assertInstanceOf('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
72+
}
73+
74+
public function testServicesNotShared()
75+
{
76+
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->logService, false);
77+
$this->serviceLocator->add('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->databaseService, false);
78+
79+
$this->assertNotSame($this->logService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
80+
$this->assertInstanceOf('DesignPatterns\More\ServiceLocator\LogServiceInterface', $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\LogServiceInterface'));
81+
82+
$this->assertNotSame($this->databaseService, $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
83+
$this->assertInstanceOf('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface', $this->serviceLocator->get('DesignPatterns\More\ServiceLocator\DatabaseServiceInterface'));
84+
}
85+
}

More/ServiceLocator/Test/ServiceLocatorTest.php

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)