/** * Show a snippet */ function smarty_function_snippet($params, &$smarty) { /** * Supported parameters: * - source (the source file in WEB_PATH . /snippet/ * - * (will be converted to <snippet_name>_* */ $template = new \Skeleton\Template\Template(); if (empty($params['source'])) { throw new Exception('source required for snippet'); } else { $source = $params['source']; $var_identifier = substr($source, 0, -4); } foreach ($smarty->getTemplateVars() as $key => $value) { $template->assign($key, $value); } foreach ($params as $key => $value) { $template->assign($var_identifier . '_' . $key, $value); } foreach ($params as $key => $value) { $template->assign($key, $value); } $template->set_template_directory(\Skeleton\Core\Application::Get()->template_path); $content = $template->render('../snippet/' . $source); return $content; }
/** * get module_path * * @access public * @return string $path */ public function get_module_path() { $reflection = new \ReflectionClass($this); $application = Application::Get(); $path = '/' . str_replace($application->module_path, '', $reflection->getFileName()); $path = str_replace('.php', '', $path); return $path; }
/** * Get function, returns a Config object * * @return Config * @access public */ public static function Get() { if (!isset(self::$config)) { try { self::$config = \Skeleton\Core\Application::Get()->config; } catch (Exception $e) { return new Config(); } } return self::$config; }
/** * Constructor */ public function __construct() { $this->template = new \Skeleton\Template\Template(); $application = \Skeleton\Core\Application::Get(); if (file_exists($application->template_path)) { $this->template->add_template_directory($application->template_path, $application->name); } $packages = \Skeleton\Core\Package::get_all(); foreach ($packages as $package) { if (file_exists($package->template_path)) { $this->template->add_template_directory($package->template_path, $package->name); } } }
/** * Do a reverse rewrite of a link * * @access private * @param string $url_raw * @return string $reverse_rewrite */ private static function rewrite_reverse_routes($url_raw) { $url = parse_url($url_raw); $params = []; $application = \Skeleton\Core\Application::Get(); $routes = $application->config->routes; if (isset($url['query'])) { // Allow & instead of & $url['query'] = str_replace('&', '&', $url['query']); parse_str($url['query'], $params); } /** * Add language to the known parameters */ if (isset($application->language) and !isset($params['language'])) { $params['language'] = $application->language->name_short; } /** * Search for the requested module */ if (!isset($url['path'])) { return $url_raw; } if ($url['path'] != '' and $url['path'][0] == '/') { $url['path'] = substr($url['path'], 1); } $module_name = null; /** * Check skeleton packages */ $packages = \Skeleton\Core\Package::get_all(); foreach ($packages as $package) { $parts = explode('/', $url['path']); if (isset($parts[0]) and $parts[0] == $package->name) { unset($parts[0]); $package_parts = explode('-', str_replace('skeleton-package-', '', $package->name)); foreach ($package_parts as $key => $package_part) { $package_parts[$key] = ucfirst($package_part); } $module_name = '\\Skeleton\\Package\\' . implode('\\', $package_parts) . '\\Web\\Module\\' . str_replace('_', '\\', implode('/', $parts)); } } if ($module_name === null) { $module_name = 'web_module_' . str_replace('/', '_', $url['path']); } $module_defined = false; $package_module = false; if (isset($routes[$module_name])) { $module_defined = true; } elseif (isset($routes[$module_name . '_index'])) { $module_name = $module_name . '_index'; $module_defined = true; } else { foreach ($routes as $classname => $dummy) { $application = Application::get(); $module_filename = str_replace('web_module_', '', $classname); $filename_parts = explode('_', $module_filename); $module_filename = ''; foreach ($filename_parts as $filename_part) { $module_filename .= '/' . strtolower($filename_part); } $module_filename .= '.php'; $module_filename = $application->module_path . $module_filename; if (file_exists($module_filename) and !class_exists($classname)) { require_once $module_filename; } if (class_exists($classname) and (strtolower(get_parent_class($classname)) == strtolower($module_name) or is_subclass_of($classname, $module_name))) { $module_name = strtolower($classname); $module_defined = true; $package_module = true; } } } if (!$module_defined) { return $url_raw; } $routes = $routes[$module_name]; $correct_route = null; foreach ($routes as $route) { $route_parts = explode('/', $route); $route_part_matches = 0; foreach ($route_parts as $key => $route_part) { if (trim($route_part) == '') { unset($route_parts[$key]); continue; } if ($route_part[0] != '$') { $route_part_matches++; continue; } /** * $language[en,nl] => language[en,nl] */ $route_part = substr($route_part, 1); /** * Fetch required values */ $required_values = []; preg_match_all('/(\\[(.*?)\\])/', $route_part, $matches); if (count($matches[2]) > 0) { /** * There are required values, parse them */ $required_values = explode(',', $matches[2][0]); $route_part = str_replace($matches[0][0], '', $route_part); $route_parts[$key] = '$' . $route_part; } if (isset($params[$route_part])) { /** * if there are no required values => Proceed */ if (count($required_values) == 0) { $route_part_matches++; continue; } /** * Check the required values */ $values_ok = false; foreach ($required_values as $required_value) { if ($required_value == $params[$route_part]) { $values_ok = true; } } if ($values_ok) { $route_part_matches++; continue; } } } if ($route_part_matches == count($route_parts)) { $correct_route = $route_parts; } } if ($correct_route === null and !$package_module) { return $url_raw; } elseif ($correct_route === null and $package_module) { $module_name = str_replace('web_module_', '', $module_name); $new_url = '/' . str_replace('_', '/', $module_name); } else { $new_url = ''; foreach ($correct_route as $url_part) { if ($url_part[0] !== '$') { $new_url .= '/' . $url_part; continue; } $url_part = substr($url_part, 1); $new_url .= '/' . $params[$url_part]; unset($params[$url_part]); } } /** * If the first character is a /, remove it */ if ($new_url[0] == '/') { $new_url = substr($new_url, 1); } if (count($params) > 0) { $new_url .= '?' . urldecode(http_build_query($params)); } /** * Is there a fragment ('#') available? */ if (isset($url['fragment'])) { $new_url .= '#' . $url['fragment']; } return $new_url; }
/** * Render * * @access public * @param string $template * @return string $html */ public function render($template) { if (strpos($template, '.') === false) { throw new \Exception('Please provide a valid template filename. Incorrect filename "' . $template . '"'); } list($filename, $extension) = explode('.', basename($template)); switch ($extension) { case 'twig': $renderer = new \Skeleton\Template\Twig\Twig(); break; case 'tpl': $renderer = new \Skeleton\Template\Smarty\Smarty(); break; default: throw new \Exception('Unknown template type'); } if (count($this->template_directories) == 0) { throw new \Exception('No template directory set, please set $template->set_template_directory()'); } // Set the template path foreach ($this->template_directories as $template_directory) { $renderer->add_template_directory($template_directory['directory'], $template_directory['namespace']); } // Pass the environment variables to the template renderer if (count($this->environment) > 0) { foreach ($this->environment as $key => $value) { $renderer->add_environment($key, $value); } } // Pass the variables to the template renderer foreach ($this->variables as $key => $value) { $renderer->assign($key, $value); } // Set the translation object if ($this->translation !== null) { $renderer->set_translation($this->translation); } else { if (class_exists('\\Skeleton\\I18n\\Translation') and class_exists('Skeleton\\Core\\Application')) { try { $language = \Skeleton\Core\Application::Get()->language; $application_name = \Skeleton\Core\Application::Get()->name; $translation = \Skeleton\I18n\Translation::Get($language, $application_name); $renderer->set_translation($translation); } catch (\Exception $e) { } } } return $renderer->render($template); }