Browse Source

On copie mnapoly/simplex chez nous afin de le modifier de ses deprecated en PHP.

Je n’ai pas trouvé de librairie alternative qui soit compat php 7.3 ou 7.2 à 8.1 sans avoir de deprecated.
Du coup, on va le gérer soi-même.
pull/42/head
Matthieu Marcillaud 11 months ago
parent
commit
8824afb318
  1. 8
      composer.json
  2. 95
      composer.lock
  3. 2
      src/Application.php
  4. 370
      src/Container/Container.php
  5. 18
      src/Container/Exception/EntryNotFound.php

8
composer.json

@ -4,15 +4,15 @@
"keywords" : ["spip","cli"],
"minimum-stability": "stable",
"require": {
"php": "^7.3|^8",
"psr/container": "^1.1",
"php": "^7.2|^8",
"symfony/console": "^5.4",
"symfony/finder": "^5.4",
"symfony/process": "^5.4",
"mnapoli/simplex": "^0.5.0"
"symfony/process": "^5.4"
},
"config": {
"platform": {
"php": "7.3.31"
"php": "7.2.34"
}
},
"autoload": {

95
composer.lock generated

@ -4,97 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "a1a118f1920e37adf602971e89ed6b40",
"content-hash": "3b6c7d627bbb33e8c995ebfd569bd00f",
"packages": [
{
"name": "container-interop/service-provider",
"version": "v0.4.0",
"source": {
"type": "git",
"url": "https://github.com/container-interop/service-provider.git",
"reference": "4969b9e49460690b7430b3f1a87cab07be61418a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/container-interop/service-provider/zipball/4969b9e49460690b7430b3f1a87cab07be61418a",
"reference": "4969b9e49460690b7430b3f1a87cab07be61418a",
"shasum": ""
},
"require": {
"psr/container": "^1.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Interop\\Container\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Promoting container interoperability through standard service providers",
"homepage": "https://github.com/container-interop/service-provider",
"support": {
"issues": "https://github.com/container-interop/service-provider/issues",
"source": "https://github.com/container-interop/service-provider/tree/master"
},
"time": "2017-09-20T14:13:36+00:00"
},
{
"name": "mnapoli/simplex",
"version": "0.5.0",
"source": {
"type": "git",
"url": "https://github.com/mnapoli/simplex.git",
"reference": "cdc81c738262739f4f35e911f6888da9ee59016b"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/mnapoli/simplex/zipball/cdc81c738262739f4f35e911f6888da9ee59016b",
"reference": "cdc81c738262739f4f35e911f6888da9ee59016b",
"shasum": ""
},
"require": {
"container-interop/service-provider": "~0.4.0",
"php": ">=5.3.0",
"psr/container": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "~4.0"
},
"type": "library",
"autoload": {
"psr-4": {
"Simplex\\": "src/Simplex"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Fabien Potencier",
"email": "fabien@symfony.com"
},
{
"name": "Matthieu Napoli",
"email": "matthieu@mnapoli.fr"
}
],
"description": "Pimple fork with full container-interop support",
"homepage": "https://github.com/mnapoli/simplex",
"keywords": [
"container",
"dependency injection"
],
"support": {
"issues": "https://github.com/mnapoli/simplex/issues",
"source": "https://github.com/mnapoli/simplex/tree/master"
},
"time": "2018-02-13T08:14:36+00:00"
},
{
"name": "psr/container",
"version": "1.1.1",
@ -1097,11 +1008,11 @@
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": "^7.3|^8"
"php": "^7.2|^8"
},
"platform-dev": [],
"platform-overrides": {
"php": "7.3.31"
"php": "7.2.34"
},
"plugin-api-version": "2.1.0"
}

2
src/Application.php

@ -3,7 +3,7 @@
namespace Spip\Cli;
use Psr\Container\ContainerInterface;
use Simplex\Container;
use Spip\Cli\Container\Container;
use Spip\Cli\Console\Style\SpipCliStyle;
use Symfony\Component\Console\Application as ConsoleApplication;
use Symfony\Component\Console\Input\ArgvInput;

370
src/Container/Container.php

@ -0,0 +1,370 @@
<?php
/*
* This file is part of Spip-cli.
* Code adapted from Mnapoli/Simplex
*
* Copyright (c) 2009 Fabien Potencier
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
namespace Spip\Cli\Container;
use Psr\Container\ContainerInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Spip\Cli\Container\Exception\EntryNotFound;
/**
* Container main class.
*
* @author Fabien Potencier
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class Container implements \ArrayAccess, ContainerInterface
{
private $values = array();
private $factories;
private $protected;
private $frozen = array();
private $raw = array();
private $keys = array();
/**
* Container from which to fetch dependencies.
* @see https://github.com/container-interop/container-interop/blob/master/docs/Delegate-lookup.md
* @var ContainerInterface
*/
private $rootContainer;
/**
* Instantiate the container.
*
* Objects and parameters can be passed as argument to the constructor.
*
* @param array $providers The service providers to register.
* @param array $values The parameters or objects.
* @param ContainerInterface $rootContainer Container from which to fetch dependencies. If null, this container
* will be considered the root container.
*/
public function __construct(array $providers = array(), array $values = array(), ContainerInterface $rootContainer = null)
{
$this->rootContainer = $rootContainer ?: $this;
$this->factories = new \SplObjectStorage();
$this->protected = new \SplObjectStorage();
$this->register($providers, $values);
}
/**
* Finds an entry of the container by its identifier and returns it.
*
* @param string $id Identifier of the entry to look for.
*
* @throws NotFoundExceptionInterface No entry was found for this identifier.
* @throws ContainerExceptionInterface Error while retrieving the entry.
*
* @return mixed Entry.
*/
public function get($id)
{
return $this->offsetGet($id);
}
/**
* Returns true if the container can return an entry for the given identifier.
* Returns false otherwise.
*
* @param string $id Identifier of the entry to look for.
*
* @return boolean
*/
public function has($id)
{
return $this->offsetExists($id);
}
/**
* Sets a parameter or an object.
*
* Objects must be defined as Closures.
*
* Allowing any PHP callable leads to difficult to debug problems
* as function names (strings) are callable (creating a function with
* the same name as an existing parameter would break your container).
*
* @param string $id The unique identifier for the parameter or object
* @param mixed $value The value of the parameter or a closure to define an object
*
* @throws \RuntimeException Prevent override of a frozen service
*/
public function set($id, $value)
{
$this->offsetSet($id, $value);
}
/**
* Sets a parameter or an object.
*
* Objects must be defined as Closures.
*
* Allowing any PHP callable leads to difficult to debug problems
* as function names (strings) are callable (creating a function with
* the same name as an existing parameter would break your container).
*
* @param string $id The unique identifier for the parameter or object
* @param mixed $value The value of the parameter or a closure to define an object
*
* @throws \RuntimeException Prevent override of a frozen service
*/
public function offsetSet($id, $value)
{
if (isset($this->frozen[$id])) {
throw new \RuntimeException(sprintf('Cannot override frozen service "%s".', $id));
}
$this->values[$id] = $value;
$this->keys[$id] = true;
}
/**
* Gets a parameter or an object.
*
* @param string $id The unique identifier for the parameter or object
*
* @return mixed The value of the parameter or an object
*
* @throws NotFoundExceptionInterface if the identifier is not defined
*/
public function offsetGet($id)
{
if (!isset($this->keys[$id])) {
throw new EntryNotFound($id);
}
if (
isset($this->raw[$id])
|| !is_object($this->values[$id])
|| isset($this->protected[$this->values[$id]])
|| !method_exists($this->values[$id], '__invoke')
) {
return $this->values[$id];
}
if (isset($this->factories[$this->values[$id]])) {
return $this->values[$id]($this);
}
$raw = $this->values[$id];
$val = $this->values[$id] = $raw($this->rootContainer);
$this->raw[$id] = $raw;
$this->frozen[$id] = true;
return $val;
}
/**
* Checks if a parameter or an object is set.
*
* @param string $id The unique identifier for the parameter or object
*
* @return bool
*/
public function offsetExists($id)
{
return isset($this->keys[$id]);
}
/**
* Unsets a parameter or an object.
*
* @param string $id The unique identifier for the parameter or object
*/
public function offsetUnset($id)
{
if (isset($this->keys[$id])) {
if (is_object($this->values[$id])) {
unset($this->factories[$this->values[$id]], $this->protected[$this->values[$id]]);
}
unset($this->values[$id], $this->frozen[$id], $this->raw[$id], $this->keys[$id]);
}
}
/**
* Marks a callable as being a factory service.
*
* @param callable $callable A service definition to be used as a factory
*
* @return callable The passed callable
*
* @throws \InvalidArgumentException Service definition has to be a closure of an invokable object
*/
public function factory($callable)
{
if (!method_exists($callable, '__invoke')) {
throw new \InvalidArgumentException('Service definition is not a Closure or invokable object.');
}
$this->factories->attach($callable);
return $callable;
}
/**
* Protects a callable from being interpreted as a service.
*
* This is useful when you want to store a callable as a parameter.
*
* @param callable $callable A callable to protect from being evaluated
*
* @return callable The passed callable
*
* @throws \InvalidArgumentException Service definition has to be a closure of an invokable object
*/
public function protect($callable)
{
if (!method_exists($callable, '__invoke')) {
throw new \InvalidArgumentException('Callable is not a Closure or invokable object.');
}
$this->protected->attach($callable);
return $callable;
}
/**
* Gets a parameter or the closure defining an object.
*
* @param string $id The unique identifier for the parameter or object
*
* @return mixed The value of the parameter or the closure defining an object
*
* @throws \InvalidArgumentException if the identifier is not defined
*/
public function raw($id)
{
if (!isset($this->keys[$id])) {
throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
}
if (isset($this->raw[$id])) {
return $this->raw[$id];
}
return $this->values[$id];
}
/**
* Extends an object definition.
*
* Useful when you want to extend an existing object definition,
* without necessarily loading that object.
*
* @param string $id The unique identifier for the object
* @param callable $callable A service definition to extend the original
*
* @return callable The wrapped callable
*
* @throws \InvalidArgumentException if the identifier is not defined or not a service definition
*/
public function extend($id, $callable)
{
if (!isset($this->keys[$id])) {
throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
}
if (!is_object($callable) || !method_exists($callable, '__invoke')) {
throw new \InvalidArgumentException('Extension service definition is not a Closure or invokable object.');
}
$factory = $this->values[$id];
$extended = function ($c) use ($callable, $factory) {
$previous = is_callable($factory) ? $factory($c) : $factory;
return $callable($previous, $c);
};
if (is_object($factory) && isset($this->factories[$factory])) {
$this->factories->detach($factory);
$this->factories->attach($extended);
}
return $this[$id] = $extended;
}
/**
* Returns all defined value names.
*
* @return array An array of value names
*/
public function keys()
{
return array_keys($this->values);
}
/**
* Registers service providers.
*
* @param array $providers the list of service providers to register
* @param array $values An array of values that customizes the providers
*
* @return void
*/
private function register(array $providers, array $values)
{
foreach ($providers as $provider) {
$factories = $provider->getFactories();
foreach ($factories as $key => $callable) {
$this[$key] = function (ContainerInterface $c) use ($callable) {
return call_user_func($callable, $c);
};
}
}
foreach ($providers as $provider) {
$extensions = $provider->getExtensions();
foreach ($extensions as $key => $callable) {
if (isset($this->keys[$key])) {
// Extend a previous entry
$this[$key] = $this->extend($key, function ($previous, ContainerInterface $c) use ($callable) {
return call_user_func($callable, $c, $previous);
});
} else {
$this[$key] = function (ContainerInterface $c) use ($callable) {
return call_user_func($callable, $c);
};
}
}
}
foreach ($values as $key => $value) {
$this[$key] = $value;
}
}
}

18
src/Container/Exception/EntryNotFound.php

@ -0,0 +1,18 @@
<?php
namespace Spip\Cli\Container\Exception;
use Psr\Container\NotFoundExceptionInterface;
/**
* No entry was found in the container.
*
* @author Matthieu Napoli <matthieu@mnapoli.fr>
*/
class EntryNotFound extends \Exception implements NotFoundExceptionInterface
{
public function __construct($id)
{
parent::__construct(sprintf('Identifier "%s" is not defined.', $id));
}
}
Loading…
Cancel
Save