public static function generateBasicController($controller, $action, $code) { $controllerHash = md5($controller . "/" . $action); if (!isset(ControllerGenerator::$generatedControllers[$controllerHash])) { ControllerGenerator::$generatedControllers[$controllerHash] = 0; } ControllerGenerator::$generatedControllers[$controllerHash]++; $controllerFile = WEBAPP_ROOT . "/tmp/controllers/Temp{$controllerHash}_" . ControllerGenerator::$generatedControllers[$controllerHash] . ".php"; $skeleton = file_get_contents(dirname(__FILE__) . "/controller-skeletons/basic-skeleton.phps"); // Fill in the blanks $data = array("{name}" => "Temp{$controllerHash}_" . ControllerGenerator::$generatedControllers[$controllerHash], "{method}" => "func", "{code}" => ViewCompiler::cleanFileAfterCompilation($code)); $controllerCode = str_replace(array_keys($data), array_values($data), $skeleton); file_put_contents($controllerFile, $controllerCode); ControllerGenerator::addRoute("/{$controllerHash}_" . ControllerGenerator::$generatedControllers[$controllerHash] . "/func", $controllerFile, "Temp{$controllerHash}_" . ControllerGenerator::$generatedControllers[$controllerHash] . "Controller"); if (property_exists("AppConfiguration", "APP_PATH")) { $path_prefix = AppConfiguration::$APP_PATH; } else { $path_prefix = ""; } return "{$path_prefix}/{$controllerHash}_" . ControllerGenerator::$generatedControllers[$controllerHash] . "/func"; }
public static function compileView($path, $file, $controller, $function) { /* Set an error handler to trap compilation errors */ set_error_handler("ViewCompiler::errorHandler", E_WARNING); /* Wrap the file in <mvc:components> to make sure it's XML */ $file = "<mvc:components xmlns:mvc=\"http://simplemvc.org/ns/2010\">{$file}\n</mvc:components>"; /* * We need to do some modifications to the file so it's processed properly, for example * converting HTML -comments to something else (<!-- -->) as otherwise CSS or JavaScript * might be left out. We convert them back after the file has been compiled */ $file = ViewCompiler::prepareFileForCompilation($file); $dom = new DOMDocument(); $dom->preserveWhiteSpace = true; $dom->loadXML($file); // Traverse the tree $compiler = new ViewCompiler($path, $controller, $function); ob_start(); $compiler->traverse($dom->documentElement); $compiledView = ob_get_contents(); ob_end_clean(); return ViewCompiler::cleanFileAfterCompilation($compiledView); }