default services conflit ?

This commit is contained in:
armansansd
2022-04-27 11:30:43 +02:00
parent 28190a5749
commit 8bb1064a3b
8132 changed files with 900138 additions and 426 deletions

View File

@@ -0,0 +1,74 @@
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Component\Routing\Enhancer;
use Symfony\Cmf\Component\Routing\ContentRepositoryInterface;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* This enhancer uses a ContentRepositoryInterface to load a content if $target
* is empty.
*
* $source specifies the field that contains the ID to load, $target specifies
* the field where to put the content returned by the repository.
*
* @author Samusev Andrey
*/
class ContentRepositoryEnhancer implements RouteEnhancerInterface
{
/**
* @var ContentRepositoryInterface
*/
private $contentRepository;
/**
* @var string
*/
private $target;
/**
* @var string
*/
private $source;
/**
* @param ContentRepositoryInterface $contentRepository repository to search for the content
* @param string $target the field name to set content
* @param string $source the field name of the request parameter that contains the id
*/
public function __construct(
ContentRepositoryInterface $contentRepository,
$target = RouteObjectInterface::CONTENT_OBJECT,
$source = RouteObjectInterface::CONTENT_ID
) {
$this->contentRepository = $contentRepository;
$this->target = $target;
$this->source = $source;
}
/**
* {@inheritdoc}
*/
public function enhance(array $defaults, Request $request)
{
if (array_key_exists($this->target, $defaults)
|| !array_key_exists($this->source, $defaults)
) {
return $defaults;
}
$defaults[$this->target] = $this->contentRepository->findById($defaults[$this->source]);
return $defaults;
}
}

View File

@@ -0,0 +1,84 @@
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Component\Routing\Enhancer;
use Symfony\Component\HttpFoundation\Request;
/**
* This enhancer sets a field if not yet existing from the class of an object
* in another field.
*
* The comparison is done with instanceof to support proxy classes and such.
*
* Only works with RouteObjectInterface routes that can return a referenced
* content.
*
* @author David Buchmann
*/
class FieldByClassEnhancer implements RouteEnhancerInterface
{
/**
* @var string field for the source class
*/
protected $source;
/**
* @var string field to write hashmap lookup result into
*/
protected $target;
/**
* @var array containing the mapping between a class name and the target value
*/
protected $map;
/**
* @param string $source the field name of the class
* @param string $target the field name to set from the map
* @param array $map the map of class names to field values
*/
public function __construct($source, $target, $map)
{
$this->source = $source;
$this->target = $target;
$this->map = $map;
}
/**
* If the source field is instance of one of the entries in the map,
* target is set to the value of that map entry.
*
* {@inheritdoc}
*/
public function enhance(array $defaults, Request $request)
{
if (array_key_exists($this->target, $defaults)
|| !array_key_exists($this->source, $defaults)
) {
return $defaults;
}
// we need to loop over the array and do instanceof in case the content
// class extends the specified class
// i.e. phpcr-odm generates proxy class for the content.
foreach ($this->map as $class => $value) {
if ($defaults[$this->source] instanceof $class) {
// found a matching entry in the map
$defaults[$this->target] = $value;
return $defaults;
}
}
return $defaults;
}
}

View File

@@ -0,0 +1,69 @@
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Component\Routing\Enhancer;
use Symfony\Component\HttpFoundation\Request;
/**
* This enhancer can fill one field with the result of a hashmap lookup of
* another field. If the target field is already set, it does nothing.
*
* @author David Buchmann
*/
class FieldMapEnhancer implements RouteEnhancerInterface
{
/**
* @var string field for key in hashmap lookup
*/
protected $source;
/**
* @var string field to write hashmap lookup result into
*/
protected $target;
/**
* @var array containing the mapping between the source field value and target field value
*/
protected $hashmap;
/**
* @param string $source the field to read
* @param string $target the field to write the result of the lookup into
* @param array $hashmap for looking up value from source and get value for target
*/
public function __construct($source, $target, array $hashmap)
{
$this->source = $source;
$this->target = $target;
$this->hashmap = $hashmap;
}
/**
* If the target field is not set but the source field is, map the field.
*
* {@inheritdoc}
*/
public function enhance(array $defaults, Request $request)
{
if (array_key_exists($this->target, $defaults)
|| !array_key_exists($this->source, $defaults)
|| !array_key_exists($defaults[$this->source], $this->hashmap)
) {
return $defaults;
}
$defaults[$this->target] = $this->hashmap[$defaults[$this->source]];
return $defaults;
}
}

View File

@@ -0,0 +1,75 @@
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Component\Routing\Enhancer;
use Symfony\Component\HttpFoundation\Request;
/**
* This enhancer sets a field to a fixed value. You can specify a source field
* name to only set the target field if the source is present.
*
* @author David Buchmann
*/
class FieldPresenceEnhancer implements RouteEnhancerInterface
{
/**
* Field name for the source field that must exist. If null, the target
* field is always set if not already present.
*
* @var string|null
*/
protected $source;
/**
* Field name to write the value into.
*
* @var string
*/
protected $target;
/**
* Value to set the target field to.
*
* @var string
*/
private $value;
/**
* @param string|null $source the field name of the class, null to disable the check
* @param string $target the field name to set from the map
* @param string $value value to set target field to if source field exists
*/
public function __construct($source, $target, $value)
{
$this->source = $source;
$this->target = $target;
$this->value = $value;
}
/**
* {@inheritdoc}
*/
public function enhance(array $defaults, Request $request)
{
if (array_key_exists($this->target, $defaults)) {
return $defaults;
}
if (null !== $this->source && !array_key_exists($this->source, $defaults)) {
return $defaults;
}
$defaults[$this->target] = $this->value;
return $defaults;
}
}

View File

@@ -0,0 +1,79 @@
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Component\Routing\Enhancer;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* This enhancer sets the content to target field if the route provides content.
*
* Only works with RouteObjectInterface routes that can return a referenced
* content.
*
* @author David Buchmann
*/
class RouteContentEnhancer implements RouteEnhancerInterface
{
/**
* @var string field for the route class
*/
protected $routefield;
/**
* @var string field to write hashmap lookup result into
*/
protected $target;
/**
* @param string $routefield the field name of the route class
* @param string $target the field name to set from the map
* @param array $hashmap the map of class names to field values
*/
public function __construct($routefield, $target)
{
$this->routefield = $routefield;
$this->target = $target;
}
/**
* If the route has a non-null content and if that content class is in the
* injected map, returns that controller.
*
* {@inheritdoc}
*/
public function enhance(array $defaults, Request $request)
{
if (array_key_exists($this->target, $defaults)) {
// no need to do anything
return $defaults;
}
if (!array_key_exists($this->routefield, $defaults)
|| !$defaults[$this->routefield] instanceof RouteObjectInterface
) {
// we can't determine the content
return $defaults;
}
/** @var $route RouteObjectInterface */
$route = $defaults[$this->routefield];
$content = $route->getContent();
if (!$content) {
// we have no content
return $defaults;
}
$defaults[$this->target] = $content;
return $defaults;
}
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Component\Routing\Enhancer;
use Symfony\Component\HttpFoundation\Request;
/**
* A route enhancer can change the values in the route data arrays.
*
* This is useful to provide information to the rest of the routing system
* that can be inferred from other parameters rather than hardcode that
* information in every route.
*
* @author David Buchmann
*/
interface RouteEnhancerInterface
{
/**
* Update the defaults based on its own data and the request.
*
* @param array $defaults the getRouteDefaults array
* @param Request $request the Request instance
*
* @return array the modified defaults. Each enhancer MUST return the
* $defaults but may add or remove values
*/
public function enhance(array $defaults, Request $request);
}

View File

@@ -0,0 +1,107 @@
<?php
/*
* This file is part of the Symfony CMF package.
*
* (c) Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Cmf\Component\Routing\Enhancer;
use Symfony\Component\HttpFoundation\Request;
/**
* Functionality to collect and apply route enhancers to a request.
*
* @author Tim Plunkett
* @author Larry Garfield
* @author David Buchmann
*/
trait RouteEnhancerTrait
{
/**
* @var RouteEnhancerInterface[][]
*/
private $enhancers = [];
/**
* Cached sorted list of enhancers.
*
* @var RouteEnhancerInterface[]
*/
private $sortedEnhancers = [];
/**
* Apply the route enhancers to the defaults, according to priorities.
*
* @param array $defaults
*
* @return array
*/
protected function applyRouteEnhancers($defaults, Request $request)
{
foreach ($this->getRouteEnhancers() as $enhancer) {
$defaults = $enhancer->enhance($defaults, $request);
}
return $defaults;
}
/**
* Add route enhancers to the router to let them generate information on
* matched routes.
*
* The order of the enhancers is determined by the priority, the higher the
* value, the earlier the enhancer is run.
*
* @param int $priority
*
* @return self
*/
public function addRouteEnhancer(RouteEnhancerInterface $enhancer, $priority = 0)
{
if (empty($this->enhancers[$priority])) {
$this->enhancers[$priority] = [];
}
$this->enhancers[$priority][] = $enhancer;
$this->sortedEnhancers = [];
return $this;
}
/**
* Sorts the enhancers and flattens them.
*
* @return RouteEnhancerInterface[] the enhancers ordered by priority
*/
public function getRouteEnhancers()
{
if (0 === count($this->sortedEnhancers)) {
$this->sortedEnhancers = $this->sortRouteEnhancers();
}
return $this->sortedEnhancers;
}
/**
* Sort enhancers by priority.
*
* The highest priority number is the highest priority (reverse sorting).
*
* @return RouteEnhancerInterface[] the sorted enhancers
*/
private function sortRouteEnhancers()
{
if (0 === count($this->enhancers)) {
return [];
}
krsort($this->enhancers);
return call_user_func_array('array_merge', $this->enhancers);
}
}