@@ -24,43 +24,43 @@ |
||
24 | 24 | use SWP\Component\MultiTenancy\Model\TenantInterface; |
25 | 25 | |
26 | 26 | class CachedTenantContext extends TenantContext implements CachedTenantContextInterface { |
27 | - private $resolvedTenants = []; |
|
27 | + private $resolvedTenants = []; |
|
28 | 28 | |
29 | - public function getTenant(): ?TenantInterface { |
|
29 | + public function getTenant(): ?TenantInterface { |
|
30 | 30 | $currentRequest = $this->requestStack->getCurrentRequest(); |
31 | 31 | if ($currentRequest && $this->requestStack->getCurrentRequest()->attributes->get('exception') instanceof TenantNotFoundException) { |
32 | - return null; |
|
32 | + return null; |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | if ($currentRequest === null) { |
36 | - return $this->tenant; |
|
36 | + return $this->tenant; |
|
37 | 37 | } |
38 | 38 | |
39 | 39 | $cacheKey = self::getCacheKey($currentRequest->getHost()); |
40 | 40 | if (!array_key_exists($cacheKey, $this->resolvedTenants) || $this->resolvedTenants[$cacheKey] instanceof TenantInterface) { |
41 | - $this->resolvedTenants[$cacheKey] = parent::getTenant(); |
|
41 | + $this->resolvedTenants[$cacheKey] = parent::getTenant(); |
|
42 | 42 | } else { |
43 | - $this->tenant = $this->resolvedTenants[$cacheKey]; |
|
43 | + $this->tenant = $this->resolvedTenants[$cacheKey]; |
|
44 | 44 | } |
45 | 45 | |
46 | 46 | return $this->tenant; |
47 | - } |
|
47 | + } |
|
48 | 48 | |
49 | - public function setTenant(TenantInterface $tenant): void { |
|
49 | + public function setTenant(TenantInterface $tenant): void { |
|
50 | 50 | parent::setTenant($tenant); |
51 | 51 | $this->dispatcher->dispatch(new GenericEvent(), MultiTenancyEvents::TENANTABLE_ENABLE); |
52 | 52 | |
53 | 53 | $this->resolvedTenants[self::getCacheKey( |
54 | 54 | $tenant->getSubdomain() ? $tenant->getSubdomain() . '.' . $tenant->getDomainName() : $tenant->getDomainName() |
55 | 55 | )] = $tenant; |
56 | - } |
|
56 | + } |
|
57 | 57 | |
58 | - public function reset(): void { |
|
58 | + public function reset(): void { |
|
59 | 59 | $this->tenant = null; |
60 | 60 | $this->resolvedTenants = []; |
61 | - } |
|
61 | + } |
|
62 | 62 | |
63 | - private static function getCacheKey(string $host): string { |
|
63 | + private static function getCacheKey(string $host): string { |
|
64 | 64 | return md5('tenant_cache__' . $host); |
65 | - } |
|
65 | + } |
|
66 | 66 | } |
@@ -28,62 +28,62 @@ |
||
28 | 28 | * Class TenantContext. |
29 | 29 | */ |
30 | 30 | class TenantContext implements TenantContextInterface { |
31 | - /** |
|
32 | - * @var TenantInterface |
|
33 | - */ |
|
34 | - protected $tenant; |
|
31 | + /** |
|
32 | + * @var TenantInterface |
|
33 | + */ |
|
34 | + protected $tenant; |
|
35 | 35 | |
36 | - /** |
|
37 | - * @var TenantResolverInterface |
|
38 | - */ |
|
39 | - protected $tenantResolver; |
|
36 | + /** |
|
37 | + * @var TenantResolverInterface |
|
38 | + */ |
|
39 | + protected $tenantResolver; |
|
40 | 40 | |
41 | - /** |
|
42 | - * @var RequestStack |
|
43 | - */ |
|
44 | - protected $requestStack; |
|
41 | + /** |
|
42 | + * @var RequestStack |
|
43 | + */ |
|
44 | + protected $requestStack; |
|
45 | 45 | |
46 | - /** |
|
47 | - * @var EventDispatcherInterface |
|
48 | - */ |
|
49 | - protected $dispatcher; |
|
46 | + /** |
|
47 | + * @var EventDispatcherInterface |
|
48 | + */ |
|
49 | + protected $dispatcher; |
|
50 | 50 | |
51 | - /** |
|
52 | - * TenantContext constructor. |
|
53 | - */ |
|
54 | - public function __construct(TenantResolverInterface $tenantResolver, RequestStack $requestStack, |
|
55 | - EventDispatcherInterface $dispatcher) { |
|
51 | + /** |
|
52 | + * TenantContext constructor. |
|
53 | + */ |
|
54 | + public function __construct(TenantResolverInterface $tenantResolver, RequestStack $requestStack, |
|
55 | + EventDispatcherInterface $dispatcher) { |
|
56 | 56 | $this->tenantResolver = $tenantResolver; |
57 | 57 | $this->requestStack = $requestStack; |
58 | 58 | $this->dispatcher = $dispatcher; |
59 | - } |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * {@inheritdoc} |
|
63 | - */ |
|
64 | - public function getTenant() { |
|
61 | + /** |
|
62 | + * {@inheritdoc} |
|
63 | + */ |
|
64 | + public function getTenant() { |
|
65 | 65 | if ($this->tenant !== null) |
66 | - return $this->tenant; |
|
66 | + return $this->tenant; |
|
67 | 67 | |
68 | 68 | $currentRequest = $this->requestStack->getCurrentRequest(); |
69 | 69 | if (null !== $currentRequest && false !== strpos($currentRequest->getRequestUri(), '_profiler')) { |
70 | - $profilerTenant = new Tenant(); |
|
71 | - $profilerTenant->setDomainName($currentRequest->getHost()); |
|
72 | - $this->setTenant($profilerTenant); |
|
73 | - return $profilerTenant; |
|
70 | + $profilerTenant = new Tenant(); |
|
71 | + $profilerTenant->setDomainName($currentRequest->getHost()); |
|
72 | + $this->setTenant($profilerTenant); |
|
73 | + return $profilerTenant; |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | $newTenant = $this->tenantResolver->resolve($currentRequest ? $currentRequest->getHost() : null); |
77 | 77 | $this->setTenant($newTenant); |
78 | 78 | return $newTenant; |
79 | - } |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * {@inheritdoc} |
|
83 | - */ |
|
84 | - public function setTenant(TenantInterface $tenant) { |
|
81 | + /** |
|
82 | + * {@inheritdoc} |
|
83 | + */ |
|
84 | + public function setTenant(TenantInterface $tenant) { |
|
85 | 85 | $this->tenant = $tenant; |
86 | 86 | |
87 | 87 | $this->dispatcher->dispatch(new GenericEvent($tenant), MultiTenancyEvents::TENANT_SET); |
88 | - } |
|
88 | + } |
|
89 | 89 | } |
@@ -78,7 +78,7 @@ |
||
78 | 78 | return $paginator->paginate( |
79 | 79 | $queryBuilder, |
80 | 80 | (int)$criteria->get('firstResult', 0), |
81 | - (int)$criteria->get('maxResults', RepositoryInterface::MAX_RESULTS) |
|
81 | + (int)$criteria->get('maxResults', RepositoryInterface::MAX_RESULTS) |
|
82 | 82 | ); |
83 | 83 | } |
84 | 84 |
@@ -36,7 +36,7 @@ |
||
36 | 36 | |
37 | 37 | private function getPaths() : array |
38 | 38 | { |
39 | - return [$this->themeUploader->getAvailableThemesPath()]; |
|
39 | + return [$this->themeUploader->getAvailableThemesPath()]; |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | /** |
@@ -25,62 +25,62 @@ discard block |
||
25 | 25 | use Symfony\Component\HttpFoundation\RequestStack; |
26 | 26 | |
27 | 27 | class Paginator implements PaginatorInterface { |
28 | - private EventDispatcherInterface $eventDispatcher; |
|
29 | - private ?RequestStack $requestStack; |
|
30 | - |
|
31 | - private array $defaultOptions = [ |
|
32 | - self::PAGE_PARAMETER_NAME => 'page', |
|
33 | - self::SORT_FIELD_PARAMETER_NAME => 'sort', |
|
34 | - self::SORT_DIRECTION_PARAMETER_NAME => 'direction', |
|
35 | - self::FILTER_FIELD_PARAMETER_NAME => 'filterParam', |
|
36 | - self::FILTER_VALUE_PARAMETER_NAME => 'filterValue', |
|
37 | - self::DISTINCT => true, |
|
38 | - self::PAGE_OUT_OF_RANGE => self::PAGE_OUT_OF_RANGE_IGNORE, |
|
39 | - self::DEFAULT_LIMIT => self::DEFAULT_LIMIT_VALUE, |
|
40 | - ]; |
|
41 | - |
|
42 | - |
|
43 | - public function __construct(EventDispatcherInterface $eventDispatcher, ?RequestStack $requestStack = null) { |
|
28 | + private EventDispatcherInterface $eventDispatcher; |
|
29 | + private ?RequestStack $requestStack; |
|
30 | + |
|
31 | + private array $defaultOptions = [ |
|
32 | + self::PAGE_PARAMETER_NAME => 'page', |
|
33 | + self::SORT_FIELD_PARAMETER_NAME => 'sort', |
|
34 | + self::SORT_DIRECTION_PARAMETER_NAME => 'direction', |
|
35 | + self::FILTER_FIELD_PARAMETER_NAME => 'filterParam', |
|
36 | + self::FILTER_VALUE_PARAMETER_NAME => 'filterValue', |
|
37 | + self::DISTINCT => true, |
|
38 | + self::PAGE_OUT_OF_RANGE => self::PAGE_OUT_OF_RANGE_IGNORE, |
|
39 | + self::DEFAULT_LIMIT => self::DEFAULT_LIMIT_VALUE, |
|
40 | + ]; |
|
41 | + |
|
42 | + |
|
43 | + public function __construct(EventDispatcherInterface $eventDispatcher, ?RequestStack $requestStack = null) { |
|
44 | 44 | $this->eventDispatcher = $eventDispatcher; |
45 | 45 | $this->requestStack = $requestStack; |
46 | - } |
|
46 | + } |
|
47 | 47 | |
48 | - public function setDefaultPaginatorOptions(array $options): void { |
|
48 | + public function setDefaultPaginatorOptions(array $options): void { |
|
49 | 49 | $this->defaultOptions = \array_merge($this->defaultOptions, $options); |
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * {@inheritdoc} |
|
54 | - * |
|
55 | - * IMPORTANT: This method expects OFFSET value instead PAGE |
|
56 | - */ |
|
57 | - public function paginate($target, int $offset = 1, $limit = 10, array $options = array()) : PaginationInterface { |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * {@inheritdoc} |
|
54 | + * |
|
55 | + * IMPORTANT: This method expects OFFSET value instead PAGE |
|
56 | + */ |
|
57 | + public function paginate($target, int $offset = 1, $limit = 10, array $options = array()) : PaginationInterface { |
|
58 | 58 | $limit = intval(abs($limit)); |
59 | 59 | if (!$limit) { |
60 | - throw new \LogicException('Invalid item per page number, must be a positive number'); |
|
60 | + throw new \LogicException('Invalid item per page number, must be a positive number'); |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | $page = intval(round($offset / $limit), 10); |
64 | 64 | if ($page < 1) { |
65 | - $page = 1; |
|
65 | + $page = 1; |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | $options = \array_merge($this->defaultOptions, $options); |
69 | 69 | |
70 | 70 | // normalize default sort field |
71 | 71 | if (isset($options[PaginatorInterface::DEFAULT_SORT_FIELD_NAME]) && is_array($options[PaginatorInterface::DEFAULT_SORT_FIELD_NAME])) { |
72 | - $options[PaginatorInterface::DEFAULT_SORT_FIELD_NAME] = implode('+', $options[PaginatorInterface::DEFAULT_SORT_FIELD_NAME]); |
|
72 | + $options[PaginatorInterface::DEFAULT_SORT_FIELD_NAME] = implode('+', $options[PaginatorInterface::DEFAULT_SORT_FIELD_NAME]); |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | $request = null === $this->requestStack ? Request::createFromGlobals() : $this->requestStack->getCurrentRequest(); |
76 | 76 | |
77 | 77 | // default sort field and direction are set based on options (if available) |
78 | 78 | if (isset($options[self::DEFAULT_SORT_FIELD_NAME]) && !$request->query->has($options[self::SORT_FIELD_PARAMETER_NAME])) { |
79 | - $request->query->set($options[self::SORT_FIELD_PARAMETER_NAME], $options[self::DEFAULT_SORT_FIELD_NAME]); |
|
79 | + $request->query->set($options[self::SORT_FIELD_PARAMETER_NAME], $options[self::DEFAULT_SORT_FIELD_NAME]); |
|
80 | 80 | |
81 | - if (!$request->query->has($options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME])) { |
|
81 | + if (!$request->query->has($options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME])) { |
|
82 | 82 | $request->query->set($options[PaginatorInterface::SORT_DIRECTION_PARAMETER_NAME], $options[PaginatorInterface::DEFAULT_SORT_DIRECTION] ?? 'asc'); |
83 | - } |
|
83 | + } |
|
84 | 84 | } |
85 | 85 | |
86 | 86 | // before pagination start |
@@ -92,20 +92,20 @@ discard block |
||
92 | 92 | $itemsEvent->target = &$target; |
93 | 93 | $this->eventDispatcher->dispatch($itemsEvent, 'knp_pager.items'); |
94 | 94 | if (!$itemsEvent->isPropagationStopped()) { |
95 | - throw new \RuntimeException('One of listeners must count and slice given target'); |
|
95 | + throw new \RuntimeException('One of listeners must count and slice given target'); |
|
96 | 96 | } |
97 | 97 | if ($page > ceil($itemsEvent->count / $limit)) { |
98 | - $pageOutOfRangeOption = $options[PaginatorInterface::PAGE_OUT_OF_RANGE] ?? $this->defaultOptions[PaginatorInterface::PAGE_OUT_OF_RANGE]; |
|
99 | - if ($pageOutOfRangeOption === PaginatorInterface::PAGE_OUT_OF_RANGE_FIX && $itemsEvent->count > 0) { |
|
98 | + $pageOutOfRangeOption = $options[PaginatorInterface::PAGE_OUT_OF_RANGE] ?? $this->defaultOptions[PaginatorInterface::PAGE_OUT_OF_RANGE]; |
|
99 | + if ($pageOutOfRangeOption === PaginatorInterface::PAGE_OUT_OF_RANGE_FIX && $itemsEvent->count > 0) { |
|
100 | 100 | // replace page number out of range with max page |
101 | 101 | return $this->paginate($target, (int) ceil($itemsEvent->count / $limit), $limit, $options); |
102 | - } |
|
103 | - if ($pageOutOfRangeOption === self::PAGE_OUT_OF_RANGE_THROW_EXCEPTION && $page > 1) { |
|
102 | + } |
|
103 | + if ($pageOutOfRangeOption === self::PAGE_OUT_OF_RANGE_THROW_EXCEPTION && $page > 1) { |
|
104 | 104 | throw new PageNumberOutOfRangeException( |
105 | 105 | sprintf('Page number: %d is out of range.', $page), |
106 | 106 | (int) ceil($itemsEvent->count / $limit) |
107 | 107 | ); |
108 | - } |
|
108 | + } |
|
109 | 109 | } |
110 | 110 | |
111 | 111 | // pagination initialization event |
@@ -114,7 +114,7 @@ discard block |
||
114 | 114 | $paginationEvent->options = &$options; |
115 | 115 | $this->eventDispatcher->dispatch($paginationEvent, 'knp_pager.pagination'); |
116 | 116 | if (!$paginationEvent->isPropagationStopped()) { |
117 | - throw new \RuntimeException('One of listeners must create pagination view'); |
|
117 | + throw new \RuntimeException('One of listeners must create pagination view'); |
|
118 | 118 | } |
119 | 119 | // pagination class can be different, with different rendering methods |
120 | 120 | $paginationView = $paginationEvent->getPagination(); |
@@ -130,5 +130,5 @@ discard block |
||
130 | 130 | $this->eventDispatcher->dispatch($afterEvent, 'knp_pager.after'); |
131 | 131 | |
132 | 132 | return $paginationView; |
133 | - } |
|
133 | + } |
|
134 | 134 | } |
@@ -36,88 +36,88 @@ |
||
36 | 36 | use Twig\TwigFunction; |
37 | 37 | |
38 | 38 | final class DecoratingRoutingExtension extends AbstractExtension { |
39 | - private RoutingExtension $routingExtension; |
|
39 | + private RoutingExtension $routingExtension; |
|
40 | 40 | |
41 | - public function __construct(RoutingExtension $routingExtension) { |
|
41 | + public function __construct(RoutingExtension $routingExtension) { |
|
42 | 42 | $this->routingExtension = $routingExtension; |
43 | - } |
|
43 | + } |
|
44 | 44 | |
45 | - public function getPath($name, $parameters = [], $relative = false): ?string { |
|
45 | + public function getPath($name, $parameters = [], $relative = false): ?string { |
|
46 | 46 | if ($name == null) { |
47 | - return null; |
|
47 | + return null; |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | if (is_object($name)) { |
51 | - $object = $name; |
|
52 | - $name = null; |
|
53 | - self::setupParams($object, $name, $parameters); |
|
51 | + $object = $name; |
|
52 | + $name = null; |
|
53 | + self::setupParams($object, $name, $parameters); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | try { |
57 | - return $this->routingExtension->getPath($name, $parameters, $relative); |
|
57 | + return $this->routingExtension->getPath($name, $parameters, $relative); |
|
58 | 58 | } catch (RouteNotFoundException|MissingMandatoryParametersException|InvalidParameterException $e) { |
59 | - // allow empty path |
|
59 | + // allow empty path |
|
60 | 60 | } |
61 | 61 | |
62 | 62 | return null; |
63 | - } |
|
63 | + } |
|
64 | 64 | |
65 | - public function getUrl($name, $parameters = [], $schemeRelative = false): ?string { |
|
65 | + public function getUrl($name, $parameters = [], $schemeRelative = false): ?string { |
|
66 | 66 | if ($name == null) { |
67 | - return null; |
|
67 | + return null; |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | if (is_object($name)) { |
71 | - $object = $name; |
|
72 | - $name = null; |
|
73 | - self::setupParams($object, $name, $parameters); |
|
71 | + $object = $name; |
|
72 | + $name = null; |
|
73 | + self::setupParams($object, $name, $parameters); |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | try { |
77 | - return $this->routingExtension->getUrl($name, $parameters, $schemeRelative); |
|
77 | + return $this->routingExtension->getUrl($name, $parameters, $schemeRelative); |
|
78 | 78 | } catch (RouteNotFoundException|MissingMandatoryParametersException|InvalidParameterException $e) { |
79 | - // allow empty url |
|
79 | + // allow empty url |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | return null; |
83 | - } |
|
83 | + } |
|
84 | 84 | |
85 | - public static function setupParams(object $object, &$name, &$parameters) { |
|
85 | + public static function setupParams(object $object, &$name, &$parameters) { |
|
86 | 86 | $name = RouteObjectInterface::OBJECT_BASED_ROUTE_NAME; |
87 | 87 | $parameters[RouteObjectInterface::ROUTE_OBJECT] = $object; |
88 | 88 | |
89 | 89 | if ($object instanceof Meta) { |
90 | - $values = $object->getValues(); |
|
91 | - if (($values instanceof ArticleMediaInterface || $values instanceof ImageRenditionInterface)) { |
|
90 | + $values = $object->getValues(); |
|
91 | + if (($values instanceof ArticleMediaInterface || $values instanceof ImageRenditionInterface)) { |
|
92 | 92 | $name = MediaRouter::OBJECT_BASED_ROUTE_NAME; |
93 | 93 | return; |
94 | - } |
|
94 | + } |
|
95 | 95 | |
96 | - if ($values instanceof ArticleSeoMediaInterface) { |
|
96 | + if ($values instanceof ArticleSeoMediaInterface) { |
|
97 | 97 | $name = SeoMediaRouter::OBJECT_BASED_ROUTE_NAME; |
98 | 98 | return; |
99 | - } |
|
99 | + } |
|
100 | 100 | |
101 | - if ($values instanceof AuthorMediaInterface) { |
|
101 | + if ($values instanceof AuthorMediaInterface) { |
|
102 | 102 | $name = ArticleAuthorMediaRouter::OBJECT_BASED_ROUTE_NAME; |
103 | 103 | return; |
104 | - } |
|
104 | + } |
|
105 | 105 | |
106 | - if ($values instanceof ArticleInterface || $values instanceof RouteInterface) { |
|
106 | + if ($values instanceof ArticleInterface || $values instanceof RouteInterface) { |
|
107 | 107 | $name = MetaRouter::OBJECT_BASED_ROUTE_NAME; |
108 | 108 | return; |
109 | - } |
|
109 | + } |
|
110 | 110 | } |
111 | 111 | |
112 | 112 | if ($object instanceof RouteInterface || $object instanceof ArticleInterface) { |
113 | - $name = MetaRouter::OBJECT_BASED_ROUTE_NAME; |
|
113 | + $name = MetaRouter::OBJECT_BASED_ROUTE_NAME; |
|
114 | + } |
|
114 | 115 | } |
115 | - } |
|
116 | 116 | |
117 | - public function getFunctions(): array { |
|
117 | + public function getFunctions(): array { |
|
118 | 118 | return [ |
119 | 119 | new TwigFunction('url', [$this, 'getUrl'], ['is_safe_callback' => [$this->routingExtension, 'isUrlGenerationSafe']]), |
120 | 120 | new TwigFunction('path', [$this, 'getPath'], ['is_safe_callback' => [$this->routingExtension, 'isUrlGenerationSafe']]), |
121 | 121 | ]; |
122 | - } |
|
122 | + } |
|
123 | 123 | } |
@@ -6,8 +6,8 @@ |
||
6 | 6 | |
7 | 7 | final class BypassFinalHook implements BeforeTestHook |
8 | 8 | { |
9 | - public function executeBeforeTest(string $test): void |
|
10 | - { |
|
9 | + public function executeBeforeTest(string $test): void |
|
10 | + { |
|
11 | 11 | BypassFinals::enable(); |
12 | - } |
|
12 | + } |
|
13 | 13 | } |
@@ -37,83 +37,83 @@ discard block |
||
37 | 37 | |
38 | 38 | class MenuController extends AbstractFOSRestController { |
39 | 39 | |
40 | - private FormFactoryInterface $formFactory; |
|
41 | - private EventDispatcherInterface $eventDispatcher; |
|
42 | - private MenuItemRepositoryInterface $menuItemRepository; |
|
43 | - private MenuItemManager $menuItemManager; |
|
44 | - private MenuFactoryInterface $menuFactory; |
|
45 | - |
|
46 | - /** |
|
47 | - * @param FormFactoryInterface $formFactory |
|
48 | - * @param EventDispatcherInterface $eventDispatcher |
|
49 | - * @param MenuItemRepositoryInterface $menuItemRepository |
|
50 | - * @param MenuItemManager $menuItemManager |
|
51 | - * @param MenuFactoryInterface $menuFactory |
|
52 | - */ |
|
53 | - public function __construct(FormFactoryInterface $formFactory, EventDispatcherInterface $eventDispatcher, |
|
54 | - MenuItemRepositoryInterface $menuItemRepository, MenuItemManager $menuItemManager, |
|
55 | - MenuFactoryInterface $menuFactory) { |
|
40 | + private FormFactoryInterface $formFactory; |
|
41 | + private EventDispatcherInterface $eventDispatcher; |
|
42 | + private MenuItemRepositoryInterface $menuItemRepository; |
|
43 | + private MenuItemManager $menuItemManager; |
|
44 | + private MenuFactoryInterface $menuFactory; |
|
45 | + |
|
46 | + /** |
|
47 | + * @param FormFactoryInterface $formFactory |
|
48 | + * @param EventDispatcherInterface $eventDispatcher |
|
49 | + * @param MenuItemRepositoryInterface $menuItemRepository |
|
50 | + * @param MenuItemManager $menuItemManager |
|
51 | + * @param MenuFactoryInterface $menuFactory |
|
52 | + */ |
|
53 | + public function __construct(FormFactoryInterface $formFactory, EventDispatcherInterface $eventDispatcher, |
|
54 | + MenuItemRepositoryInterface $menuItemRepository, MenuItemManager $menuItemManager, |
|
55 | + MenuFactoryInterface $menuFactory) { |
|
56 | 56 | $this->formFactory = $formFactory; |
57 | 57 | $this->eventDispatcher = $eventDispatcher; |
58 | 58 | $this->menuItemRepository = $menuItemRepository; |
59 | 59 | $this->menuItemManager = $menuItemManager; |
60 | 60 | $this->menuFactory = $menuFactory; |
61 | - } |
|
61 | + } |
|
62 | 62 | |
63 | 63 | |
64 | - /** |
|
65 | - * @Route("/api/{version}/menus/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_list_menu") |
|
66 | - */ |
|
67 | - public function listAction(Request $request): ResourcesListResponseInterface { |
|
64 | + /** |
|
65 | + * @Route("/api/{version}/menus/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_list_menu") |
|
66 | + */ |
|
67 | + public function listAction(Request $request): ResourcesListResponseInterface { |
|
68 | 68 | $menuRepository = $this->menuItemRepository; |
69 | 69 | $page = $request->query->get('page', 1); |
70 | 70 | $limit = $request->query->get('limit', 10); |
71 | 71 | |
72 | 72 | return new ResourcesListResponse($menuRepository->findRootNodes($this->eventDispatcher, $page, $limit)); |
73 | - } |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * @Route("/api/{version}/menus/{id}/children/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_list_children_menu") |
|
77 | - */ |
|
78 | - public function listChildrenAction($id): ResourcesListResponseInterface { |
|
75 | + /** |
|
76 | + * @Route("/api/{version}/menus/{id}/children/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_list_children_menu") |
|
77 | + */ |
|
78 | + public function listChildrenAction($id): ResourcesListResponseInterface { |
|
79 | 79 | $menuRepository = $this->menuItemRepository; |
80 | 80 | |
81 | 81 | $menus = $menuRepository->findChildrenAsTree($this->eventDispatcher, $this->findOr404($id)); |
82 | 82 | |
83 | 83 | return new ResourcesListResponse($menus); |
84 | - } |
|
84 | + } |
|
85 | 85 | |
86 | - /** |
|
87 | - * @Route("/api/{version}/menus/{id}/move/", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_core_move_menu", requirements={"id"="\d+"}) |
|
88 | - */ |
|
89 | - public function moveAction(Request $request, $id): SingleResourceResponseInterface { |
|
86 | + /** |
|
87 | + * @Route("/api/{version}/menus/{id}/move/", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_core_move_menu", requirements={"id"="\d+"}) |
|
88 | + */ |
|
89 | + public function moveAction(Request $request, $id): SingleResourceResponseInterface { |
|
90 | 90 | $menuItem = $this->findOr404($id); |
91 | 91 | $form = $this->formFactory->createNamed('', MenuItemMoveType::class, [], ['method' => $request->getMethod()]); |
92 | 92 | $form->handleRequest($request); |
93 | 93 | |
94 | 94 | if ($form->isSubmitted() && $form->isValid()) { |
95 | - $menuItemManager = $this->menuItemManager; |
|
96 | - $formData = $form->getData(); |
|
95 | + $menuItemManager = $this->menuItemManager; |
|
96 | + $formData = $form->getData(); |
|
97 | 97 | |
98 | - $menuItemManager->move($menuItem, $formData['parent'], $formData['position']); |
|
98 | + $menuItemManager->move($menuItem, $formData['parent'], $formData['position']); |
|
99 | 99 | |
100 | - return new SingleResourceResponse($menuItem); |
|
100 | + return new SingleResourceResponse($menuItem); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | return new SingleResourceResponse($form, new ResponseContext(400)); |
104 | - } |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * @Route("/api/{version}/menus/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_get_menu") |
|
108 | - */ |
|
109 | - public function getAction($id): SingleResourceResponseInterface { |
|
106 | + /** |
|
107 | + * @Route("/api/{version}/menus/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_get_menu") |
|
108 | + */ |
|
109 | + public function getAction($id): SingleResourceResponseInterface { |
|
110 | 110 | return new SingleResourceResponse($this->findOr404($id)); |
111 | - } |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * @Route("/api/{version}/menus/", options={"expose"=true}, defaults={"version"="v2"}, methods={"POST"}, name="swp_api_core_create_menu") |
|
115 | - */ |
|
116 | - public function createAction(Request $request): SingleResourceResponseInterface { |
|
113 | + /** |
|
114 | + * @Route("/api/{version}/menus/", options={"expose"=true}, defaults={"version"="v2"}, methods={"POST"}, name="swp_api_core_create_menu") |
|
115 | + */ |
|
116 | + public function createAction(Request $request): SingleResourceResponseInterface { |
|
117 | 117 | /* @var MenuItemInterface $menu */ |
118 | 118 | $menu = $this->menuFactory->create(); |
119 | 119 | $form = $this->formFactory->createNamed('', MenuType::class, $menu, ['method' => $request->getMethod()]); |
@@ -121,20 +121,20 @@ discard block |
||
121 | 121 | $form->handleRequest($request); |
122 | 122 | |
123 | 123 | if ($form->isSubmitted() && $form->isValid()) { |
124 | - $this->menuItemManager->update($menu); |
|
125 | - $this->menuItemRepository->add($menu); |
|
126 | - $this->eventDispatcher->dispatch(new GenericEvent($menu), MenuEvents::MENU_CREATED); |
|
124 | + $this->menuItemManager->update($menu); |
|
125 | + $this->menuItemRepository->add($menu); |
|
126 | + $this->eventDispatcher->dispatch(new GenericEvent($menu), MenuEvents::MENU_CREATED); |
|
127 | 127 | |
128 | - return new SingleResourceResponse($menu, new ResponseContext(201)); |
|
128 | + return new SingleResourceResponse($menu, new ResponseContext(201)); |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | return new SingleResourceResponse($form, new ResponseContext(400)); |
132 | - } |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * @Route("/api/{version}/menus/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"DELETE"}, name="swp_api_core_delete_menu") |
|
136 | - */ |
|
137 | - public function deleteAction(int $id) { |
|
134 | + /** |
|
135 | + * @Route("/api/{version}/menus/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"DELETE"}, name="swp_api_core_delete_menu") |
|
136 | + */ |
|
137 | + public function deleteAction(int $id) { |
|
138 | 138 | $repository = $this->menuItemRepository; |
139 | 139 | $menu = $this->findOr404($id); |
140 | 140 | |
@@ -142,35 +142,35 @@ discard block |
||
142 | 142 | $this->eventDispatcher->dispatch(new GenericEvent($menu), MenuEvents::MENU_DELETED); |
143 | 143 | |
144 | 144 | return new SingleResourceResponse(null, new ResponseContext(204)); |
145 | - } |
|
145 | + } |
|
146 | 146 | |
147 | - /** |
|
148 | - * @Route("/api/{version}/menus/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_core_update_menu") |
|
149 | - */ |
|
150 | - public function updateAction(Request $request, $id): SingleResourceResponseInterface { |
|
147 | + /** |
|
148 | + * @Route("/api/{version}/menus/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_core_update_menu") |
|
149 | + */ |
|
150 | + public function updateAction(Request $request, $id): SingleResourceResponseInterface { |
|
151 | 151 | $menu = $this->findOr404($id); |
152 | 152 | $parent = $menu->getParent(); |
153 | 153 | $form = $this->formFactory->createNamed('', MenuType::class, $menu, ['method' => $request->getMethod()]); |
154 | 154 | $form->handleRequest($request); |
155 | 155 | if ($form->isSubmitted() && $form->isValid()) { |
156 | - $this->menuItemManager->update($menu); |
|
157 | - if($parent !== $menu->getParent()) { |
|
158 | - $this->menuItemManager->update($parent); |
|
159 | - } |
|
160 | - $this->menuItemRepository->flush(); |
|
161 | - $this->eventDispatcher->dispatch(new GenericEvent($menu), MenuEvents::MENU_UPDATED); |
|
162 | - return new SingleResourceResponse($menu); |
|
156 | + $this->menuItemManager->update($menu); |
|
157 | + if($parent !== $menu->getParent()) { |
|
158 | + $this->menuItemManager->update($parent); |
|
159 | + } |
|
160 | + $this->menuItemRepository->flush(); |
|
161 | + $this->eventDispatcher->dispatch(new GenericEvent($menu), MenuEvents::MENU_UPDATED); |
|
162 | + return new SingleResourceResponse($menu); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | return new SingleResourceResponse($form, new ResponseContext(400)); |
166 | - } |
|
166 | + } |
|
167 | 167 | |
168 | - private function findOr404($id): MenuItemInterface { |
|
168 | + private function findOr404($id): MenuItemInterface { |
|
169 | 169 | /* @var MenuItemInterface $menu */ |
170 | 170 | if (null === $menu = $this->menuItemRepository->findOneBy(['id' => $id])) { |
171 | - throw new NotFoundHttpException('Menu item was not found.'); |
|
171 | + throw new NotFoundHttpException('Menu item was not found.'); |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | return $menu; |
175 | - } |
|
175 | + } |
|
176 | 176 | } |
@@ -21,58 +21,58 @@ |
||
21 | 21 | use Twig\TwigFunction; |
22 | 22 | |
23 | 23 | class MenuExtension extends KnpMenuExtension { |
24 | - /** |
|
25 | - * {@inheritdoc} |
|
26 | - */ |
|
27 | - public function getMy($menu, array $path = [], array $options = []): ?ItemInterface { |
|
24 | + /** |
|
25 | + * {@inheritdoc} |
|
26 | + */ |
|
27 | + public function getMy($menu, array $path = [], array $options = []): ?ItemInterface { |
|
28 | 28 | try { |
29 | - return parent::get($menu, $path, $options); |
|
29 | + return parent::get($menu, $path, $options); |
|
30 | 30 | } catch (\InvalidArgumentException $e) { |
31 | - return null; |
|
31 | + return null; |
|
32 | + } |
|
32 | 33 | } |
33 | - } |
|
34 | 34 | |
35 | - /** |
|
36 | - * {@inheritdoc} |
|
37 | - */ |
|
38 | - public function render($menu, array $options = [], $renderer = null): string { |
|
35 | + /** |
|
36 | + * {@inheritdoc} |
|
37 | + */ |
|
38 | + public function render($menu, array $options = [], $renderer = null): string { |
|
39 | 39 | try { |
40 | - return parent::render($menu, $options, $renderer); |
|
40 | + return parent::render($menu, $options, $renderer); |
|
41 | 41 | } catch (\InvalidArgumentException $e) { |
42 | - // allow to render empty value |
|
43 | - return ""; |
|
42 | + // allow to render empty value |
|
43 | + return ""; |
|
44 | + } |
|
44 | 45 | } |
45 | - } |
|
46 | 46 | |
47 | - /** |
|
48 | - * {@inheritdoc} |
|
49 | - */ |
|
50 | - public function getBreadcrumbsArrayMy($menu, $subItem = null): ?array { |
|
47 | + /** |
|
48 | + * {@inheritdoc} |
|
49 | + */ |
|
50 | + public function getBreadcrumbsArrayMy($menu, $subItem = null): ?array { |
|
51 | 51 | try { |
52 | - return parent::getBreadcrumbsArray($menu, $subItem); |
|
52 | + return parent::getBreadcrumbsArray($menu, $subItem); |
|
53 | 53 | } catch (\InvalidArgumentException $e) { |
54 | - // allow to render empty value |
|
55 | - return null; |
|
54 | + // allow to render empty value |
|
55 | + return null; |
|
56 | + } |
|
56 | 57 | } |
57 | - } |
|
58 | 58 | |
59 | - /** |
|
60 | - * {@inheritdoc} |
|
61 | - */ |
|
62 | - public function getCurrentItemMy($menu): ?ItemInterface { |
|
59 | + /** |
|
60 | + * {@inheritdoc} |
|
61 | + */ |
|
62 | + public function getCurrentItemMy($menu): ?ItemInterface { |
|
63 | 63 | try { |
64 | - return parent::getCurrentItem($menu); |
|
64 | + return parent::getCurrentItem($menu); |
|
65 | 65 | } catch (\InvalidArgumentException $e) { |
66 | - return null; |
|
66 | + return null; |
|
67 | + } |
|
67 | 68 | } |
68 | - } |
|
69 | 69 | |
70 | - public function getFunctions(): array { |
|
70 | + public function getFunctions(): array { |
|
71 | 71 | return [ |
72 | 72 | new TwigFunction('knp_menu_get', [$this, 'getMy']), |
73 | 73 | new TwigFunction('knp_menu_render', [$this, 'render'], ['is_safe' => ['html']]), |
74 | 74 | new TwigFunction('knp_menu_get_breadcrumbs_array', [$this, 'getBreadcrumbsArrayMy']), |
75 | 75 | new TwigFunction('knp_menu_get_current_item', [$this, 'getCurrentItemMy']), |
76 | 76 | ]; |
77 | - } |
|
77 | + } |
|
78 | 78 | } |