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\Object\ObjectFactory;
10 use Org\Mxchange\CoreFramework\Request\Requestable;
11 use Org\Mxchange\CoreFramework\Resolver\BaseResolver;
12 use Org\Mxchange\CoreFramework\Utils\String\StringUtils;
15 use \InvalidArgumentException;
16 use \UnexpectedValueException;
19 * A generic command resolver class
21 * @author Roland Haeder <webmaster@shipsimu.org>
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.shipsimu.org
27 * This program is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program. If not, see <http://www.gnu.org/licenses/>.
40 abstract class BaseCommandResolver extends BaseResolver {
44 private $commandName = '';
47 * Protected constructor
49 * @param $className Name of the class
52 protected function __construct (string $className) {
53 // Call parent constructor
54 parent::__construct($className);
58 * Setter for command name
60 * @param $commandName Last validated command name
63 protected final function setCommandName (string $commandName) {
64 $this->commandName = $commandName;
68 * Getter for command name
70 * @return $commandName Last validated command name
72 public final function getCommandName () {
73 return $this->commandName;
77 * "Loads" a given command and instances it if not yet cached
79 * @param $commandName A command name we shall look for
80 * @return $commandInstance A loaded command instance
81 * @throws InvalidCommandException Thrown if even the default
82 * command class is missing (bad!)
84 protected function loadCommand (string $commandName) {
85 // Init command instance
86 $commandInstance = NULL;
91 $this->getNamespace(),
92 $this->getCapitalizedClassPrefix(),
93 StringUtils::convertToClassName($commandName)
96 // Create command class name
97 $this->setClassName($className);
99 // Is this class loaded?
100 if (!class_exists($this->getClassName())) {
101 // Class not found, so throw an exception
102 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
105 // Initiate the command
106 $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), [$this]);
109 return $commandInstance;
113 * Returns an command instance for a given request class or null if
116 * @param $requestInstance An instance of a Requestable class
117 * @return $commandInstance An instance of the resolved command
118 * @throws InvalidCommandException Thrown if $commandName is invalid
119 * @throws UnexpectedValueException Thrown if $commandInstance is an invalid instance
121 public function resolveCommandByRequest (Requestable $requestInstance) {
123 $commandName = $requestInstance->getRequestElement('command');
124 $commandType = FrameworkBootstrap::getRequestTypeFromSystem();
125 $commandInstance = NULL;
127 // Is the command empty? Then fall back to default command
128 if (empty($commandName)) {
129 // Fall back to default command
130 $commandName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry(sprintf('default_%s_command', $commandType));
133 // Check if command is valid
134 if ($this->isCommandValid($this->getNamespace(), $commandName) === false) {
135 // This command is invalid!
136 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
140 $commandInstance = $this->loadCommand($commandName);
143 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
144 // This command has an invalid instance!
145 throw new UnexpectedValueException(sprintf('commandInstance for commandName=%s is not object (%s) or does not implement Commandable.', $commandName, gettype($commandInstance)), self::EXCEPTION_INVALID_COMMAND);
149 $this->setResolvedInstance($commandInstance);
151 // Init template engine
152 $commandInstance->initTemplateEngine($commandType);
154 // Return the resolved command instance
155 return $commandInstance;
159 * Resolves the command by its direct name and returns an instance of its class
161 * @param $namespace Namespace to look in
162 * @param $commandName The direct command name we shall resolve
163 * @return $commandInstance An instance of the command class
164 * @throws InvalidCommandException Thrown if $commandName is invalid
166 public function resolveCommand (string $namespace, string $commandName) {
168 if (empty($namespace)) {
169 // Then thrown an exception here
170 throw new InvalidArgumentException('Parameter "namespace" is empty');
171 } elseif (empty($commandName)) {
172 // Then thrown an exception here
173 throw new InvalidArgumentException('Parameter "commandName" is empty');
176 // Initiate the instance variable
177 $commandInstance = NULL;
178 $commandType = FrameworkBootstrap::getRequestTypeFromSystem();
180 // Is the command empty? Then fall back to default command
181 if (empty($commandName)) {
182 // Init default command
183 $commandName = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry(sprintf('default_%s_command', $commandType));
186 // Check if command is valid
187 if ($this->isCommandValid($namespace, $commandName) === false) {
188 // This command is invalid!
189 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
193 $commandInstance = $this->loadCommand($commandName);
195 // Init template engine
196 $commandInstance->initTemplateEngine($commandType);
198 // Return the instance
199 return $commandInstance;
203 * Checks whether the given command is valid
205 * @param $namespace Namespace to look in
206 * @param $commandName The default command we shall execute
207 * @return $isValid Whether the given command is valid
208 * @throws InvalidArgumentException Thrown if given command is not set
210 protected function isCommandValid (string $namespace, string $commandName) {
211 // Is namespace and command name set?
212 if (empty($namespace)) {
213 // Then thrown an exception here
214 throw new InvalidArgumentException('Parameter "namespace" is empty');
215 } elseif (empty($commandName)) {
216 // Then thrown an exception here
217 throw new InvalidArgumentException('Parameter "commandName" is empty');
220 // By default nothing shall be valid
223 // Create the full class name
224 $className = sprintf(
227 $this->getCapitalizedClassPrefix(),
228 StringUtils::convertToClassName($commandName)
231 // Now, let us create the full name of the command class
232 $this->setClassName($className);
234 // Is this class already loaded?
235 if (class_exists($this->getClassName())) {
236 // This class does exist. :-)
241 $this->setCommandName($commandName);