public function __construct($compile_dir = NULL, $template_dir = NULL, $cache_dir = NULL, $plugins_dir = [], $config_dir = [], $disable_security = FALSE, Loops $loops = NULL) { parent::__construct($loops); $loops = $this->getLoops(); $application = $loops->getService("application"); $renderer = $loops->getService("renderer"); $app_dir = $application->app_dir; $cache_dir = $application->cache_dir; //adjust directories $compile_dir = Misc::fullPath($compile_dir ?: "renderer_cache/smarty", $cache_dir); $template_dir = array_map(function ($path) use($app_dir) { return Misc::fullPath($path, $app_dir); }, (array) ($template_dir ?: $renderer->view_dir)); $cache_dir = Misc::fullPath($compile_dir ?: "renderer_cache/smarty_cache", $cache_dir); $plugins_dir = array_map(function ($path) use($app_dir) { return Misc::fullPath($path, $app_dir); }, (array) $plugins_dir); $config_dir = array_map(function ($path) use($app_dir) { return Misc::fullPath($path, $app_dir); }, (array) $config_dir); //setup smarty $this->smarty = new Smarty(); $this->smarty->setCompileDir($compile_dir); $this->smarty->setTemplateDir($template_dir); $this->smarty->setCacheDir($cache_dir); $this->smarty->addPluginsDir($plugins_dir); $this->smarty->setConfigDir($config_dir); if ($disable_security) { $this->smarty->disableSecurity(); } //register extra modifier $this->smarty->registerPlugin("modifier", "render", [$loops->getService("renderer"), "render"]); $this->smarty->registerPlugin("modifier", "tr", [$this, "tr"]); }
public function __construct($filename = "loops.log", $mode = "a+", $level = "notice", Loops $loops = NULL) { parent::__construct($level, $loops); $application = $this->getLoops()->getService("application"); $filename = Misc::fullPath($filename, $application->cache_dir); $this->fo = new SplFileObject($filename, $mode); }
public function __construct($view_dir = [], Loops $loops = NULL) { parent::__construct($loops); $this->view_dir = array_map(function ($dir) { return Misc::fullPath($dir, $this->getLoops()->getService("application")->app_dir); }, (array) $view_dir); $this->view_dir = array_filter($this->view_dir, "is_dir"); $loops_views = realpath(__DIR__ . "/../../../views"); if (!in_array($loops_views, $this->view_dir)) { $this->view_dir[] = $loops_views; } }
/** * Setups a Loops application. * * This constructor will take a Loops context (via the $config parameter) or create a new one. * * The application will register itself as a service named 'application'. * Therefore, the application instance can be quickly accessed in a Loops\Object by $this->application. * * In Debug mode, changes in the application folder are attempted to be detected and if so, the complete cache (according to service 'cache') and renderer cache files (according to service 'renderer') will be deleted. * * The application will also speed up autoloading classes by remembering the filenames of class definitions in the cache module. * This behaviour can be disabled by setting the config value ->application->enable_cached_autoload to FALSE * * Include Dir: * The value ->application->include_dir is read from the config and defines paths that are consided for autoloading in the PSR-4 format. * This value can be an absolute path or relative to $app_dir. If not specified, [ 'inc' ] is used by default which specifies the 'inc' directory insied the application directory. * * Boot: * The value ->application->boot can hold a name of a php file that is executed after application creation. * This value can be an absolute path or relative to $app_dir. By default the file 'boot.php' inside the application directory is used. * Inside the boot script only the variable $loops is set. * * @param string $app_dir The application directory * @param string $cache_dir The directory for temporary files (as full paths or relative to $appdir) * @param string|Loops|Loops\ArrayObject $config A Loops context, a Loops\ArrayObject that is used to create a Loops context or the location of a php file that returns a Loops\ArrayObject or Loops context. * @param bool $boot Specifies if the boot script should be executed */ public function __construct($app_dir, $cache_dir = "/tmp", $config = "config.php", $boot = TRUE) { //setup directories $app_dir = realpath($app_dir); $cache_dir = Misc::fullPath($cache_dir, $app_dir); //make a context from config file if (is_string($config)) { $config = (include Misc::fullPath($config, $app_dir)); } if ($config instanceof ArrayObject) { $loops = new Loops($config, @$config->loops->debug === NULL ? TRUE : (bool) @$config->loops->debug); } elseif ($config instanceof Loops) { $loops = $config; } else { throw new Exception("Failed to create Loops Context."); } //register application service $loops->registerService("application", $this); $this->app_dir = $app_dir; $this->cache_dir = $cache_dir; $this->include_dir = (array) (@$loops->config->application->include_dir === NULL ? ["inc"] : $loops->config->application->include_dir); $this->boot = @$loops->config->application->boot ?: "boot.php"; $this->enable_cached_autoload = @$loops->config->application->enable_cached_autoload === NULL ? TRUE : $loops->config->application->enable_cached_autoload; $this->enable_cache_flush = @$loops->config->application->enable_cache_flush === NULL ? TRUE : $loops->config->application->enable_cache_flush; if (substr($this->boot, 0, 1) != "/") { $this->boot = "{$app_dir}/" . $this->boot; } foreach ($this->include_dir as $key => $include_dir) { if (substr($include_dir, 0, 1) != "/") { $this->include_dir[$key] = "{$app_dir}/{$include_dir}"; } } //register autoload foreach ($this->include_dir as $path) { spl_autoload_register(function ($classname) use($path) { $filename = $path . "/" . str_replace("\\", "/", $classname) . ".php"; if (!file_exists($filename)) { return; } require_once $filename; }); } if ($this->enable_cached_autoload) { $this->setupCachedAutoload($loops); } //delete cache if files have changed - do this check only in debugmode if ($this->enable_cache_flush && $loops->debug) { $cache = $loops->getService("cache"); $renderer = $loops->getService("renderer"); $dirs = []; if ($this->enable_cache_flush & 0x1) { $dirs[] = $this->app_dir; } if ($this->enable_cache_flush & 0x2) { $dirs = array_merge($dirs, $this->include_dir); } if ($this->enable_cache_flush & 0x4) { $dirs = array_merge($dirs, $renderer->view_dir); } if ($file = Misc::lastChange($dirs, $cache, $key)) { Misc::recursiveUnlink("{$this->cache_dir}/renderer_cache"); $cache->flushAll(); $cache->save($key, $file->getMTime()); } } parent::__construct($loops); //boot if requested if ($boot) { $this->boot(); } }
public function testFullPathsUpper() { $cwd = getcwd(); $parts = explode(DIRECTORY_SEPARATOR, $cwd); array_pop($parts); $path = implode(DIRECTORY_SEPARATOR, $parts); if ($cwd != "/") { $this->assertEquals($path . DIRECTORY_SEPARATOR . 'test', Misc::fullPath('../test', NULL, TRUE)); } $this->assertEquals('/test' . DIRECTORY_SEPARATOR . 'test', Misc::fullPath('../test', '/test' . DIRECTORY_SEPARATOR . 'sub', TRUE)); $this->assertEquals('/test' . DIRECTORY_SEPARATOR . 'test', Misc::fullPath('../test', '/test' . DIRECTORY_SEPARATOR . 'sub' . DIRECTORY_SEPARATOR, TRUE)); $this->assertEquals('C:\\test' . DIRECTORY_SEPARATOR . 'test', Misc::fullPath('../test', 'C:\\test' . DIRECTORY_SEPARATOR . 'sub', TRUE)); $this->assertEquals('C:\\test' . DIRECTORY_SEPARATOR . 'test', Misc::fullPath('../test', 'C:\\test' . DIRECTORY_SEPARATOR . 'sub' . DIRECTORY_SEPARATOR, TRUE)); }