Source of file Cache.php

Size: 3,017 Bytes - Last Modified: 2015-12-22T09:12:14-05:00

../src/Cache.php

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
Covered by 2 test(s):
  • MvcLite\CacheTest::testInit with data set "with prefix"
  • MvcLite\CacheTest::testInit with data set "without prefix"
47
Covered by 2 test(s):
  • MvcLite\CacheTest::testInit with data set "with prefix"
  • MvcLite\CacheTest::testInit with data set "without prefix"
48
Covered by 2 test(s):
  • MvcLite\CacheTest::testInit with data set "with prefix"
  • MvcLite\CacheTest::testInit with data set "without prefix"
49505152535455565758596061626364
Covered by 3 test(s):
  • MvcLite\CacheTest::testGet with data set #0
  • MvcLite\CacheTest::testGet with data set #1
  • MvcLite\CacheTest::testGet with data set #2
65
Covered by 3 test(s):
  • MvcLite\CacheTest::testGet with data set #0
  • MvcLite\CacheTest::testGet with data set #1
  • MvcLite\CacheTest::testGet with data set #2
66
Covered by 3 test(s):
  • MvcLite\CacheTest::testGet with data set #0
  • MvcLite\CacheTest::testGet with data set #1
  • MvcLite\CacheTest::testGet with data set #2
6768
Covered by 3 test(s):
  • MvcLite\CacheTest::testGet with data set #0
  • MvcLite\CacheTest::testGet with data set #1
  • MvcLite\CacheTest::testGet with data set #2
6970717273747576777879
Covered by 2 test(s):
  • MvcLite\CacheTest::testGetFilepath with data set "string filepath, string expectations"
  • MvcLite\CacheTest::testGetFilepath with data set "empty filepath, empty expectations"
808182838485868788899091929394
Covered by 3 test(s):
  • MvcLite\CacheTest::testGet with data set #0
  • MvcLite\CacheTest::testGet with data set #1
  • MvcLite\CacheTest::testGet with data set #2
95
Covered by 3 test(s):
  • MvcLite\CacheTest::testGet with data set #0
  • MvcLite\CacheTest::testGet with data set #1
  • MvcLite\CacheTest::testGet with data set #2
96
Covered by 3 test(s):
  • MvcLite\CacheTest::testGet with data set #0
  • MvcLite\CacheTest::testGet with data set #1
  • MvcLite\CacheTest::testGet with data set #2
9798
Covered by 3 test(s):
  • MvcLite\CacheTest::testGet with data set #0
  • MvcLite\CacheTest::testGet with data set #1
  • MvcLite\CacheTest::testGet with data set #2
99100101102103104105106107108109110111
Covered by 1 test(s):
  • MvcLite\CacheTest::testGetCacheKey with data set "simple testing"
112113
Covered by 1 test(s):
  • MvcLite\CacheTest::testGetCacheKey with data set "simple testing"
114
Covered by 1 test(s):
  • MvcLite\CacheTest::testGetCacheKey with data set "simple testing"
115
Covered by 1 test(s):
  • MvcLite\CacheTest::testGetCacheKey with data set "simple testing"
116
Covered by 1 test(s):
  • MvcLite\CacheTest::testGetCacheKey with data set "simple testing"
117
Covered by 1 test(s):
  • MvcLite\CacheTest::testGetCacheKey with data set "simple testing"
118119
Covered by 1 test(s):
  • MvcLite\CacheTest::testGetCacheKey with data set "simple testing"
120
Covered by 1 test(s):
  • MvcLite\CacheTest::testGetCacheKey with data set "simple testing"
121
Covered by 1 test(s):
  • MvcLite\CacheTest::testGetCacheKey with data set "simple testing"
122
Covered by 1 test(s):
  • MvcLite\CacheTest::testGetCacheKey with data set "simple testing"
123
Covered by 1 test(s):
  • MvcLite\CacheTest::testGetCacheKey with data set "simple testing"
124125126
<?php
/**
 * Defines the caching mechanism
 *
 * @category    PHP
 * @package     MvcLite
 * @subpackage  Cache
 * @since       File available since release 2.0.0
 * @author      Cory Collier <corycollier@corycollier.com>
 */

namespace MvcLite;

use MvcLite\Filter;
use MvcLite\Traits\Singleton as SingletonTrait;
use MvcLite\Traits\Filepath as FilepathTrait;

/**
 * Defines the caching mechanism
 *
 * @category    PHP
 * @package     MvcLite
 * @subpackage  Cache
 * @since       File available since release 2.0.0
 * @author      Cory Collier <corycollier@corycollier.com>
 */
class Cache extends ObjectAbstract
{
    use SingletonTrait;
    use FilepathTrait;

    /**
     * property to store the configuration of the cache object
     *
     * @var array $config
     */
    protected $config;

    /**
     * initialize the cache instance
     *
     * @return MvcLite\Cache $this for object-chaining.
     */
    public function init($data = [])
    {
        $defaults = ['prefix' => 'cache'];
        $this->config = array_merge($defaults, $data);
        return $this;
    }

    /**
     * stores data from an object.
     *
     * The object is required, to determine the namespacing of the storage
     *
     * @param MvcLite\ObjectAbstract $object
     * @param string $name
     * @param unknown_type $data
     *
     * @return MvcLite\Cache $this for object-chaining.
     */
    public function set(ObjectAbstract $object, $name, $data)
    {
        $key = $this->getCacheKey($object, $name);
        $file = $this->getFilePath($key);
        file_put_contents($file, serialize($data));

        return $this;
    }

    /**
     * Returns the relative filepath for a given filename
     *
     * @param string $filename
     * @return string
     */
    protected function getFilePath($filename)
    {
        return $this->filepath([$this->config['directory'], $filename]);
    }

    /**
     * gets data for an object, and a value
     *
     * The object is required, to determine the namespacing of the storage
     *
     * @param MvcLite\ObjectAbstract $object
     * @param string $name
     *
     * @return mixed
     */
    public function get(ObjectAbstract $object, $name)
    {
        $key  = $this->getCacheKey($object, $name);
        $file = $this->getFilePath($key);
        $data = unserialize(file_get_contents($file));

        return $data;
    }

    /**
     * Returns a string to namespace a cache entry.
     *
     * @param MvcLite\ObjectAbstract $object
     * @param string $name
     *
     * @return string
     */
    protected function getCacheKey(ObjectAbstract $object, $name)
    {
        static $filter;

        if (! $filter) {
            $filter = new FilterChain;
            $filter->addFilter(new Filter\UnderscoreToDash);
            $filter->addFilter(new Filter\StringToLower);
        }

        return $filter->filter(implode('_', [
            $this->config['prefix'],
            get_class($object),
            $name,
        ]));
    }
}