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 <<<<<<< HEAD:framework/main/classes/resolver/command/class_BaseCommandResolver.php
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
25 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
26 >>>>>>> Some updates::inc/main/classes/resolver/command/class_BaseCommandResolver.php
27 * @license GNU GPL 3.0 or any newer version
28 * @link http://www.shipsimu.org
30 * This program is free software: you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation, either version 3 of the License, or
33 * (at your option) any later version.
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
40 * You should have received a copy of the GNU General Public License
41 * along with this program. If not, see <http://www.gnu.org/licenses/>.
43 abstract class BaseCommandResolver extends BaseResolver {
45 * Protected constructor
47 * @param $className Name of the class
50 protected function __construct ($className) {
51 // Call parent constructor
52 parent::__construct($className);
56 * "Loads" a given command and instances it if not yet cached
58 * @param $commandName A command name we shall look for
59 * @return $commandInstance A loaded command instance
60 * @throws InvalidCommandException Thrown if even the default
61 * command class is missing (bad!)
63 protected function loadCommand ($commandName) {
64 // Init command instance
65 $commandInstance = NULL;
70 $this->getNamespace(),
71 $this->getCapitalizedClassPrefix(),
72 self::convertToClassName($commandName)
75 // Create command class name
76 $this->setClassName($className);
78 // Is this class loaded?
79 if (!class_exists($this->getClassName())) {
80 // Class not found, so throw an exception
81 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
84 // Initiate the command
85 $commandInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this));
88 return $commandInstance;
92 * Returns an command instance for a given request class or null if
95 * @param $requestInstance An instance of a Requestable class
96 * @return $commandInstance An instance of the resolved command
97 * @throws InvalidCommandException Thrown if $commandName is invalid
98 * @throws UnexpectedValueException Thrown if $commandInstance is an invalid instance
100 public function resolveCommandByRequest (Requestable $requestInstance) {
103 $commandInstance = NULL;
105 // This goes fine so let's resolve the command
106 $commandName = $requestInstance->getRequestElement('command');
108 // Is the command empty? Then fall back to default command
109 if (empty($commandName)) {
110 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_command');
113 // Check if command is valid
114 if ($this->isCommandValid($this->getNamespace(), $commandName) === false) {
115 // This command is invalid!
116 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
120 $commandInstance = $this->loadCommand($commandName);
123 if ((!is_object($commandInstance)) || (!$commandInstance instanceof Commandable)) {
124 // This command has an invalid instance!
125 throw new UnexpectedValueException(sprintf('commandInstance for commandName=%s is not object (%s) or does not implement Commandable.', $commandName, gettype($commandInstance)), self::EXCEPTION_INVALID_COMMAND);
129 $this->setResolvedInstance($commandInstance);
131 // Return the resolved command instance
132 return $commandInstance;
136 * Resolves the command by its direct name and returns an instance of its class
138 * @param $commandName The direct command name we shall resolve
139 * @return $commandInstance An instance of the command class
140 * @throws InvalidCommandException Thrown if $commandName is invalid
142 public function resolveCommand ($commandName) {
143 // Initiate the instance variable
144 $commandInstance = NULL;
146 // Is the command empty? Then fall back to default command
147 if (empty($commandName)) {
148 $commandName = $this->getConfigInstance()->getConfigEntry('default_' . FrameworkBootstrap::getRequestTypeFromSystem() . '_command');
151 // Check if command is valid
152 if ($this->isCommandValid($commandName) === false) {
153 // This command is invalid!
154 throw new InvalidCommandException(array($this, $commandName), self::EXCEPTION_INVALID_COMMAND);
158 $commandInstance = $this->loadCommand($commandName);
160 // Return the instance
161 return $commandInstance;
165 * Checks whether the given command is valid
167 * @param $namespace Namespace to look in
168 * @param $commandName The default command we shall execute
169 * @return $isValid Whether the given command is valid
170 * @throws InvalidArgumentException Thrown if given command is not set
172 protected function isCommandValid ($namespace, $commandName) {
173 // By default nothing shall be valid
176 // Is namespace and command name set?
177 if (empty($namespace)) {
178 // Then thrown an exception here
179 throw new InvalidArgumentException('Parameter "namespace" is empty');
180 } elseif (empty($commandName)) {
181 // Then thrown an exception here
182 throw new InvalidArgumentException('Parameter "commandName" is empty');
185 // Create the full class name
186 $className = sprintf(
189 $this->getCapitalizedClassPrefix(),
190 self::convertToClassName($commandName)
193 // Now, let us create the full name of the command class
194 $this->setClassName($className);
196 // Is this class already loaded?
197 if (class_exists($this->getClassName())) {
198 // This class does exist. :-)
203 $this->setCommandName($commandName);