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