<commit_before><?php

class EngineBlock_ServiceRegistry_CacheProxy_Exception extends EngineBlock_Exception
{
}

/**
 * A Caching Proxy for the Service Registry, will cache all function calls.
 *
 * Can even detect Sercvice Registry problems and chug along on the (stale) cache. 
 */
class EngineBlock_ServiceRegistry_CacheProxy
{
    const DEFAULT_LIFETIME = 5;

    public function __call($name, $arguments)
    {
        $client = $this->_getClient();

        $cache = $this->_getCacheFrontend();
        if (!$cache) {
            return call_user_method_array($name, $client, $arguments);
        }

        // Clone the original client because calling it will alter the $client object
        // making it impossible to reuse stale cache
        $originalClient = clone $client;

        try {
            return $cache->call(array($client, $name), $arguments);

        } catch(Exception $e) { // Whoa, something went wrong, maybe the SR is down? Trying to use stale cache...
            if (floatval(phpversion()) > 5.3) {
                $e = new EngineBlock_ServiceRegistry_CacheProxy_Exception("Service Registry problems?!?", 0, $e);
            }
            else {
                $e = new EngineBlock_ServiceRegistry_CacheProxy_Exception("Service Registry problems?!?", 0);
            }
            EngineBlock_ApplicationSingleton::getInstance()->reportError($e);

            // Give any stale cache some more time
            $callback = array($originalClient, $name);
            $cacheId = $cache->makeId($callback, $arguments);
            $cacheBackend = $cache->getBackend();
            $data = $cacheBackend->load($cacheId, TRUE);
            if ($data !== false) {
                $cacheBackend->save($data, $cacheId, array(), self::DEFAULT_LIFETIME);
            }

            // And try to use that cache.
            return $cache->call($callback, $arguments);
        }
    }

    /**
     * @return Zend_Cache_Frontend_Function
     */
    protected function _getCacheFrontend()
    {
        $application = EngineBlock_ApplicationSingleton::getInstance();
        $serviceRegistryConfig = $application->getConfiguration()->serviceRegistry;
        if (!isset($serviceRegistryConfig->caching)) {
            return null;
        }
        $cachingConfiguration = $serviceRegistryConfig->caching;

        $backendCaching        = $cachingConfiguration->backend->get('name', 'File');
        $backendCachingOptions = $cachingConfiguration->backend->options->toArray();
        if (isset($backendCachingOptions['file_name_prefix'])) {
            $backendCachingOptions['file_name_prefix'] .= '_' . $application->getApplicationEnvironmentId();
        }

        $cache = Zend_Cache::factory(
            'Function',
            $backendCaching,
            array(),
            $backendCachingOptions
        );
        $cache->setLifetime($cachingConfiguration->get('lifetime', self::DEFAULT_LIFETIME));
        return $cache;
    }

    protected function _getClient()
    {
        return new EngineBlock_ServiceRegistry_Client();
    }
}<commit_msg>Replace . in app env id with _ to make Zend_Cache happy<commit_after><?php

class EngineBlock_ServiceRegistry_CacheProxy_Exception extends EngineBlock_Exception
{
}

/**
 * A Caching Proxy for the Service Registry, will cache all function calls.
 *
 * Can even detect Sercvice Registry problems and chug along on the (stale) cache. 
 */
class EngineBlock_ServiceRegistry_CacheProxy
{
    const DEFAULT_LIFETIME = 5;

    public function __call($name, $arguments)
    {
        $client = $this->_getClient();

        $cache = $this->_getCacheFrontend();
        if (!$cache) {
            return call_user_method_array($name, $client, $arguments);
        }

        // Clone the original client because calling it will alter the $client object
        // making it impossible to reuse stale cache
        $originalClient = clone $client;

        try {
            return $cache->call(array($client, $name), $arguments);

        } catch(Exception $e) { // Whoa, something went wrong, maybe the SR is down? Trying to use stale cache...
            if (floatval(phpversion()) > 5.3) {
                $e = new EngineBlock_ServiceRegistry_CacheProxy_Exception("Service Registry problems?!?", 0, $e);
            }
            else {
                $e = new EngineBlock_ServiceRegistry_CacheProxy_Exception("Service Registry problems?!?", 0);
            }
            EngineBlock_ApplicationSingleton::getInstance()->reportError($e);

            // Give any stale cache some more time
            $callback = array($originalClient, $name);
            $cacheId = $cache->makeId($callback, $arguments);
            $cacheBackend = $cache->getBackend();
            $data = $cacheBackend->load($cacheId, TRUE);
            if ($data !== false) {
                $cacheBackend->save($data, $cacheId, array(), self::DEFAULT_LIFETIME);
            }

            // And try to use that cache.
            return $cache->call($callback, $arguments);
        }
    }

    /**
     * @return Zend_Cache_Frontend_Function
     */
    protected function _getCacheFrontend()
    {
        $application = EngineBlock_ApplicationSingleton::getInstance();
        $serviceRegistryConfig = $application->getConfiguration()->serviceRegistry;
        if (!isset($serviceRegistryConfig->caching)) {
            return null;
        }
        $cachingConfiguration = $serviceRegistryConfig->caching;

        $backendCaching        = $cachingConfiguration->backend->get('name', 'File');
        $backendCachingOptions = $cachingConfiguration->backend->options->toArray();
        if (isset($backendCachingOptions['file_name_prefix'])) {
            $backendCachingOptions['file_name_prefix'] .= '_' . str_replace($application->getApplicationEnvironmentId(), '.', '_');
        }

        $cache = Zend_Cache::factory(
            'Function',
            $backendCaching,
            array(),
            $backendCachingOptions
        );
        $cache->setLifetime($cachingConfiguration->get('lifetime', self::DEFAULT_LIFETIME));
        return $cache;
    }

    protected function _getClient()
    {
        return new EngineBlock_ServiceRegistry_Client();
    }
}
{'fraction_non_alphanumeric': 0.11647681694208246, 'fraction_numerical': 0.0016043638697256538, 'mean_word_length': 2.546075085324232, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, 'https://': 0, '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': '4549179', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1583, 'n_words': 377}