| 61 | | $includes = array(); |
| 62 | | $inits = array(); |
| 63 | | $instances = array(); |
| 64 | | |
| 65 | | // let's do our fancy work |
| 66 | | foreach ($factories as $factory) |
| 67 | | { |
| 68 | | |
| 69 | | // see if the factory exists |
| 70 | | if (!isset($ini[$factory])) |
| 71 | | { |
| 72 | | |
| 73 | | // factory hasn't been registered |
| 74 | | $error = 'Configuration file "%s" must register "%s" factory'; |
| 75 | | $error = sprintf($config, $factory); |
| 76 | | |
| 77 | | throw new ParseException($error); |
| 78 | | |
| 79 | | } |
| 80 | | |
| 81 | | // get factory keys |
| 82 | | $keys = $ini[$factory]; |
| 83 | | |
| 84 | | if (!isset($keys['class'])) |
| 85 | | { |
| 86 | | |
| 87 | | // missing class key |
| 88 | | $error = 'Configuration file "%s" specifies category ' . |
| 89 | | '"%s" with missing class key'; |
| 90 | | $error = sprintf($error, $config, $factory); |
| 91 | | |
| 92 | | throw new ParseException($error); |
| 93 | | |
| 94 | | } |
| 95 | | |
| 96 | | $class = $keys['class']; |
| 97 | | |
| 98 | | if (isset($keys['file'])) |
| 99 | | { |
| 100 | | |
| 101 | | // we have a file to include |
| 102 | | $file =& $keys['file']; |
| 103 | | $file = $this->replaceConstants($file); |
| 104 | | $file = $this->replacePath($file); |
| 105 | | |
| 106 | | if (!is_readable($file)) |
| 107 | | { |
| 108 | | |
| 109 | | // factory file doesn't exist |
| 110 | | $error = 'Configuration file "%s" specifies class ' . |
| 111 | | '"%s" with nonexistent or unreadablefile ' . |
| 112 | | '"%s"'; |
| 113 | | $error = sprintf($error, $config, $class, $file); |
| 114 | | |
| 115 | | throw new ParseException($error); |
| 116 | | |
| | 65 | $controllers = array(); |
| | 66 | |
| | 67 | // check that every controller has the right paramers |
| | 68 | foreach($ini as $controllerName => $factories) { |
| | 69 | // init our data and includes arrays |
| | 70 | $includes = array(); |
| | 71 | $inits = array(); |
| | 72 | $instances = array(); |
| | 73 | |
| | 74 | // Build all classes |
| | 75 | foreach($required_factories as $factory) { |
| | 76 | if (!array_key_exists($factory, $factories)) { |
| | 77 | $error = 'Configuration file "%s" is missing "%s" key in "%s" category'; |
| | 78 | $error = sprintf($error, $config, $factory, $controllerName); |
| | 79 | throw new ParseException($error); |
| | 80 | } |
| | 81 | |
| | 82 | // Get class name |
| | 83 | $class = $factories[$factory]; |
| | 84 | |
| | 85 | // parse parameters |
| | 86 | $parameters = ParameterParser::parse($factories, $factory .'.param'); |
| | 87 | |
| | 88 | // append new data |
| | 89 | switch ($factory) { |
| | 90 | case 'request': |
| | 91 | |
| | 92 | // append instance creation |
| | 93 | $tmp = "\t\$this->request = " . "Request::newInstance('%s');"; |
| | 94 | $instances[] = sprintf($tmp, $class); |
| | 95 | |
| | 96 | // append instance initialization |
| | 97 | $tmp = "\t\$this->request->initialize(\$this->context, " . "%s);"; |
| | 98 | $inits[] = sprintf($tmp, $parameters); |
| | 99 | |
| | 100 | break; |
| | 101 | case 'security_filter': |
| | 102 | |
| | 103 | // append creation/initialization in one swipe |
| | 104 | $tmp = "\n\tif (MO_USE_SECURITY)\n\t{\n" . |
| | 105 | "\t\t\$this->securityFilter = " . |
| | 106 | "SecurityFilter::newInstance('%s');\n" . |
| | 107 | "\t\t\$this->securityFilter->initialize(" . |
| | 108 | "\$this->context, %s);\n\t}\n"; |
| | 109 | $inits[] = sprintf($tmp, $class, $parameters); |
| | 110 | |
| | 111 | break; |
| | 112 | case 'storage': |
| | 113 | |
| | 114 | // append instance creation |
| | 115 | $tmp = "\t\$this->storage = " . |
| | 116 | "Storage::newInstance('%s');"; |
| | 117 | $instances[] = sprintf($tmp, $class); |
| | 118 | |
| | 119 | // append instance initialization |
| | 120 | $tmp = "\t\$this->storage->initialize(\$this->context, " . "%s);"; |
| | 121 | $inits[] = sprintf($tmp, $parameters); |
| | 122 | |
| | 123 | break; |
| | 124 | case 'user': |
| | 125 | |
| | 126 | // append instance creation |
| | 127 | $tmp = "\t\$this->user = User::newInstance('%s');"; |
| | 128 | $instances[] = sprintf($tmp, $class); |
| | 129 | |
| | 130 | // append instance initialization |
| | 131 | $tmp = "\t\$this->user->initialize(\$this->context, %s);"; |
| | 132 | $inits[] = sprintf($tmp, $parameters); |
| | 133 | break; |
| | 134 | default: |
| | 135 | continue; |
| 119 | | // append our data |
| 120 | | $tmp = "require_once('%s');"; |
| 121 | | $includes[] = sprintf($tmp, $file); |
| 122 | | |
| | 138 | if (isset($factories[$factory.'.file'])) { |
| | 139 | // we have a file to include |
| | 140 | $file =& $factories[$factory.'.file']; |
| | 141 | $file = $this->replaceConstants($file); |
| | 142 | $file = $this->replacePath($file); |
| | 143 | |
| | 144 | if (!is_readable($file)) { |
| | 145 | |
| | 146 | // factory file doesn't exist |
| | 147 | $error = 'Configuration file "%s" specifies class ' . |
| | 148 | '"%s" with nonexistent or unreadablefile ' . |
| | 149 | '"%s"'; |
| | 150 | $error = sprintf($error, $config, $class, $file); |
| | 151 | |
| | 152 | throw new ParseException($error); |
| | 153 | |
| | 154 | } |
| | 155 | |
| | 156 | // append our data |
| | 157 | $tmp = "\trequire_once('%s');"; |
| | 158 | $includes[] = sprintf($tmp, $file); |
| | 159 | |
| | 160 | } |
| 124 | | |
| 125 | | // parse parameters |
| 126 | | $parameters = ParameterParser::parse($keys); |
| 127 | | |
| 128 | | // append new data |
| 129 | | switch ($factory) |
| 130 | | { |
| 131 | | |
| 132 | | case 'request': |
| 133 | | |
| 134 | | // append instance creation |
| 135 | | $tmp = "\$this->request = " . |
| 136 | | "Request::newInstance('%s');"; |
| 137 | | $instances[] = sprintf($tmp, $class); |
| 138 | | |
| 139 | | // append instance initialization |
| 140 | | $tmp = "\$this->request->initialize(\$this->context, " . |
| 141 | | "%s);"; |
| 142 | | $inits[] = sprintf($tmp, $parameters); |
| 143 | | |
| 144 | | break; |
| 145 | | |
| 146 | | case 'security_filter': |
| 147 | | |
| 148 | | // append creation/initialization in one swipe |
| 149 | | $tmp = "\nif (MO_USE_SECURITY)\n{\n" . |
| 150 | | "\t\$this->securityFilter = " . |
| 151 | | "SecurityFilter::newInstance('%s');\n" . |
| 152 | | "\t\$this->securityFilter->initialize(" . |
| 153 | | "\$this->context, %s);\n}\n"; |
| 154 | | $inits[] = sprintf($tmp, $class, $parameters); |
| 155 | | |
| 156 | | break; |
| 157 | | |
| 158 | | case 'storage': |
| 159 | | |
| 160 | | // append instance creation |
| 161 | | $tmp = "\$this->storage = " . |
| 162 | | "Storage::newInstance('%s');"; |
| 163 | | $instances[] = sprintf($tmp, $class); |
| 164 | | |
| 165 | | // append instance initialization |
| 166 | | $tmp = "\$this->storage->initialize(\$this->context, " . |
| 167 | | "%s);"; |
| 168 | | $inits[] = sprintf($tmp, $parameters); |
| 169 | | |
| 170 | | break; |
| 171 | | |
| 172 | | case 'user': |
| 173 | | |
| 174 | | // append instance creation |
| 175 | | $tmp = "\$this->user = User::newInstance('%s');"; |
| 176 | | $instances[] = sprintf($tmp, $class); |
| 177 | | |
| 178 | | // append instance initialization |
| 179 | | $tmp = "\$this->user->initialize(\$this->context, %s);"; |
| 180 | | $inits[] = sprintf($tmp, $parameters); |
| 181 | | |
| 182 | | } |
| 183 | | |
| | 162 | |
| | 163 | // context creation |
| | 164 | $context = "\t\$this->context = new Context(%s, %s, %s, %s, %s);"; |
| | 165 | $context = sprintf($context, '$this', '$this->request', '$this->user', '$this->storage', |
| | 166 | '$this->databaseManager'); |
| | 167 | |
| | 168 | $tmp = "if (\$this instanceof $controllerName)\n{\n%s\n%s\n%s\n%s\n\treturn;\n}"; |
| | 169 | $controllers[] = sprintf($tmp, implode("\n", $includes), |
| | 170 | implode("\n", $instances), $context, implode("\n", $inits)); |