http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 6 LOC: 123 Statements: 23
Legend: executednot executeddead code
Source file Statements Methods Total coverage
Cache.php 100.0% 100.0% 100.0%
 
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 LibreWorks, LLC (http://libreworks.net)
13
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
14
 * @version   $Id: Cache.php 418 2010-10-18 21:40:08Z jonathanhawk $
15
 */
16
namespace Xyster\Controller\Action\Helper;
17
/**
18
 * Cache control action helper
19
 *
20
 * @category  Xyster
21
 * @package   Xyster_Controller
22
 * @subpackage Helpers
23
 * @copyright Copyright LibreWorks, LLC (http://libreworks.net)
24
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
25
 */
26
class Cache extends \Zend_Controller_Action_Helper_Abstract
27 1
{
28
    /**
29
     * The cache-control header to send
30
     *
31
     * This header has been tested to work successfully over SSL
32
     */
33
    const CACHE_CONTROL_HEADER = 'Cache-Control: private, must-revalidate, max-age=0, pre-check=0';
34
35
    /**
36
     * Checks the user's last mod date against the date supplied
37
     *
38
     * @param int $date
39
     * @return boolean
40
     */
41
    public function direct( $date )
42
    {
43 4
        return $this->checkModifiedSince($date);
44
    }
45
46
    /**
47
     * Checks the user's last mod date against the date supplied
48
     *
49
     * All appropriate headers are sent.  In the event the page hasn't been
50
     * modified, the view renderer is disabled through the front controller.
51
     *
52
     * @param int $date
53
     * @return boolean
54
     */
55
    public function checkModifiedSince( $date )
56
    {
57 4
        if ( $this->_wasModifiedSince($date) ) {
58 2
            return true;
59
        }
60
61 2
        $this->_lastModified($date);
62 2
        return false;
63
    }
64
65
    /**
66
     * Formats a date in GMT syntax suitable for HTTP headers
67
     *
68
     * @param int $date
69
     * @return string
70
     */
71
    protected function _getGmDate( $date )
72
    {
73 4
        return gmdate('D, d M Y H:i:s', $date) . ' GMT';
74
    }
75
76
    /**
77
     * Sends cache-control, last-modified, and etag headers
78
     *
79
     * @param int $date
80
     */
81
    protected function _lastModified($date)
82
    {
83 2
        $gmdate = $this->_getGmDate($date);
84
85 2
        $this->getResponse()->setRawHeader(self::CACHE_CONTROL_HEADER)
86 2
            ->setRawHeader('Last-Modified: ' . $gmdate)
87 2
            ->setHeader('Etag', md5($gmdate));
88
    }
89
90
    /**
91
     * Checks the date supplied against the if-modified-since header
92
     *
93
     * @param int $date
94
     * @return boolean
95
     */
96
    protected function _wasModifiedSince( $date )
97
    {
98 4
        $since = $this->getRequest()->getServer('HTTP_IF_MODIFIED_SINCE');
99
100 4
        if ( $since && $this->_getGmDate($date) == preg_replace('/;.*$/', '', $since) ) {
101
            // send the not modified headers
102 2
            $this->getResponse()->setHttpResponseCode(304)
103 2
                ->setRawHeader('Status: 304 Not Modified')
104 2
                ->setHeader('Content-Length', 0, true);
105
            // make sure the view renderer doesn't fire
106 2
            \Zend_Controller_Front::getInstance()->setParam('noViewRenderer', true);
107 2
            return true;
108
        }
109
110 2
        return false;
111
    }
112
113
    /**
114
     * Gets the name of this helper.
115
     *
116
     * @todo Remove when ZF2 is out
117
     * @return string The name of the helper
118
     */
119
    public function getName()
120
    {
121 2
        return "Cache";
122
    }
123 1
}


Report generated at 2010-10-18T17:19:48-04:00