Skip to content

Commit

Permalink
Fix backward compatibility of LeagueContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Nov 28, 2023
1 parent e823052 commit 1411036
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:

- name: Install third-party packages not for PHP 5.3
if: ${{ matrix.php-versions != '5.3' }}
run: composer require filp/whoops nikic/fast-route phroute/phroute rdlowrey/auryn zendframework/zend-diactoros http-interop/http-middleware:^0.4.1 league/container:~2.4 twig/twig:~1.4 --dev
run: composer require filp/whoops league/container nikic/fast-route phroute/phroute rdlowrey/auryn zendframework/zend-diactoros http-interop/http-middleware:^0.4.1 twig/twig:~1.4 --dev

- name: Install third-party packages for PHP 7.1
if: ${{ matrix.php-versions == '7.1' }}
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to `Slytherin` will be documented in this file.
- Forward slash (`\\`) not required if setting `$namespace` in `Router`
- Checking of available instances in `Container::value`
- If `ServerRequestInterface` is an argument with a middleware
- Backward compatibility for `LeagueContainer::set`

## [0.9.6](https://github.com/rougin/slytherin/compare/v0.9.5...v0.9.6) - 2023-11-16

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ Please see [CHANGELOG][link-changelog] for more information what has changed rec
## Testing

``` bash
$ composer require filp/whoops nikic/fast-route phroute/phroute rdlowrey/auryn zendframework/zend-diactoros http-interop/http-middleware:^0.4.1 league/container:~2.4 twig/twig:~1.4 zendframework/zend-stratigility:~2.0 --dev
$ composer require filp/whoops league/container nikic/fast-route phroute/phroute rdlowrey/auryn zendframework/zend-diactoros http-interop/http-middleware:^0.4.1 twig/twig:~1.4 --dev
$ composer test
```

Expand Down
37 changes: 19 additions & 18 deletions src/Container/LeagueContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,33 @@
*/
class LeagueContainer extends BaseContainer implements ContainerInterface
{
/**
* Finds an entry of the container by its identifier and returns it.
*
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \Psr\Container\ContainerExceptionInterface
*
* @param string $id
* @param array<string, mixed> $arguments
* @return mixed
*/
public function get($id, array $arguments = array())
{
return parent::get($id, $arguments);
}

/**
* Sets a new instance to the container.
*
* @param string $id
* @param mixed $concrete
* @param boolean $share
* @param boolean $shared
* @return self
*/
public function set($id, $concrete, $share = false)
public function set($id, $concrete, $shared = false)
{
$this->add($id, $concrete, $share);
// Backward compatibility on versions >=2.0 ---
$exists = method_exists($this, 'addShared');

if ($shared && $exists)
{
/** @var callable */
$class = array($this, 'addShared');

$params = array($id, $concrete);

call_user_func_array($class, $params);

return $this;
}
// --------------------------------------------

$this->add($id, $concrete, $shared);

return $this;
}
Expand Down
10 changes: 7 additions & 3 deletions tests/IoC/League/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,13 @@ public function testSetMethod()
*/
public function testGetMethod()
{
$this->container->add($this->class)
->withArgument(new NewClass)
->withArgument(new AnotherClass);
// Should only used methods found in ContainerInterface ---
// $this->container->add($this->class)
// ->withArgument(new NewClass)
// ->withArgument(new AnotherClass);
// --------------------------------------------------------

$this->container->set($this->class, $this->instance);

$this->assertEquals($this->instance, $this->container->get($this->class));
}
Expand Down
12 changes: 7 additions & 5 deletions tests/Testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ class Testcase extends Legacy
{
public function setExpectedException($exception)
{
/** @var callable */
$class = array($this, 'setExpectedException');

if (method_exists($this, 'expectException'))
{
$this->expectException($exception);
}
else
{
parent::setExpectedException($exception);
/** @var callable */
$class = array($this, 'expectException');
}

call_user_func($class, $exception);
}
}

0 comments on commit 1411036

Please sign in to comment.