[go: nahoru, domu]

Skip to content
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

Merged
merged 6 commits into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"phpmyadmin/motranslator": "^3.0",
"phpmyadmin/shapefile": "^2.0",
"phpseclib/phpseclib": "^2.0",
"google/recaptcha": "^1.1"
"google/recaptcha": "^1.1",
"psr/container": "^1.0"
},
"conflict": {
"tecnickcom/tcpdf": "<6.2"
Expand Down
50 changes: 45 additions & 5 deletions libraries/di/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
Copy link
Contributor

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...

*
* @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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really don't get the code here:

  • Why is there try { throw } catch {} block at all?
  • The echo is there for something else than debugging purposes?


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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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]);

}

/**
Expand Down
21 changes: 21 additions & 0 deletions libraries/di/ContainerException.php
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
{

}
20 changes: 20 additions & 0 deletions libraries/di/NotFoundException.php
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
{

}
93 changes: 93 additions & 0 deletions test/classes/di/ContainerTest.php
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'));
Copy link
Contributor

Choose a reason for hiding this comment

The 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'));
}
}