Exemplo n.º 1
0
 /**
  * Test the cache can be bootstrapped when using the redis driver.
  *
  * @covers Molovo\Amnesia\Cache::bootstrap
  * @covers Molovo\Amnesia\Cache\Instance::__construct
  * @covers Molovo\Amnesia\Driver\File::__construct
  * @covers Molovo\Amnesia\Cache::instance
  */
 public function testBootstrap()
 {
     $name = 'file_driver_test';
     $config = [$name => ['driver' => 'file', 'store_path' => dirname(dirname(__DIR__)) . '/_data/cache/store']];
     if (!is_dir($config[$name]['store_path'])) {
         // This is a test cache, so just let anyone write to it
         // to avoid having to deal with permissions issues
         mkdir($config[$name]['store_path'], 0777, true);
     }
     Cache::bootstrap($config);
     $instance = Cache::instance($name);
     verify($instance)->isInstanceOf(Instance::class);
     // Test that the driver has been instantiated correctly
     $property = new \ReflectionProperty(Instance::class, 'driver');
     $property->setAccessible(true);
     $driver = $property->getValue($instance);
     verify($driver)->isInstanceOf(File::class);
     // Call a second time to test retrieval from cache
     $cached_instance = Cache::instance($name);
     // Compare hashes of the two instances to ensure they are
     // the same object
     $hash1 = spl_object_hash($instance);
     $hash2 = spl_object_hash($cached_instance);
     verify($hash1)->equals($hash2);
     // Store the instance so we can use it in other tests
     static::$instance = $instance;
 }
Exemplo n.º 2
0
 /**
  * Test the cache can be bootstrapped when using the memcached driver.
  *
  * @covers Molovo\Amnesia\Cache::bootstrap
  * @covers Molovo\Amnesia\Cache\Instance::__construct
  * @covers Molovo\Amnesia\Driver\Memcached::__construct
  * @covers Molovo\Amnesia\Cache::instance
  */
 public function testBootstrap()
 {
     $name = 'memcached_driver_test';
     $config = [$name => ['driver' => 'memcached', 'servers' => [['localhost', 11211]]]];
     Cache::bootstrap($config);
     $instance = Cache::instance($name);
     verify($instance)->isInstanceOf(Instance::class);
     // Test that the driver has been instantiated correctly
     $property = new \ReflectionProperty(Instance::class, 'driver');
     $property->setAccessible(true);
     $driver = $property->getValue($instance);
     verify($driver)->isInstanceOf(Memcached::class);
     // Call a second time to test retrieval from cache
     $cached_instance = Cache::instance($name);
     // Compare hashes of the two instances to ensure they are
     // the same object
     $hash1 = spl_object_hash($instance);
     $hash2 = spl_object_hash($cached_instance);
     verify($hash1)->equals($hash2);
     // Store the instance so we can use it in other tests
     static::$instance = $instance;
 }
Exemplo n.º 3
0
 /**
  * Create a new instance of the cache and driver.
  *
  * @param string|null $name   The connection name
  * @param Config|null $config The config for the instance
  */
 public function __construct($name = null, Config $config = null)
 {
     // If a name isn't provided, then we'll use the default
     $this->name = $name ?: 'default';
     // Load the config if it isn't passed in
     if ($config === null) {
         $config = Cache::config()->{$this->name};
     }
     // If config is still null, then throw an exception
     if ($config === null) {
         throw new ConfigNotFoundException('No config could be found for instance ' . $this->name . '.');
     }
     // Set the name in the config as some drivers need it
     $config->name = $this->name;
     // Create a cache namespace key
     $this->key = hash('adler32', $name);
     // If the driver isn't created, throw an exception
     if (!isset($this->drivers[$config->driver])) {
         throw new InvalidDriverException($config->driver . ' is not a valid driver.');
     }
     // Initialise the driver
     $driverClass = $this->drivers[$config->driver];
     $this->driver = new $driverClass($config, $this);
 }
Exemplo n.º 4
0
 /**
  * Test the cache can be bootstrapped when using the redis driver.
  *
  * @covers Molovo\Amnesia\Cache::bootstrap
  * @covers Molovo\Amnesia\Cache\Instance::__construct
  * @covers Molovo\Amnesia\Driver\Redis::__construct
  * @covers Molovo\Amnesia\Cache::instance
  */
 public function testBootstrapSocket()
 {
     $name = 'redis_driver_socket_test';
     $config = [$name => ['driver' => 'redis', 'socket' => '/usr/local/var/run/redis/redis.sock']];
     Cache::bootstrap($config);
     $instance = Cache::instance($name);
     verify($instance)->isInstanceOf(Instance::class);
     // Test that the driver has been instantiated correctly
     $property = new \ReflectionProperty(Instance::class, 'driver');
     $property->setAccessible(true);
     $driver = $property->getValue($instance);
     verify($driver)->isInstanceOf(Redis::class);
     // Call a second time to test retrieval from cache
     $cached_instance = Cache::instance($name);
     // Compare hashes of the two instances to ensure they are
     // the same object
     $hash1 = spl_object_hash($instance);
     $hash2 = spl_object_hash($cached_instance);
     verify($hash1)->equals($hash2);
 }
Exemplo n.º 5
0
 /**
  * Set a config value.
  *
  * @param string $path  The path of the value to set
  * @param mixed  $value The value to set
  *
  * @return mixed The value
  */
 public static function set($key, $value = null)
 {
     return Cache::config()->setValueForPath($key, $value);
 }
Exemplo n.º 6
0
 /**
  * Create a new application instance.
  */
 public function __construct()
 {
     // Store the instance statically
     static::$instance = $this;
     // Load the app's config
     $this->loadConfig();
     // Register the error handler
     $this->registerErrorHandler();
     // Create our request and response objects
     $this->request = new Request();
     $this->response = new Response();
     // Bootstrap the database
     Database::bootstrap($this->config->db->toArray());
     // Convert relative store paths to absolute, and bootstrap the cache
     foreach ($this->config->cache as $instance => $config) {
         $cacheStorePath = $config->store_path;
         if ($cacheStorePath !== null) {
             if (!is_dir($cacheStorePath) && is_dir(APP_ROOT . $cacheStorePath)) {
                 $this->config->cache->{$instance}->store_path = APP_ROOT . $cacheStorePath;
             }
         }
     }
     Cache::bootstrap($this->config->cache->toArray());
     // Convert relative store paths to absolute, and bootstrap the session
     $sessionStorePath = $this->config->session->store_path;
     if ($sessionStorePath !== null) {
         if (!is_dir($sessionStorePath) && is_dir(APP_ROOT . $sessionStorePath)) {
             $this->config->session->store_path = APP_ROOT . $sessionStorePath;
         }
     }
     Session::bootstrap($this->config->session);
     // Include the app routes
     require APP_ROOT . 'routes.php';
     // Register global view variables
     View::addGlobal('appName', $this->config->app->name);
     View::addGlobal('app', $this);
     View::addGlobal('input', $this->request->input);
     $this->compileAssets();
     // Execute routes
     Router::execute();
     if (PHP_SAPI !== 'cli') {
         $this->checkRoute();
     }
 }
Exemplo n.º 7
0
 /**
  * Render the contents of a view.
  *
  * @param array $vars        Variables to pass to views
  * @param bool  $bypassCache Whether to bypass the cache
  *
  * @return string The rendered HTML
  */
 public function render(array $vars = [], $bypassCache = false)
 {
     // Check the cache
     if ($this->cache && !$bypassCache) {
         if (($content = Cache::get($this->cacheKey())) !== null) {
             return $content;
         }
     }
     $vars = array_merge($this->vars, $vars);
     switch ($this->type) {
         // The view is a markdown file
         case 'md':
         case 'markdown':
             $this->content = $this->renderMarkdown($vars);
             break;
             // The view is a mustache template
         // The view is a mustache template
         case 'mhtml':
         case 'mustache':
             $this->content = $this->renderMustache($vars);
             break;
             // The view is a simple PHP file
         // The view is a simple PHP file
         case 'php':
         default:
             $this->content = $this->renderPhp($vars);
             break;
     }
     $vars = array_merge($this->vars, $vars);
     if ($this->options['layout'] !== null) {
         $vars['content'] = $this->content;
         $this->content = (new self($this->options['layout'], $vars))->render();
     }
     if ($this->cache) {
         Cache::set($this->cacheKey(), $this->content, $this->cache_expiry);
     }
     return $this->content;
 }
Exemplo n.º 8
0
 /**
  * Create the instance which we will use during testing.
  */
 public static function setUpBeforeClass()
 {
     Cache::bootstrap(['default' => ['driver' => 'redis']]);
     static::$instance = Cache::instance();
 }