<commit_before><?php
/**
 * @link      https://github.com/weierophinney/PhlySimplePage for the canonical source repository
 * @copyright Copyright (c) 2012 <NAME> (http://mwop.net)
 * @license   https://github.com/weierophinney/PhlySimplePage/blog/master/LICENSE.md New BSD License
 */

namespace PhlySimplePage;

use Zend\Cache\Storage\Adapter\AbstractAdapter;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;

/**
 * Event listener implementing page level caching for pages provided by the 
 * PageController.
 */
class PageCacheListener implements ListenerAggregateInterface
{
    /**
     * @var AbstractAdapter
     */
    protected $cache;

    /**
     * @var \Zend\Stdlib\CallbackHandler[]
     */
    protected $listeners = array();

    /**
     * Whether or not to cache this request
     * 
     * @var bool
     */
    protected $cacheThisRequest = false;

    /**
     * Key to use when caching
     * 
     * @var string
     */
    protected $cacheKey;

    /**
     * Constructor
     * 
     * @param AbstractAdapter $cache 
     */
    public function __construct(AbstractAdapter $cache)
    {
        $this->cache = $cache;
    }

    /**
     * Attach listeners to the application event system.
     *
     * Registers two event listeners:
     *
     * - route, at priority -99, 
     * - finish, at priority -10001, 
     * 
     * @param EventManagerInterface $events 
     */
    public function attach(EventManagerInterface $events)
    {
        $events->attach('route', array($this, 'onRoutePost'), -99);
        $events->attach('finish', array($this, 'onFinishPost'), -10001);
    }

    /**
     * Detach any registered listeners from the given event manager instance.
     * 
     * @param EventManagerInterface $events 
     */
    public function detach(EventManagerInterface $events)
    {
        foreach ($this->listeners as $index => $listener) {
            if ($events->detach($listener)) {
                unset($this->listeners[$index]);
            }
        }
    }

    /**
     * "route" event listener
     *
     * Checks to see if (a) we have a controller we're interested in, (b) if
     * a "template" was provided in the route matches, and (c) if we have a 
     * cache hit for that template name. If we do, we return a populated 
     * response; if not, we continue, but indicate that we should cache the
     * response on completion.
     * 
     * @param  \Zend\Mvc\MvcEvent $e 
     * @return null|\Zend\Stdlib\ResponseInterface
     */
    public function onRoutePost($e)
    {
        $matches = $e->getRouteMatch();
        if (!$matches) {
            return;
        }

        $controller = $matches->getParam('controller');
        if ($controller != 'PhlySimplePage\Controller\Page') {
            return;
        }

        $template = $matches->getParam('template', false);
        if (!$template) {
            return;
        }

        $cacheKey = $this->normalizeCacheKey($template);
        
        $result = $cache->getItem($cacheKey, $success);
        if (!$success) {
            // Not a cache hit; keep working, but indicate we should cache this
            $this->cacheThisRequest = true;
            $this->cacheKey         = $cacheKey;
            return;
        }<commit_msg>Use instance variable<commit_after><?php
/**
 * @link      https://github.com/weierophinney/PhlySimplePage for the canonical source repository
 * @copyright Copyright (c) 2012 <NAME> (http://mwop.net)
 * @license   https://github.com/weierophinney/PhlySimplePage/blog/master/LICENSE.md New BSD License
 */

namespace PhlySimplePage;

use Zend\Cache\Storage\Adapter\AbstractAdapter;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;

/**
 * Event listener implementing page level caching for pages provided by the 
 * PageController.
 */
class PageCacheListener implements ListenerAggregateInterface
{
    /**
     * @var AbstractAdapter
     */
    protected $cache;

    /**
     * @var \Zend\Stdlib\CallbackHandler[]
     */
    protected $listeners = array();

    /**
     * Whether or not to cache this request
     * 
     * @var bool
     */
    protected $cacheThisRequest = false;

    /**
     * Key to use when caching
     * 
     * @var string
     */
    protected $cacheKey;

    /**
     * Constructor
     * 
     * @param AbstractAdapter $cache 
     */
    public function __construct(AbstractAdapter $cache)
    {
        $this->cache = $cache;
    }

    /**
     * Attach listeners to the application event system.
     *
     * Registers two event listeners:
     *
     * - route, at priority -99, 
     * - finish, at priority -10001, 
     * 
     * @param EventManagerInterface $events 
     */
    public function attach(EventManagerInterface $events)
    {
        $events->attach('route', array($this, 'onRoutePost'), -99);
        $events->attach('finish', array($this, 'onFinishPost'), -10001);
    }

    /**
     * Detach any registered listeners from the given event manager instance.
     * 
     * @param EventManagerInterface $events 
     */
    public function detach(EventManagerInterface $events)
    {
        foreach ($this->listeners as $index => $listener) {
            if ($events->detach($listener)) {
                unset($this->listeners[$index]);
            }
        }
    }

    /**
     * "route" event listener
     *
     * Checks to see if (a) we have a controller we're interested in, (b) if
     * a "template" was provided in the route matches, and (c) if we have a 
     * cache hit for that template name. If we do, we return a populated 
     * response; if not, we continue, but indicate that we should cache the
     * response on completion.
     * 
     * @param  \Zend\Mvc\MvcEvent $e 
     * @return null|\Zend\Stdlib\ResponseInterface
     */
    public function onRoutePost($e)
    {
        $matches = $e->getRouteMatch();
        if (!$matches) {
            return;
        }

        $controller = $matches->getParam('controller');
        if ($controller != 'PhlySimplePage\Controller\Page') {
            return;
        }

        $template = $matches->getParam('template', false);
        if (!$template) {
            return;
        }

        $cacheKey = $this->normalizeCacheKey($template);
        
        $result = $this->cache->getItem($cacheKey, $success);
        if (!$success) {
            // Not a cache hit; keep working, but indicate we should cache this
            $this->cacheThisRequest = true;
            $this->cacheKey         = $cacheKey;
            return;
        }
{'fraction_non_alphanumeric': 0.11741623905825536, 'fraction_numerical': 0.005433142167220043, 'mean_word_length': 2.1708133971291868, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 34, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1676753', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1819, 'n_words': 502}