Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/**
4
 * MySQL Resource
5
 * Resource Implementation based on the Custom API to use
6
 * MySQL as the storage resource for Smarty's templates and configs.
7
 * Table definition:
8
 * <pre>CREATE TABLE IF NOT EXISTS `templates` (
9
 *   `name` varchar(100) NOT NULL,
10
 *   `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
11
 *   `source` text,
12
 *   PRIMARY KEY (`name`)
13
 * ) ENGINE=InnoDB DEFAULT CHARSET=utf8;</pre>
14
 * Demo data:
15
 * <pre>INSERT INTO `templates` (`name`, `modified`, `source`) VALUES ('test.tpl', "2010-12-25 22:00:00", '{$x="hello
16
 * world"}{$x}');</pre>
17
 *
18
 *
19
 * @package Resource-examples
20
 * @author  Rodney Rehm
21
 */
22
class Smarty_Resource_Mysql extends Smarty_Resource_Custom
23
{
24
    /**
25
     * PDO instance
26
     *
27
     * @var \PDO
28
     */
29
    protected $db;
30
 
31
    /**
32
     * prepared fetch() statement
33
     *
34
     * @var \PDOStatement
35
     */
36
    protected $fetch;
37
 
38
    /**
39
     * prepared fetchTimestamp() statement
40
     *
41
     * @var \PDOStatement
42
     */
43
    protected $mtime;
44
 
45
    /**
46
     * Smarty_Resource_Mysql constructor.
47
     *
48
     * @throws \SmartyException
49
     */
50
    public function __construct()
51
    {
52
        try {
53
            $this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty");
54
        } catch (PDOException $e) {
55
            throw new SmartyException('Mysql Resource failed: ' . $e->getMessage());
56
        }
57
        $this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name');
58
        $this->mtime = $this->db->prepare('SELECT modified FROM templates WHERE name = :name');
59
    }
60
 
61
    /**
62
     * Fetch a template and its modification time from database
63
     *
64
     * @param string  $name   template name
65
     * @param string  $source template source
66
     * @param integer $mtime  template modification timestamp (epoch)
67
     *
68
     * @return void
69
     */
70
    protected function fetch($name, &$source, &$mtime)
71
    {
72
        $this->fetch->execute(array('name' => $name));
73
        $row = $this->fetch->fetch();
74
        $this->fetch->closeCursor();
75
        if ($row) {
76
            $source = $row[ 'source' ];
77
            $mtime = strtotime($row[ 'modified' ]);
78
        } else {
79
            $source = null;
80
            $mtime = null;
81
        }
82
    }
83
 
84
    /**
85
     * Fetch a template's modification time from database
86
     *
87
     * @note implementing this method is optional. Only implement it if modification times can be accessed faster than
88
     *       loading the comple template source.
89
     *
90
     * @param string $name template name
91
     *
92
     * @return integer timestamp (epoch) the template was modified
93
     */
94
    protected function fetchTimestamp($name)
95
    {
96
        $this->mtime->execute(array('name' => $name));
97
        $mtime = $this->mtime->fetchColumn();
98
        $this->mtime->closeCursor();
99
        return strtotime($mtime);
100
    }
101
}