<commit_before><?php
/**
 *  BEAR.Framework
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 */
namespace BEAR\Framework;

use BEAR\Resource\Resource;
use Ray\Di\Definition;
use Ray\Di\Annotation;
use Ray\Di\Config;
use Ray\Di\Forge;
use Ray\Di\Container;
use Ray\Di\Injector;
use Ray\Di\InjectorInterface as Inject;
use BEAR\Framework\Router;
use BEAR\Framework\DevRouter;
use BEAR\Framework\Exception\NotFound;
use Aura\Autoload\Exception\NotReadable;
use BEAR\Framework\AppContext;

use Doctrine\Common\Cache\MemcacheCache as Cache;
use Guzzle\Common\Cache\DoctrineCacheAdapter as CacheAdapter;

/**
 * Dispatcher
 *
 * @package BEAR.Framework
 * @author  <NAME> <<EMAIL>>
 */
final class Dispatcher
{
    /**
     * Application context
     *
     * @var AppContext
     */
    private $app;


    /**
     * Constructor
     *
     * @param string $appName
     * @param string $appPath
     */
    public function __construct(AppContext $app, Cache $cache = null)
    {
        $this->app = $app;
        $this->cache = $cache;
    }

    public function setCache($cache)
    {
        $this->cache = $cache;
    }

    /**
     * Get instance
     *
     * @param string $pageUri Page resource path ("/hello/world")
     *
     * @return array [BEAR\Resource\ResourceInterface $resource, BEAR\Resource\Object $page]
     *
     * @throws ResourceNotFound
     */
    public function getInstance($pageUri)
    {
        if ($this->cache && $this->cached) {
            list($resource, $page) = unserialize($cached);
        }
        $resource = $this->app->resource;
        try {
            $page = $resource->newInstance($pageUri);
        } catch (NotReadable $e) {
            try {
                $page = $resource->newInstance($pageUri . 'index');
            } catch (NotReadable $e) {
                throw new Exception\ResourceNotFound($pageUri, 404, $e);
            }
        } catch (\Exception $e) {
            throw $e;
        }
        if ($this->cache) {
            $this->cache->save($key, serialize([$resource, $page]));
        }
        // serializable test
        $page = unserialize(serialize($page));<commit_msg>Dspatcher use APC storage.<commit_after><?php
/**
 *  BEAR.Framework
 *
 * @license http://opensource.org/licenses/bsd-license.php BSD
 */
namespace BEAR\Framework;

use BEAR\Resource\Resource;
use BEAR\Framework\Exception\NotFound;
use Aura\Autoload\Exception\NotReadable;

/**
 * Dispatcher
 *
 * @package BEAR.Framework
 * @author  <NAME> <<EMAIL>>
 */
final class Dispatcher
{
    /**
     * Application context
     *
     * @var AppContext
     */
    private $app;


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

    /**
     * Return resource client and resource object
     *
     * @param string $uri Page resource path ("/hello/world")
     *
     * @return array [BEAR\Resource\ResourceInterface $resource, BEAR\Resource\Object $page]
     *
     * @throws ResourceNotFound
     */
    public function getInstance($uri)
    {
        $key = 'BEAR_DISPATCH_' . $uri;
        if (apc_exists($key)) {
            list($resource, $page) = apc_fetch($key);
            return [$resource, $page];
        }
        $resource = $this->app->resource;
        try {
            $page = $resource->newInstance($uri);
        } catch (NotReadable $e) {
            try {
                $page = $resource->newInstance($uri . 'index');
            } catch (NotReadable $e) {
                throw new Exception\ResourceNotFound($uri, 404, $e);
            }
        } catch (\Exception $e) {
            throw $e;
        }
        apc_store($key, [$resource, $page]);
{'fraction_non_alphanumeric': 0.13866666666666666, 'fraction_numerical': 0.0016, 'mean_word_length': 2.1547518923465097, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 26, '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': '3136110', 'n_tokens_mistral': 1160, 'n_tokens_neox': 1136, 'n_words': 244}