source: branches/2.2/workflow/lib/factory/Factory.php @ 3167

Revision 3167, 4.0 KB checked in by viani, 14 years ago (diff)

Ticket #1135 - Merged r1990:3166 from /trunk/workflow into /branches/2.2/workflow

  • Property svn:executable set to *
Line 
1<?php
2/**************************************************************************\
3* eGroupWare                                                               *
4* http://www.egroupware.org                                                *
5* --------------------------------------------                             *
6*  This program is free software; you can redistribute it and/or modify it *
7*  under the terms of the GNU General Public License as published by the   *
8*  Free Software Foundation; either version 2 of the License, or (at your  *
9*  option) any later version.                                              *
10\**************************************************************************/
11
12/**
13 * The Factory frontend class.
14 * This class controls which concrete Factory
15 * will be used, depending on the current
16 * 'security mode'. It lazy instantiates both
17 * factories (process and module) when they are
18 * required, and stores these objects. All the
19 * accesses to factories are done through this class,
20 * implementing a kind of Proxy design pattern.
21 * This class depends on Security frontend class
22 * to decide which factory to call.
23 *
24 * @package Factory
25 * @license http://www.gnu.org/copyleft/gpl.html GPL
26 * @author Pedro Eugênio Rocha - pedro.eugenio.rocha@gmail.com
27 */
28class Factory {
29
30
31        /**
32         * @var object $_unsecuredFactory Stores WorkflowFactory object.
33         * @access private
34         * @static
35         */
36        private static $_unsecuredFactory = null;
37
38
39        /**
40         * @var object $_securedFactory Stores ProcessFactory object.
41         * @access private
42         * @static
43         */
44        private static $_securedFactory = null;
45
46
47        /**
48         * Constructor. Just disable direct instantiation.
49         *
50         * @access public
51         * @return void
52         * @static
53         */
54        public function __construct() {
55                throw new Exception("Oops! Static only class.");
56        }
57
58
59        /**
60         * Just forward this call.
61         *
62         * @access public
63         * @return object
64         * @static
65         */
66        public static function &getInstance() {
67
68                $args = func_get_args();
69                return self::_callMethod(__FUNCTION__, $args);
70        }
71
72
73        /**
74         * Just forward this call.
75         *
76         * @access public
77         * @return object
78         * @static
79         */
80        public static function &newInstance() {
81
82                $args = func_get_args();
83                return self::_callMethod(__FUNCTION__, $args);
84        }
85
86
87        /**
88         * Selecting the proper factory to call. This function
89         * should never be called with a random $methodName. Allowed
90         * values are 'getInstance' and 'newInstance'.
91         *
92         * @param string $methodName Name of the BaseFactory method to call.
93         * @param array $args Parameters to class's constructor.
94         * @access private
95         * @return object
96         * @static
97         */
98        private static function &_callMethod($methodName, $args) {
99
100                /* security off (module space) */
101                if (!Security::isEnabled()) {
102
103                        /* it must be instatiated */
104                        if (is_null(self::$_unsecuredFactory))
105                                self::$_unsecuredFactory = new WorkflowFactory();
106
107                        $className = array_shift($args);
108                        return self::$_unsecuredFactory->$methodName($className, $args);
109                }
110                /* oops. we are in the process space (restricted). */
111                else {
112
113                        /* it must be instatiated */
114                        if (is_null(self::$_securedFactory))
115                                self::$_securedFactory = new ProcessFactory();
116
117                        $className = array_shift($args);
118
119                        /**
120                         * If the class is not allowed, we must check who is trying
121                         * to instantiate it. If it's a module guy, let's allow him.
122                         * Throw up the exception otherwise.
123                         */
124                        try {
125                                $obj = &self::$_securedFactory->$methodName($className, $args);
126                        }
127
128                        /**
129                         * We are erroneously catching any exceptions. We should catch only the 'class not allowed'
130                         * types of exceptions. To do so, a custom exception class must be defined.
131                         */
132                        catch(Exception $e) {
133
134                                /**
135                                 * Here we are using depth 2 in isSafeDir method, because we are on a private
136                                 * method. Thus, we need to know if the "caller's caller's" function is on a
137                                 * safe dir, instead of the direct caller's method.
138                                 */
139                                if (Security::isSafeDir(2))
140                                        $obj = &self::$_unsecuredFactory->$methodName($className, $args);
141
142                                /* naaasty one. take this! */
143                                else
144                                        throw($e);
145                        }
146
147                        // finally
148                        return $obj;
149                }
150        }
151}
152
153?>
Note: See TracBrowser for help on using the repository browser.