getDependencies();
return static::$instance;
}
/**
* Recupere le contexte d'execution
*
* @return void
*/
protected function getDependencies()
{
if(true === $this->is_initialized) {
return;
}
global $base_path;
$this->styles_path = trim($base_path.'/styles');
$this->is_initialized = true;
}
/**
* Lit les fichiers d'un repertoire en fonction de leur extension et les trie par ordre alpha
*
* @param string $dir : repertoire a lire avec / final
* @param string $extension : extension du fichier
*
* @return array|string[] : tableau de fichiers
*/
protected function readAndSortFiles(string $dir, string $extension)
{
$handle = @opendir($dir);
$files = [];
if(!$handle) {
return [];
}
$l = strlen($extension);
if($handle) {
while( false !== ($item = readdir($handle)) ) {
if( !in_array($dir.$item, $this->common_disabled_files) && is_file($dir.$item) && substr($item, -$l) == $extension ) {
$files[] = $dir.$item;
}
}
closedir($handle);
}
sort($files);
return $files;
}
/**
* Retourne les feuilles de style et fichiers js associes a inclure
*
* @param string $style
*
* @return string
*/
public function getStyle(string $style)
{
if( !is_null($this->css_content) ) {
return $this->css_content;
}
$dir = __DIR__.'/../styles';
if($this->styles_path) {
$dir = $this->styles_path;
}
$this->css_content = "";
// Lecture fichier disable dans le repertoire common
if(is_file($dir.'/common/disable') && is_readable($dir.'/common/disable')) {
$s = file_get_contents($dir.'/common/disable');
$t = explode(PHP_EOL, $s);
if(is_array($t)) {
foreach($t as $v) {
if($v) {
$this->common_disabled_files[] = $dir.'/common/'.trim($v);
}
}
}
}
// inclusion des feuilles de style communes
$css_files = static::readAndSortFiles($dir.'/common/', '.css');
foreach($css_files as $css_file){
$time = @filemtime($css_file);
$this->css_content.= PHP_EOL."";
}
// inclusion des fichiers js communs
$js_files = static::readAndSortFiles($dir.'/common/javascript', '.js');
foreach($js_files as $js_file) {
$time = @filemtime($js_file);
$this->css_content.= PHP_EOL."";
}
// inclusion des feuilles de style issues du style demande
$css_files = static::readAndSortFiles($dir.'/'.$style.'/', '.css');
foreach($css_files as $css_file){
$time = @filemtime($css_file);
$this->css_content.= PHP_EOL."";
}
// inclusion des fichiers js issus du style demande
$js_files = static::readAndSortFiles($dir.'/'.$style.'/javascript/', '.js');
foreach($js_files as $js_file) {
$time = @filemtime($js_file);
$this->css_content.= PHP_EOL."";
}
//Ajout du style dans une globale js
$this->css_content.= "";
return $this->css_content;
}
}