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 $commandName The direct command name we shall resolve
135 * @return $commandInstance An instance of the command class
136 * @throws InvalidCommandException Thrown if $commandName is invalid
138 public function resolveCommand ($commandName) {
139 // Initiate the instance variable
140 $commandInstance = NULL;
142 // Is the command empty? Then fall back to default command
143 if (empty($commandName)) {
144 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_command');
147 // Check if command is valid
148 if ($this->isCommandValid($commandName) === false) {
149 // This command is invalid!
150 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
154 $commandInstance = $this->loadCommand($commandName);
156 // Return the instance
157 return $commandInstance;
161 * Checks whether the given command is valid
163 * @param $namespace Namespace to look in
164 * @param $commandName The default command we shall execute
165 * @return $isValid Whether the given command is valid
166 * @throws InvalidArgumentException Thrown if given command is not set
168 protected function isCommandValid ($namespace, $commandName) {
169 // By default nothing shall be valid
172 // Is namespace and command name set?
173 if (empty($namespace)) {
174 // Then thrown an exception here
175 throw new InvalidArgumentException('Parameter "namespace" is empty');
176 } elseif (empty($commandName)) {
177 // Then thrown an exception here
178 throw new InvalidArgumentException('Parameter "commandName" is empty');
181 // Create the full class name
182 $className = sprintf(
185 $this->getCapitalizedClassPrefix(),
186 self::convertToClassName($commandName)
189 // Now, let us create the full name of the command class
190 $this->setClassName($className);
192 // Is this class already loaded?
193 if (class_exists($this->getClassName())) {
194 // This class does exist. :-)
199 $this->setCommandName($commandName);