cyrilleinvalides/choupas/www/admin/app/libs/sense/Engine/EngineSystem.php

87 lines
1.8 KiB
PHP
Executable File

<?php
class EngineSystem {
protected static $instances = array();
public static function get ( $name ) {
if ( !empty(static::$instances[$name]) )
return static::$instances[$name];
throw new Exception(sprintf(
'EngineSystem "%1$s" not registered !',
$name
));
}
public static function set ( $name, $engineClass, $root = '', $cache = '', $mkCache = false ) {
if ( $mkCache && !file_exists($cache) )
mkdir($cache, 0777, 1);
return static::$instances[$name] = new static($engineClass, $root, $cache);
}
protected
$root = '',
$cache = '',
$engineClass = '';
protected function __construct ( $engineClass, $root, $cache ) {
if ( !class_exists($engineClass) || !method_exists($engineClass, 'fromFile') )
throw new Exception(sprintf('Engine class %1$s does not exists or does not have a static "fromFile" method !', $engineClass));
if ( !file_exists($root) )
throw new Exception(sprintf('Root folder "%1$s" does not exists !', $root));
if ( $cache && !file_exists($cache) )
throw new Exception(sprintf('Cache folder "%1$s" does not exists !', $cache));
$this->root = realpath($root);
$this->cache = realpath($cache);
$this->engineClass = $engineClass;
}
public function load ( $file, $content = null ) {
$engineClass = $this->engineClass;
return $engineClass::fromFile($file, $this->root, $this->cache, $content);
}
public function exists ( $file ) {
$root = Path::get($this->root);
$file = $root->append($file);
return $file->shift($root) && file_exists($file) && is_file($file);
}
public function getClass () {
return $this->engineClass;
}
public function getRoot () {
return $this->root;
}
public function getCache () {
return $this->cache;
}
}
?>