Remove trailing <? in template expansion.
[hoplite.git] / views / pdo_cache_backend.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2013 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\views;
18
19 require_once HOPLITE_ROOT . '/views/cache_backend.php';
20
21 /*!
22 An instance of CacheBackend that stores compiled templates in a database
23 table accessed through a PDO object.
24 */
25 class PDOCacheBackend implements CacheBackend
26 {
27 /*! @var \PDO The dat abase to use. */
28 protected $db = NULL;
29
30 /*! @var \PDOStatement Used to query for cached templates. */
31 protected $fetch_statement = NULL;
32
33 /*! @var \PDOStatement Used to insert new templates. */
34 protected $insert_statement = NULL;
35
36 /*! @var \PDOStatement Used to expire out-of-date templates. */
37 protected $delete_statement = NULL;
38
39 /*! @var string Base query for a multi-fetch operation. */
40 protected $fetch_multi_query = "";
41
42 /*!
43 Constructor that prepares the database statements.
44
45 @param \PDO The connected database object.
46 @param string Name of the database table to store templates in.
47 @param string Column in which the template name is stored.
48 @param string Column in which the template data is stored.
49 @param string Column in which the template last modified time is stored.
50 */
51 public function __construct(\PDO $db,
52 $table_name,
53 $name_column_name = 'name',
54 $data_column_name = 'template',
55 $timestamp_column_name = 'timestamp')
56 {
57 $this->db = $db;
58
59 $this->fetch_statement = $db->Prepare("
60 SELECT `$data_column_name` AS template,
61 `$timestamp_column_name` AS timestamp
62 FROM $table_name
63 WHERE `$name_column_name` IN (:name)
64 ");
65
66 $this->insert_statement = $db->Prepare("
67 INSERT INTO `$table_name`
68 (`$name_column_name`, `$data_column_name`, `$timestamp_column_name`)
69 VALUES
70 (:name, :template, :timestamp)
71 ");
72
73 $this->delete_statement = $db->Prepare("
74 DELETE FROM `$table_name`
75 WHERE `$name_column_name` = :name
76 ");
77
78 $this->fetch_multi_query = "
79 SELECT `$name_column_name` AS name,
80 `$data_column_name` AS template,
81 `$timestamp_column_name` AS timestamp
82 FROM $table_name
83 WHERE `$name_column_name` IN ";
84 }
85
86 public function GetTemplateDataForName($name, $modification_time)
87 {
88 $this->fetch_statement->Execute(array('name' => $name));
89 $tpl = $this->fetch_statement->FetchObject();
90 if (!$tpl)
91 return NULL;
92
93 if ($tpl->timestamp < $modification_time) {
94 $this->delete_statement->Execute(array('name' => $name));
95 return NULL;
96 }
97
98 return $tpl->template;
99 }
100
101 public function StoreCompiledTemplate($name, $modification_time, $data)
102 {
103 $this->insert_statement->Execute(array(
104 'name' => $name,
105 'timestamp' => $modification_time,
106 'template' => $data,
107 ));
108 }
109
110 public function GetMultipleTemplates(Array $fetch_templates)
111 {
112 $placeholders = array_fill(0, count($fetch_templates), '?');
113 $stmt = $this->db->Prepare($this->fetch_multi_query . '(' . implode(',', $placeholders) . ')');
114 $stmt->Execute(array_keys($fetch_templates));
115
116 $templates = array();
117 while (($template = $stmt->FetchObject())) {
118 if ($template->timestamp >= $fetch_templates[$template->name])
119 $templates[$template->name] = $template->template;
120 }
121 return $templates;
122 }
123 }