]> git.andy128k.dev Git - ipf.git/commitdiff
separate project related template stuff
authorAndrey Kutejko <andy128k@gmail.com>
Tue, 30 Jul 2013 21:10:32 +0000 (00:10 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Tue, 30 Jul 2013 21:10:32 +0000 (00:10 +0300)
ipf/project_template.php [new file with mode: 0644]
ipf/shortcuts.php
ipf/template/environment/filesystem.php
ipf/template/tag/sql.php [deleted file]
ipf/template/tag/url.php [deleted file]

diff --git a/ipf/project_template.php b/ipf/project_template.php
new file mode 100644 (file)
index 0000000..f1602b7
--- /dev/null
@@ -0,0 +1,106 @@
+<?php
+
+final class IPF_Project_Template
+{
+    private static $defaultEnvironment = null;
+
+    public static function getDefaultTemplateEnvironment()
+    {
+        if (!self::$defaultEnvironment)
+            self::$defaultEnvironment = self::createEnvironment();
+        return self::$defaultEnvironment;
+    }
+
+    private static function createEnvironment()
+    {
+        $e = new IPF_Template_Environment_FileSystem;
+
+        $e->cache = IPF::get('tmp');
+        $e->debug = IPF::get('debug');
+
+        // folders
+        $projectTemplates = IPF::get('project_path') . '/templates';
+        if (is_dir($projectTemplates))
+            $e->folders[] = $projectTemplates;
+
+        foreach (IPF_Project::getInstance()->appList() as $app) {
+            $applicationTemplates = $app->getPath() . 'templates';
+            if (is_dir($applicationTemplates))
+                $e->folders[] = $applicationTemplates;
+        }
+
+        $e->tags['url'] = 'IPF_Project_Template_Tag_Url';
+        $e->tags['sql'] = 'IPF_Project_Template_Tag_Sql';
+        // extra tags
+        $e->tags = array_merge(IPF::get('template_tags', array()), $e->tags);
+
+        // extra modifiers
+        $e->modifiers = array_merge(IPF::get('template_modifiers', array()), $e->modifiers);
+
+        return $e;
+    }
+
+    public static function context($params=array(), $request=null)
+    {
+        if ($request) {
+            $params = array_merge(array('request' => $request), $params);
+            foreach (IPF::get('template_context_processors', array()) as $proc) {
+                IPF::loadFunction($proc);
+                $params = array_merge($proc($request), $params);
+            }
+            foreach (IPF_Project::getInstance()->appList() as $app) {
+                $params = array_merge($app->templateContext($request), $params);
+            }
+        }
+        return new IPF_Template_Context($params);
+    }
+}
+
+class IPF_Project_Template_Tag_Url extends IPF_Template_Tag
+{
+    function start()
+    {
+        $args = func_get_args();
+        $count = count($args);
+        if ($count === 0)
+            throw new IPF_Exception('No view specified');
+
+        $view = array_shift($args);
+
+        if ($count === 2 && is_array($args[0])) {
+            echo IPF_HTTP_URL::urlForView($view, $args[0]);
+        } elseif ($count === 3 && is_array($args[0]) && is_array($args[1])) {
+            echo IPF_HTTP_URL::urlForView($view, $args[0], $args[1]);
+        } else {
+            echo IPF_HTTP_URL::urlForView($view, $args);
+        }
+    }
+}
+
+class IPF_Project_Template_Tag_Sql extends IPF_Template_Tag
+{
+    function start()
+    {
+        $profiler = IPF_Project::getInstance()->sqlProfiler;
+        if ($profiler !== null) {
+            echo '<div style="padding:10px; margin:10px; background:#eee; border:1px dashed #888;"><h3>Sql Debug</h3><div style="color:#888">set <i>debug</i> to false in settings project for disable sql profiler</div>';
+            $time = 0;
+            foreach ($profiler->events as $event) {
+                $time += $event->getElapsedSecs();
+                $name = $event->getName();
+                if ($name=='fetch' || $name=='prepare' || $name=='connect')
+                    continue;
+                echo "<br>\n<b>" . $name . "</b> " . sprintf("%f", $event->getElapsedSecs()) . "<br>\n";
+                echo $event->getQuery() . "<br>\n";
+                $params = $event->getParams();
+                if( ! empty($params)) {
+                    var_dump($params);
+                    print "<br>\n";
+                }
+            }
+            echo "<br>\n<b>Total time:</b> " . $time  . " (without prepare and fetch event)<br>\n";
+            echo '</div>';
+        }
+    }
+}
+
index 7048f9278715b1bdda48026d12d723b045212323..62a25f4d3fc377da4adb794313f64564f1a467cc 100644 (file)
@@ -17,52 +17,11 @@ final class IPF_Shortcuts
 
     public static function RenderToString($tplfile, $params=array(), $request=null)
     {
-        if ($request) {
-            $params = array_merge(array('request' => $request), $params);
-            foreach (IPF::get('template_context_processors', array()) as $proc) {
-                IPF::loadFunction($proc);
-                $params = array_merge($proc($request), $params);
-            }
-            foreach (IPF_Project::getInstance()->appList() as $app) {
-                $params = array_merge($app->templateContext($request), $params);
-            }
-        }
-        $context = new IPF_Template_Context($params);
-
-        $tmpl = new IPF_Template_File($tplfile, self::getDefaultTemplateEnvironment());
+        $context = IPF_Project_Template::context($params, $request);
+        $tmpl = new IPF_Template_File($tplfile, IPF_Project_Template::getDefaultTemplateEnvironment());
         return $tmpl->render($context);
     }
 
-    private static $defaultEnvironment = null;
-
-    private static function getDefaultTemplateEnvironment()
-    {
-        if (!self::$defaultEnvironment) {
-            $dirs = array();
-
-            $projectTemplates = IPF::get('project_path') . '/templates';
-            if (is_dir($projectTemplates))
-                $dirs[] = $projectTemplates;
-
-            foreach (IPF_Project::getInstance()->appList() as $app) {
-                $applicationTemplates = $app->getPath() . 'templates';
-                if (is_dir($applicationTemplates))
-                    $dirs[] = $applicationTemplates;
-            }
-
-            self::$defaultEnvironment = new IPF_Template_Environment_FileSystem($dirs, IPF::get('tmp'));
-            self::$defaultEnvironment->debug = IPF::get('debug');
-
-            self::$defaultEnvironment->tags['url'] = 'IPF_Template_Tag_Url';
-            // extra tags
-            self::$defaultEnvironment->tags = array_merge(IPF::get('template_tags', array()), self::$defaultEnvironment->tags);
-
-            // extra modifiers
-            self::$defaultEnvironment->modifiers = array_merge(IPF::get('template_modifiers', array()), self::$defaultEnvironment->modifiers);
-        }
-        return self::$defaultEnvironment;
-    }
-
     public static function GetFormForModel($model, $data=null, $extra=array(), $label_suffix=null)
     {
         $extra['model'] = $model;
index 42c3aa5b8f0c36a4750d6484d01474d2397b056c..8eb8d6581c87b80a3f450f32f30be29877f08a64 100644 (file)
@@ -3,15 +3,9 @@
 class IPF_Template_Environment_FileSystem extends IPF_Template_Environment
 {
     public $folders = array();
-    public $cache = '';
+    public $cache = '/tmp';
     public $debug = false;
 
-    public function __construct($folders, $cache)
-    {
-        $this->folders = $folders;
-        $this->cache = $cache;
-    }
-
     public function loadTemplateFile($filename)
     {
         // FIXME: Very small security check, could be better.
diff --git a/ipf/template/tag/sql.php b/ipf/template/tag/sql.php
deleted file mode 100644 (file)
index 4039581..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-<?php
-
-class IPF_Template_Tag_Sql extends IPF_Template_Tag
-{
-    function start()
-    {
-        $profiler = IPF_Project::getInstance()->sqlProfiler;
-        if ($profiler !== null) {
-            echo '<div style="padding:10px; margin:10px; background:#eee; border:1px dashed #888;"><h3>Sql Debug</h3><div style="color:#888">set <i>debug</i> to false in settings project for disable sql profiler</div>';
-            $time = 0;
-            foreach ($profiler->events as $event) {
-                $time += $event->getElapsedSecs();
-                $name = $event->getName();
-                if ($name=='fetch' || $name=='prepare' || $name=='connect')
-                    continue;
-                echo "<br>\n<b>" . $name . "</b> " . sprintf("%f", $event->getElapsedSecs()) . "<br>\n";
-                echo $event->getQuery() . "<br>\n";
-                $params = $event->getParams();
-                if( ! empty($params)) {
-                    var_dump($params);
-                    print "<br>\n";
-                }
-            }
-            echo "<br>\n<b>Total time:</b> " . $time  . " (without prepare and fetch event)<br>\n";
-            echo '</div>';
-        }
-    }
-}
diff --git a/ipf/template/tag/url.php b/ipf/template/tag/url.php
deleted file mode 100644 (file)
index 7619f9d..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-<?php
-
-class IPF_Template_Tag_Url extends IPF_Template_Tag
-{
-    function start()
-    {
-        $args = func_get_args();
-        $count = count($args);
-        if ($count === 0)
-            throw new IPF_Exception('No view specified');
-
-        $view = array_shift($args);
-
-        if ($count === 2 && is_array($args[0])) {
-            echo IPF_HTTP_URL::urlForView($view, $args[0]);
-        } elseif ($count === 3 && is_array($args[0]) && is_array($args[1])) {
-            echo IPF_HTTP_URL::urlForView($view, $args[0], $args[1]);
-        } else {
-            echo IPF_HTTP_URL::urlForView($view, $args);
-        }
-    }
-}
-