public function render() { // Merge with Global Data Static array $this->data = array_merge(self::$global_data, $this->data); // Inject "fw" object $this->data["fw"] = new \Fw\Twig\Extend(); // Determine Template Path if (!($template_path = \Fw\Find::in("views", $this->template_file))) { throw new \Exception("Template File (" . $this->template_file . ") not found. Looked in " . \Fw\Debug::get()); } $template_path_info = pathinfo($template_path); // Init Twig Loader. Search paths are the Fw00t view paths. $loader = new \Twig_Loader_Filesystem(array_merge(array($template_path_info["dirname"]), \Fw\Find::get_paths("views"))); // Init Twig Renderer Environment if (\Fw\Config::get("env") == \Fw\Config::PRODUCTION) { $twig = new \Twig_Environment($loader, array('cache' => $this->cache_dir)); } else { $twig = new \Twig_Environment($loader, array()); } foreach (self::$filters as $filter) { $twig->addFilter($filter); } // Commence Twigging $template = $twig->loadTemplate($template_path_info["basename"]); return $template->render($this->data); }
/** * Searches for asset based on the asset resources defined in \Fw\Find * * @param $file * @return string * @throws \Exception */ static function find($file, $prefix_cdn_config = "cdn") { if (!($real_path = \Fw\Find::in("assets", $file))) { throw new \Exception("{$file} not found"); } $so = strlen(\Fw\Config::get("docroot")) - 1; return \Fw\Config::get($prefix_cdn_config) . substr($real_path, $so); }
static function init() { define("DS", DIRECTORY_SEPARATOR); // Constants define('DOCROOT', dirname($_SERVER["SCRIPT_FILENAME"]) . DS); // Derive Some Config variables from URL $_base = function () { $_b = explode(DS, $_SERVER["SCRIPT_NAME"]); return substr($_SERVER["SCRIPT_NAME"], 0, strlen($_b[sizeof(explode(DS, $_SERVER["SCRIPT_NAME"])) - 1]) * -1); }; \Fw\Config::set("base", $_base()); $_SERVER["HTTPS"] = isset($_SERVER["HTTPS"]) ? $_SERVER["HTTPS"] : "off"; if (isset($_SERVER["SERVER_NAME"])) { \Fw\Config::set("domain", @$_SERVER["HTTPS"] == "on" ? "https://" . $_SERVER["SERVER_NAME"] : "http://" . $_SERVER["SERVER_NAME"]); } else { \Fw\Config::set("domain", ""); } // Set Application Directory to server path if (!Config::exists("apppath")) { $app_path = DOCROOT; } else { $app_path = Config::get("apppath"); } // Autoload Application Class \Fw\Autoload::add_path($app_path . "classes"); // PHP Composer Autoloader if (file_exists($app_path . "/vendor/autoload.php")) { include $app_path . "/vendor/autoload.php"; } // Configuration \Fw\Config::set("docroot", DOCROOT); // doc root \Fw\Config::set("apppath", $app_path); // application path // Finder Paths Find::add_path_to("controllers", \Fw\Config::get("apppath") . "controllers"); Find::add_path_to("views", Config::get("apppath") . "views"); Find::add_path_to("assets", Config::get("apppath") . "assets"); Find::add_path_to("assets", Config::get("apppath") . "bower_components"); Find::add_path_to("modules", Config::get("apppath") . "modules"); }
function find($in, $file) { $real_path = \Fw\Find::in($in, $file); $so = strlen(\Fw\Config::get("docroot")) - 1; return substr($real_path, $so); }
function paths() { \Fw\Find::add_path_to("views", \Fw\Config::get("apppath") . "modules/example/views"); }
static function map($pattern, $target, $name = '', $method = 'GET|POST') { self::check_alto(); $route_pattern = str_replace("//", "/", \Fw\Config::get("base") . $pattern); self::$alto->map($method, $route_pattern, $target, $name); }
include "vendor/autoload.php"; // Start Fw \Fw\Framework::init(); // Conveniences use Fw\Config; use Tracy\Debugger; // Set Environment (convenience) Config::set("env", Config::get("env", "application.php")); // Switch on Debug Mode if (Config::get("env", "application.php") == Config::DEVELOPMENT) { Debugger::enable(Debugger::DEVELOPMENT); Debugger::$strictMode = true; } // Config date_default_timezone_set(Config::get("default_timezone", "application.php")); Config::set("base", Config::get("base", "application.php")); Config::set("cdn", Config::get("cdn", "application.php")); // Modules foreach (Config::get("modules", "application.php") as $module_name) { \Fw\Module::load($module_name); } // Runs all begin functions in all declared modules \Fw\Module::run_begin(); // Runs all routing functions in all declared modules \Fw\Module::run_routes(); // Runs all paths functions in all declared modules \Fw\Module::run_paths(); // Run Application Bootstrap include "app.php"; // Runs all end functions in all declared modules \Fw\Module::run_end();
<?php /* * Application Boilerplate */ \Fw\Twig::global_data("base", \Fw\Config::get("base")); \Fw\Twig::global_data("assets", \Fw\Config::get("base") . "assets/build"); \Fw\Twig::global_data("server_ip", $_SERVER["SERVER_ADDR"]); \Fw\Twig::global_data("is_dev", \Fw\Config::get("env", "application.php") == \Fw\Config::DEVELOPMENT ? true : false); \Fw\Fwouter::init(array("base" => \Fw\Config::get("base"))); if (\Fw\Fwouter::$error) { _404(\Fw\Fwouter::$error); } // 404 function _404($reason = '') { \Fw\View::twig("404.twig", array("reason" => $reason)); }