PluginProbe
JCH Optimize / trunk
JCH Optimize vtrunk
6.0.2 6.0.1 trunk 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.10.0 2.2.0 2.2.1 All 58 releases
jch-optimize / src / ControllerResolver.php

ControllerResolver.php in JCH Optimize trunk, at src/ControllerResolver.php

81 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * JCH Optimize - Performs several front-end optimizations for fast downloads
5 *
6 * @package jchoptimize/wordpress-platform
7 * @author Samuel Marshall <samuel@jch-optimize.net>
8 * @copyright Copyright (c) 2021 Samuel Marshall / JCH Optimize
9 * @license GNU/GPLv3, or later. See LICENSE file
10 *
11 * If LICENSE file missing, see <http://www.gnu.org/licenses/>.
12 */
13
14 namespace JchOptimize\WordPress;
15
16 use _JchOptimizeVendor\V91\Joomla\DI\Container;
17 use _JchOptimizeVendor\V91\Joomla\DI\ContainerAwareInterface;
18 use _JchOptimizeVendor\V91\Joomla\DI\ContainerAwareTrait;
19 use _JchOptimizeVendor\V91\Joomla\Input\Input;
20 use InvalidArgumentException;
21 use JchOptimize\Core\Mvc\Controller;
22
23 use function call_user_func;
24 use function is_null;
25
26 class ControllerResolver implements ContainerAwareInterface
27 {
28 use ContainerAwareTrait;
29
30 private Input $input;
31
32 public function __construct(Container $container, Input $input)
33 {
34 $this->container = $container;
35 $this->input = $input;
36 }
37
38 /**
39 */
40 public function resolve(): void
41 {
42 call_user_func([$this->getController(), 'execute']);
43 }
44
45 private function getControllerKey(): string
46 {
47 /** @var string|null $task */
48 $task = $this->input->get('task');
49
50 if (!is_null($task)) {
51 return $task;
52 }
53
54 /** @var string|null $view */
55 $view = $this->input->get('view');
56 /** @var string|null $tab */
57 $tab = $this->input->get('tab');
58
59 if (!is_null($tab) && is_null($view)) {
60 return $tab;
61 }
62
63 if (!is_null($view)) {
64 return $view;
65 }
66
67 return 'main';
68 }
69
70 public function getController(): Controller
71 {
72 $key = $this->getControllerKey();
73
74 if ($this->container->has($key)) {
75 return $this->container->get($key);
76 } else {
77 throw new InvalidArgumentException(sprintf('Cannot resolve controller aliased: %s', $key));
78 }
79 }
80 }
81