-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement PSR-11 #13060
Implement PSR-11 #13060
Changes from 4 commits
4e94a1c
9839caf
fafdc06
e745255
b0121f9
48897b3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,14 +7,15 @@ | |
*/ | ||
namespace PMA\libraries\di; | ||
|
||
use Psr\Container\ContainerInterface; | ||
|
||
/** | ||
* Class Container | ||
* | ||
* @package PMA\libraries\di | ||
*/ | ||
class Container | ||
class Container implements ContainerInterface | ||
{ | ||
|
||
/** | ||
* @var Item[] $content | ||
*/ | ||
|
@@ -46,19 +47,58 @@ public function __construct(Container $base = null) | |
* @param string $name Name | ||
* @param array $params Parameters | ||
* | ||
* @throws NotFoundException No entry was found for **this** identifier. | ||
* @throws ContainerException Error while retrieving the entry. | ||
* | ||
* @return mixed | ||
*/ | ||
public function get($name, $params = array()) | ||
{ | ||
try { | ||
if (!$this->has($name)) { | ||
throw new NotFoundException("No entry was found for $name identifier."); | ||
} | ||
} catch (NotFoundException $e) { | ||
echo $e->getMessage(); | ||
return null; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really don't get the code here:
|
||
|
||
try { | ||
if (isset($this->content[$name])) { | ||
return $this->content[$name]->get($params); | ||
} else if (isset($GLOBALS[$name])) { | ||
return $GLOBALS[$name]; | ||
} else { | ||
throw new ContainerException("Error while retrieving the entry."); | ||
} | ||
} catch (ContainerException $e) { | ||
echo $e->getMessage(); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Returns true if the container can return an entry for the given identifier. | ||
* Returns false otherwise. | ||
* | ||
* `has($name)` returning true does not mean that `get($name)` will not throw an exception. | ||
* It does however mean that `get($name)` will not throw a `NotFoundException`. | ||
* | ||
* @param string $name Identifier of the entry to look for. | ||
* | ||
* @return bool | ||
*/ | ||
public function has($name) | ||
{ | ||
if (isset($this->content[$name])) { | ||
return $this->content[$name]->get($params); | ||
return true; | ||
} | ||
|
||
if (isset($GLOBALS[$name])) { | ||
return $GLOBALS[$name]; | ||
return true; | ||
} | ||
|
||
return null; | ||
return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably better to write whole method as: return isset($this->content[$name]) || isset($GLOBALS[$name]); |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
/* vim: set expandtab sw=4 ts=4 sts=4: */ | ||
/** | ||
* Holds the PMA\libraries\di\ContainerException class | ||
* | ||
* @package PMA | ||
*/ | ||
namespace PMA\libraries\di; | ||
|
||
use Exception; | ||
use Psr\Container\ContainerExceptionInterface; | ||
|
||
/** | ||
* Class ContainerException | ||
* | ||
* @package PMA\libraries\di | ||
*/ | ||
class ContainerException extends Exception implements ContainerExceptionInterface | ||
{ | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
/* vim: set expandtab sw=4 ts=4 sts=4: */ | ||
/** | ||
* Holds the PMA\libraries\di\NotFoundException class | ||
* | ||
* @package PMA | ||
*/ | ||
namespace PMA\libraries\di; | ||
|
||
use Psr\Container\NotFoundExceptionInterface; | ||
|
||
/** | ||
* Class NotFoundException | ||
* | ||
* @package PMA\libraries\di | ||
*/ | ||
class NotFoundException extends ContainerException implements NotFoundExceptionInterface | ||
{ | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
/* vim: set expandtab sw=4 ts=4 sts=4: */ | ||
/** | ||
* Tests for PMA\libraries\di\Container class | ||
* | ||
* @package PhpMyAdmin-test | ||
*/ | ||
|
||
/* | ||
* Include to test. | ||
*/ | ||
require_once 'test/PMATestCase.php'; | ||
|
||
use PMA\libraries\di\Container; | ||
|
||
/** | ||
* Tests for PMA\libraries\di\Container class | ||
* | ||
* @package PhpMyAdmin-test | ||
*/ | ||
class ContainerTest extends PMATestCase | ||
{ | ||
/** | ||
* @access protected | ||
*/ | ||
protected $container; | ||
|
||
/** | ||
* Sets up the fixture. | ||
* This method is called before a test is executed. | ||
* | ||
* @access protected | ||
* @return void | ||
*/ | ||
protected function setUp() | ||
{ | ||
$this->container = new Container(); | ||
} | ||
|
||
/** | ||
* Tears down the fixture. | ||
* This method is called after a test is executed. | ||
* | ||
* @access protected | ||
* @return void | ||
*/ | ||
protected function tearDown() | ||
{ | ||
unset($this->container); | ||
} | ||
|
||
/** | ||
* Test for get | ||
* | ||
* @return void | ||
*/ | ||
public function testGetWithValidEntry() | ||
{ | ||
$this->container->set('name', 'value'); | ||
$this->assertSame('value', $this->container->get('name')); | ||
} | ||
|
||
/** | ||
* Test for get | ||
* @expectedExceptionMessageRegExp #Right.*# | ||
* @return void | ||
*/ | ||
public function testGetWithInvalidEntry() | ||
{ | ||
$this->assertSame(null, $this->container->get('invalid')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO it should throw NotFoundExceptionInterface according to PSR-11. |
||
} | ||
|
||
/** | ||
* Test for has | ||
* | ||
* @return void | ||
*/ | ||
public function testHasReturnsTrueForValidEntry() | ||
{ | ||
$this->container->set('name', 'value'); | ||
$this->assertTrue($this->container->has('name')); | ||
} | ||
|
||
/** | ||
* Test for has | ||
* | ||
* @return void | ||
*/ | ||
public function testHasReturnsFalseForInvalidEntry() | ||
{ | ||
$this->assertFalse($this->container->has('name')); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't seem to throw these as they are always handled inside...