3 namespace Org\Mxchange\CoreFramework\Resolver\Command;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Command\Commandable;
8 use Org\Mxchange\CoreFramework\Command\InvalidCommandException;
9 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
10 use Org\Mxchange\CoreFramework\Request\Requestable;
11 use Org\Mxchange\CoreFramework\Resolver\BaseResolver;
14 use \InvalidArgumentException;
15 use \UnexpectedValueException;
18 * A generic command resolver class
20 * @author Roland Haeder <webmaster@shipsimu.org>
22 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 abstract class BaseCommandResolver extends BaseResolver {
41 * Protected constructor
43 * @param $className Name of the class
46 protected function __construct ($className) {
47 // Call parent constructor
48 parent::__construct($className);
52 * "Loads" a given command and instances it if not yet cached
54 * @param $commandName A command name we shall look for
55 * @return $commandInstance A loaded command instance
56 * @throws InvalidCommandException Thrown if even the default
57 * command class is missing (bad!)
59 protected function loadCommand ($commandName) {
60 // Init command instance
61 $commandInstance = NULL;
66 $this->getNamespace(),
67 $this->getCapitalizedClassPrefix(),
68 self::convertToClassName($commandName)
71 // Create command class name
72 $this->setClassName($className);
74 // Is this class loaded?
75 if (!class_exists($this->getClassName())) {
76 // Class not found, so throw an exception
77 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
80 // Initiate the command
81 $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this));
84 return $commandInstance;
88 * Returns an command instance for a given request class or null if
91 * @param $requestInstance An instance of a Requestable class
92 * @return $commandInstance An instance of the resolved command
93 * @throws InvalidCommandException Thrown if $commandName is invalid
94 * @throws UnexpectedValueException Thrown if $commandInstance is an invalid instance
96 public function resolveCommandByRequest (Requestable $requestInstance) {
99 $commandInstance = NULL;
101 // This goes fine so let's resolve the command
102 $commandName = $requestInstance->getRequestElement('command');
104 // Is the command empty? Then fall back to default command
105 if (empty($commandName)) {
106 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_command');
109 // Check if command is valid
110 if ($this->isCommandValid($this->getNamespace(), $commandName) === false) {
111 // This command is invalid!
112 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
116 $commandInstance = $this->loadCommand($commandName);
119 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
120 // This command has an invalid instance!
121 throw new UnexpectedValueException(sprintf('commandInstance for commandName=%s is not object (%s) or does not implement Commandable.', $commandName, gettype($commandInstance)), self::EXCEPTION_INVALID_COMMAND);
125 $this->setResolvedInstance($commandInstance);
127 // Return the resolved command instance
128 return $commandInstance;
132 * Resolves the command by its direct name and returns an instance of its class
134 * @param $namespace Namespace to look in
135 * @param $commandName The direct command name we shall resolve
136 * @return $commandInstance An instance of the command class
137 * @throws InvalidCommandException Thrown if $commandName is invalid
139 public function resolveCommand ($namespace, $commandName) {
141 if (empty($namespace)) {
142 // Then thrown an exception here
143 throw new InvalidArgumentException('Parameter "namespace" is empty');
144 } elseif (empty($actionName)) {
145 // Then thrown an exception here
146 throw new InvalidArgumentException('Parameter "actionName" is empty');
149 // Initiate the instance variable
150 $commandInstance = NULL;
152 // Is the command empty? Then fall back to default command
153 if (empty($commandName)) {
154 // Init default command
155 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_command');
158 // Check if command is valid
159 if ($this->isCommandValid($namespace, $commandName) === false) {
160 // This command is invalid!
161 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
165 $commandInstance = $this->loadCommand($commandName);
167 // Return the instance
168 return $commandInstance;
172 * Checks whether the given command is valid
174 * @param $namespace Namespace to look in
175 * @param $commandName The default command we shall execute
176 * @return $isValid Whether the given command is valid
177 * @throws InvalidArgumentException Thrown if given command is not set
179 protected function isCommandValid ($namespace, $commandName) {
180 // By default nothing shall be valid
183 // Is namespace and command name set?
184 if (empty($namespace)) {
185 // Then thrown an exception here
186 throw new InvalidArgumentException('Parameter "namespace" is empty');
187 } elseif (empty($commandName)) {
188 // Then thrown an exception here
189 throw new InvalidArgumentException('Parameter "commandName" is empty');
192 // Create the full class name
193 $className = sprintf(
196 $this->getCapitalizedClassPrefix(),
197 self::convertToClassName($commandName)
200 // Now, let us create the full name of the command class
201 $this->setClassName($className);
203 // Is this class already loaded?
204 if (class_exists($this->getClassName())) {
205 // This class does exist. :-)
210 $this->setCommandName($commandName);