0c4841b6a04cba3c91d63ca47239c8a4c7141477
[core.git] / inc / main / classes / resolver / action / class_BaseActionResolver.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Resolver\Action;
4
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Resolver\BaseResolver;
8
9 /**
10  * A generic action resolver class
11  *
12  * @author              Roland Haeder <webmaster@shipsimu.org>
13  * @version             0.0.0
14  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15  * @license             GNU GPL 3.0 or any newer version
16  * @link                http://www.shipsimu.org
17  *
18  * This program is free software: you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation, either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful,
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26  * GNU General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program. If not, see <http://www.gnu.org/licenses/>.
30  */
31 class BaseActionResolver extends BaseResolver {
32         /**
33          * Validated action name
34          */
35         private $actionName = '';
36
37         /**
38          * Protected constructor
39          *
40          * @param       $className      Name of the class
41          * @return      void
42          */
43         protected function __construct ($className) {
44                 // Call parent constructor
45                 parent::__construct($className);
46         }
47
48         /**
49          * Setter for action name
50          *
51          * @param       $actionName     Last validated action name
52          * @return      void
53          */
54         protected final function setActionName ($actionName) {
55                 $this->actionName = (string) $actionName;
56         }
57
58         /**
59          * Getter for action name
60          *
61          * @return      $actionName     Last validated action name
62          */
63         public final function getActionName () {
64                 return $this->actionName;
65         }
66
67         /**
68          * Checks whether the given action is valid
69          *
70          * @param       $actionName     The default action we shall execute
71          * @return      $isValid                Whether the given action is valid
72          * @throws      EmptyVariableException  Thrown if given action is not set
73          */
74         public function isActionValid ($actionName) {
75                 // By default nothing shall be valid
76                 $isValid = FALSE;
77
78                 // Is a action set?
79                 if (empty($actionName)) {
80                         // Then thrown an exception here
81                         throw new EmptyVariableException(array($this, 'actionName'), self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
82                 } // END - if
83
84                 // Create class name
85                 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($actionName) . 'Action';
86
87                 // Now, let us create the full name of the action class
88                 $this->setClassName($className);
89
90                 // Is this class already loaded?
91                 if (class_exists($this->getClassName())) {
92                         // This class does exist. :-)
93                         $isValid = TRUE;
94                 } // END - if
95
96                 // Set action name
97                 $this->setActionName($actionName);
98
99                 // Return the result
100                 return $isValid;
101         }
102
103         /**
104          * "Loads" current action and instances it if not yet cached
105          *
106          * @return      $actionInstance                 A loaded action instance
107          */
108         protected function loadAction () {
109                 // Init action instance
110                 $actionInstance = NULL;
111
112                 // Create action class name
113                 $className = $this->getCapitalizedClassPrefix() . self::convertToClassName($this->getActionName()) . 'Action';
114
115                 // ... and set it
116                 $this->setClassName($className);
117
118                 // Initiate the action
119                 $actionInstance = ObjectFactory::createObjectByName($this->getClassName(), array($this));
120
121                 // Return the result
122                 return $actionInstance;
123         }
124
125 }