http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 5 LOC: 121 Statements: 22

Source file Statements Methods Total coverage
Cache.php 90.9% 100.0% 92.6%
   
1
<?php
2
/**
3
 * Xyster Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://www.opensource.org/licenses/bsd-license.php
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to xyster@devweblog.org so we can send you a copy immediately.
14
 *
15
 * @category  Xyster
16
 * @package   Xyster_Controller
17
 * @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org)
18
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
19
 * @version   $Id: Cache.php 85 2007-09-13 00:07:24Z doublecompile $
20
 */
21
/**
22
 * Zend_Controller_Action_Helper_Abstract
23
 */
24 1
require_once 'Zend/Controller/Action/Helper/Abstract.php';
25
/**
26
 * Cache control action helper
27
 *
28
 * @category  Xyster
29
 * @package   Xyster_Controller
30
 * @subpackage Helpers
31
 * @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org)
32
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
33
 */
34
class Xyster_Controller_Action_Helper_Cache extends Zend_Controller_Action_Helper_Abstract
35
{
36
    /**
37
     * The cache-control header to send
38
     *
39
     * This header has been tested to work successfully over SSL
40
     */
41
    const CACHE_CONTROL_HEADER = 'Cache-Control: private, must-revalidate, max-age=0, pre-check=0';
42
43
    /**
44
     * Checks the user's last mod date against the date supplied
45
     *
46
     * @param int $date
47
     * @return boolean
48
     */
49
    public function direct( $date )
50
    {
51 4
        return $this->checkModifiedSince($date);
52
    }
53
54
    /**
55
     * Checks the user's last mod date against the date supplied
56
     *
57
     * All appropriate headers are sent.  In the event the page hasn't been
58
     * modified, the view renderer is disabled through the front controller.
59
     *
60
     * @param int $date
61
     * @return boolean
62
     */
63
    public function checkModifiedSince( $date )
64
    {
65 4
        if ( $this->_wasModifiedSince($date) ) {
66 2
            return true;
67 0
        }
68
69 2
        $this->_lastModified($date);
70 2
        return false;
71
    }
72
73
    /**
74
     * Formats a date in GMT syntax suitable for HTTP headers
75
     *
76
     * @param int $date
77
     * @return string
78
     */
79
    protected function _getGmDate( $date )
80
    {
81 4
        return gmdate('D, d M Y H:i:s', $date) . ' GMT';
82
    }
83
84
    /**
85
     * Sends cache-control, last-modified, and etag headers
86
     *
87
     * @param int $date
88
     */
89
    protected function _lastModified($date)
90
    {
91 2
        $gmdate = $this->_getGmDate($date);
92
93 2
        $this->getResponse()->setRawHeader(self::CACHE_CONTROL_HEADER)
94 2
            ->setRawHeader('Last-Modified: ' . $gmdate)
95 2
            ->setHeader('Etag', md5($gmdate));
96
    }
97
98
    /**
99
     * Checks the date supplied against the if-modified-since header
100
     *
101
     * @param int $date
102
     * @return boolean
103
     */
104
    protected function _wasModifiedSince( $date )
105
    {
106 4
        $since = $this->getRequest()->getServer('HTTP_IF_MODIFIED_SINCE');
107
108 4
        if ( $since && $this->_getGmDate($date) == preg_replace('/;.*$/', '', $since) ) {
109
            // send the not modified headers
110 2
            $this->getResponse()->setHttpResponseCode(304)
111 2
                ->setRawHeader('Status: 304 Not Modified')
112 2
                ->setHeader('Content-Length', 0, true);
113
            // make sure the view renderer doesn't fire
114 2
            require_once 'Zend/Controller/Front.php';
115 2
            Zend_Controller_Front::getInstance()->setParam('noViewRenderer', true);
116 2
            return true;
117 0
        }
118
119 2
        return false;
120
    }
121
}


Report generated at 2007-11-05T09:09:01-05:00