http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 5 LOC: 116 Statements: 22

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


Report generated at 2008-01-20T12:13:39-05:00