/** * Invoke listeners */ public static function invokeListeners($listeners, $data, $event) { if (!property_exists("AppConfiguration", "MODEL_LISTENERS")) { return; } // Fire off events to the listeners foreach ($listeners as $listener) { $listener_class = $listener["listener"]; $listener_file = WEBAPP_ROOT . "/models/listeners/" . Inflector::decamelize($listener["listener"]) . ".php"; if (!class_exists($listener_class)) { require_once $listener_file; } $listener_impl = new $listener_class(); foreach ($data as $item) { // Invoke the listener $listener_impl->{$event}($item); } } }
/** * Tests Inflector::decamelize() * * @test * @dataProvider provider_decamelize * @param string Camelized string * @param string Glue * @param string Expected string */ public function test_decamelize($input, $glue, $expected) { $this->assertSame($expected, Inflector::decamelize($input, $glue)); }
public static final function executeController($controllerScript, $controllerFunction, $controllerParams, $requestURI, $methodParameters, $queryString, $presetVariables = array()) { // First enable the API, if used if (property_exists('AppConfiguration', 'API')) { if (!empty(AppConfiguration::$API)) { // Include the APIController, which will load itself in the routes as well require_once LIBS_ROOT . "/api.php"; } } // Check routing $routes = array(); if (property_exists('AppConfiguration', 'ROUTES')) { $routes = AppConfiguration::$ROUTES; } if (file_exists(WEBAPP_ROOT . "/tmp/autoroutes.php")) { require_once WEBAPP_ROOT . "/tmp/autoroutes.php"; $routes = array_merge($routes, AutoRoutes::$AUTO_ROUTES); } /** * Check if the request is mapped normally or if we have a routing rule for it */ if (!empty($routes["/{$controllerScript}/{$controllerFunction}"])) { /* Use routing */ $route = $routes["/{$controllerScript}/{$controllerFunction}"]; $controllerFilename = $route["file"]; $controllerClass = $route["class"]; } else { /* Normal mapping */ // Find the controller class $controllerFilename = WEBAPP_ROOT . "/controllers/" . $controllerScript . "_controller.php"; $controllerClass = ucfirst($controllerScript) . "Controller"; } if (!file_exists($controllerFilename)) { controllerErrors::missingcontroller($controllerScript); } require_once $controllerFilename; /* * Populate some variable to the controller */ $controller = new $controllerClass(); $controller->_requestURI = $requestURI; $controller->params = $controllerParams; $controller->controllerName = strtolower($controllerScript); /* Set the controller to the context */ MVCContext::getContext()->setController(&$controller); if (!empty($presetVariables)) { $controller->_contextVariables = $presetVariables; } // Initialize the controller $controller->_init(); /* * See if the requested method exists. If not, we try to find * a method with the name _default to call, which is a "catch-all" -method */ if (!method_exists($controller, $controllerFunction)) { if (!method_exists($controller, "_default")) { controllerErrors::missingMethod($controllerScript, $controllerFunction); } else { $methodParameters = array_merge(array($controllerScript, "default", $controllerFunction), array_slice($methodParameters, 3)); $controllerFunction = "_default"; } } /* * Populate input */ $input = array(); if (!empty($_POST)) { $input = $_POST; $controller->inputmethod = INPUT_METHOD_POST; } else { parse_str($queryString, $input); $controller->inputmethod = INPUT_METHOD_GET; } /* * Begin the execution by processing any filters */ if (property_exists('AppConfiguration', 'FILTERS')) { foreach (AppConfiguration::$FILTERS as $filter) { if ($filter["target"][0] == $controllerScript || $filter["target"][0] == "*") { // Controller -part matches, see if the action matches if ($filter["target"][1] == $controllerFunction || $filter["target"][1] == "*" || is_array($filter["target"][1]) && in_array($controllerFunction, $filter["target"][1])) { // Matches, execute the filter if (!class_exists($filter["filter"])) { require_once WEBAPP_ROOT . "/filters/" . Inflector::decamelize($filter["filter"]) . ".php"; } /* * Invoke the filter. We provide as parameter the controller name, action name and the input * sent from the browser. The input is passed by reference so it can be modified * by the filter along with any parameters defined in the AppConfiguration */ $filterClass = $filter["filter"]; $filterImpl = new $filterClass(); if (call_user_func_array(array($filterImpl, "processFilter"), array($controllerScript, $controllerFunction, $filter["parameters"], &$input)) === false) { // By returning false, the filter can stop the processing // Then we check if we just "die" or do we do some rendering if ($filterImpl->_renderView) { MVC::renderView(!empty($filterImpl->view) ? $filterImpl->view : $controllerFunction, $filterImpl->template, $filterImpl->_contextVariables, $controllerScript, $controllerFunction); } // Exit exit; } } } } } /* * Populate the controller with the input, now that it's been through the filters */ $controller->input = $input; /* * Then continue to the controller */ call_user_func_array(array($controller, $controllerFunction), $methodParameters); /* * Render the view */ if ($controller->_renderView) { /* * Render the view */ MVC::renderView(empty($controller->view) ? $controllerFunction : $controller->view, $controller->template, $controller->_contextVariables, $controllerScript, $controllerFunction); } return $controller; }