Skip to content

Commit

Permalink
deprecate arrayaccess and magic methods
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Sep 9, 2019
1 parent 104e3a8 commit a77b932
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,14 @@ use Jgut\Slim\PHPDI\Configuration;
use Jgut\Slim\PHPDI\ContainerBuilder;
use Psr\Container\ContainerInterface;
use Slim\App;
use Slim\Factory\AppFactory;

$settings = [
'definitions' => '/path/to/definitions/files',
];
$container = ContainerBuilder::build(new Configuration($settings));

$app = $container->get(App::class);
// or $app = AppFactory::createFromContainer($container);
// or $app = \Slim\Factory\AppFactory::createFromContainer($container);

// Register your services if not provided as definitions
$container->set('service_one', function (ContainerInterface $container) {
Expand Down Expand Up @@ -162,10 +161,10 @@ return [
## Migration from 2.x

* Minimum Slim version is now 4.2
* PHP-DI container now provides only the Configuration object used on building the container itself and implementations of the interfaces needed to instantiate an App. Refer to [Slim's documentation](http://www.slimframework.com/docs/v4/)
* Container only provides implementations of the interfaces needed to instantiate an App. Refer to [Slim's documentation](http://www.slimframework.com/docs/v4/)
* You can extract Slim's App directly from container or seed AppFactory from container
* Slim's App is not extended any more
* Service definitions à la Pimple support has been kept but its use is discouraged, use PHP-DI's methods instead
* ArrayAccess and magic methods on default container have been kept but are deprecated, use PSR-11 and PHP-DI's methods instead

## Contributing

Expand Down
41 changes: 38 additions & 3 deletions src/ContainerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private function getRecursive(string $key, array $parent = null)
public function offsetSet($name, $value): void
{
@\trigger_error(
'ArrayAccess set method is deprecated since 3.0, use PHP-DI methods instead.',
'ArrayAccess is deprecated since 3.0, use PSR-11 and PHP-DI methods instead.',
\E_USER_DEPRECATED
);

Expand All @@ -146,6 +146,11 @@ public function offsetSet($name, $value): void
*/
public function offsetGet($name)
{
@\trigger_error(
'ArrayAccess is deprecated since 3.0, use PSR-11 and PHP-DI methods instead.',
\E_USER_DEPRECATED
);

return $this->get($name);
}

Expand All @@ -162,6 +167,11 @@ public function offsetGet($name)
*/
public function offsetExists($name): bool
{
@\trigger_error(
'ArrayAccess is deprecated since 3.0, use PSR-11 and PHP-DI methods instead.',
\E_USER_DEPRECATED
);

return $this->has($name);
}

Expand All @@ -176,7 +186,12 @@ public function offsetExists($name): bool
*/
public function offsetUnset($name): void
{
throw new \RuntimeException('It is not possible to unset container definitions');
@\trigger_error(
'ArrayAccess is deprecated since 3.0, use PSR-11 and PHP-DI methods instead.',
\E_USER_DEPRECATED
);

throw new \RuntimeException('It is not possible to unset a container definitions');
}

/**
Expand All @@ -187,6 +202,11 @@ public function offsetUnset($name): void
*/
public function __set(string $name, $value): void
{
@\trigger_error(
'Magic methods are deprecated since 3.0, use PSR-11 and PHP-DI methods instead.',
\E_USER_DEPRECATED
);

$this->set($name, $value);
}

Expand All @@ -202,6 +222,11 @@ public function __set(string $name, $value): void
*/
public function __get(string $name)
{
@\trigger_error(
'Magic methods are deprecated since 3.0, use PSR-11 and PHP-DI methods instead.',
\E_USER_DEPRECATED
);

return $this->get($name);
}

Expand All @@ -214,6 +239,11 @@ public function __get(string $name)
*/
public function __isset(string $name): bool
{
@\trigger_error(
'Magic methods are deprecated since 3.0, use PSR-11 and PHP-DI methods instead.',
\E_USER_DEPRECATED
);

return $this->has($name);
}

Expand All @@ -228,6 +258,11 @@ public function __isset(string $name): bool
*/
public function __unset(string $name): void
{
throw new \RuntimeException('It is not possible to unset container definitions');
@\trigger_error(
'Magic methods are deprecated since 3.0, use PSR-11 and PHP-DI methods instead.',
\E_USER_DEPRECATED
);

throw new \RuntimeException('It is not possible to unset a container definitions');
}
}
6 changes: 3 additions & 3 deletions src/definitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@
// Replaced by used configuration
Configuration::class => null,

// Replaced by container itself
ContainerInterface::class => null,

ResponseFactoryInterface::class => function (): ResponseFactoryInterface {
return AppFactory::determineResponseFactory();
},

// Replaced by container itself
ContainerInterface::class => null,

CallableResolverInterface::class => function (ContainerInterface $container): CallableResolverInterface {
return new CallableResolver(new InvokerCallableResolver($container));
},
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPDI/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,15 @@ public function testSetterGetter(): void
public function testUnset(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('It is not possible to unset container definitions');
$this->expectExceptionMessage('It is not possible to unset a container definitions');

unset($this->container->foo);
}

public function testUnsetArray(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('It is not possible to unset container definitions');
$this->expectExceptionMessage('It is not possible to unset a container definitions');

unset($this->container['foo']);
}
Expand Down

0 comments on commit a77b932

Please sign in to comment.