/** * @param string $resource * * @return mixed */ public function load($resource) { $id = sprintf('%s/%s.php', $this->cacheDir, crc32($resource)); if (null === $this->cache) { $this->cache = new FileCache(); } $conf = null; // check the cache if (!$this->cache->contains($id)) { // not in cache, delegate to the next loader $conf = $this->loader->load($resource); // get the list of depending resources $this->addDependingResources(); $this->cache->setResources($this->resources->all()); $this->resources->clear(); } if (null !== $conf) { $this->cache->save($id, $conf); } if (null === $conf) { $conf = $this->cache->fetch($id); } return $conf; }
/** * @param \G\Yaml2Pimple\Loader\YamlFileLoader $loader * @param \G\Yaml2Pimple\ResourceCollection $resources * @param \G\Yaml2Pimple\FileCache $cache */ public function it_adds_cache_depending_classes_to_resources($loader, $resources, $cache) { $this->setCache($cache); $cache->contains(Argument::any())->shouldBeCalled()->willReturn(false); $cache->setResources(Argument::any())->shouldBeCalled(); $cache->save(Argument::type('string'), Argument::type('array'))->shouldBeCalled(); $dependingClasses = array('\\G\\Yaml2Pimple\\Loader\\CacheLoader', '\\G\\Yaml2Pimple\\Parameter', '\\G\\Yaml2Pimple\\Definition'); foreach ($dependingClasses as $class) { $refl = new \ReflectionClass($class); $testResource = new FileResource($refl->getFileName()); $resources->add($testResource)->shouldBeCalled(); } $resources->all()->shouldBeCalled(); $resources->clear()->shouldBeCalled(); $data = array(); $loader->load(Argument::type('string'))->willReturn($data); $this->load('test.yaml')->shouldReturn($data); }