Base application
[infodrom.org/touren.infodrom.org] / core / styles.class.php
diff --git a/core/styles.class.php b/core/styles.class.php
new file mode 100644 (file)
index 0000000..44ced38
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+
+class Styles extends Singleton {
+    protected static $instance = false;
+    protected $code = array();
+    protected $files = array();
+
+    public function add($code)
+    {
+       $this->code[] = $code;
+    }
+
+    public function file($path)
+    {
+       $app = Application::get();
+
+       $info = pathinfo($path);
+
+       $minfile = sprintf('%s%s/%s.min.%s', $app->getBaseDir(), $info['dirname'], $info['filename'], $info['extension']);
+       if (file_exists($minfile)) {
+           $origpath = $app->getBaseDir().$path;
+           if (filemtime($minfile) > filemtime($origpath))
+               $path = sprintf('%s/%s.min.%s', $info['dirname'], $info['filename'], $info['extension']);
+       }
+
+       $this->files[$path] = true;
+    }
+
+    public function toString()
+    {
+       $app = Application::get();
+
+       $ret = '';
+       foreach (array_keys($this->files) as $file)
+           $ret .= sprintf('<link href="%s%s" rel="stylesheet" type="text/css">'."\n", $app->getBaseURL(), $file);
+
+       if (count($this->code)) {
+           $ret .= '<style type="text/css">'."\n";
+           $ret .= implode("\n", $this->code);
+           $ret .= "</style>\n";
+       }
+
+       return $ret;
+    }
+
+}