/** * @covers Yosmanyga\Resource\Resource::setMetadata * @covers Yosmanyga\Resource\Resource::getMetadata * @covers Yosmanyga\Resource\Resource::hasMetadata * @covers Yosmanyga\Resource\Resource::setType * @covers Yosmanyga\Resource\Resource::getType * @covers Yosmanyga\Resource\Resource::hasType */ public function testAccessors() { $resource = new Resource(); $resource->setMetadata('foo1', 'bar1'); $resource->setMetadata('foo2', 'bar2'); $resource->setType('foo'); $this->assertTrue($resource->hasMetadata('foo1')); $this->assertEquals('bar1', $resource->getMetadata('foo1')); $this->assertEquals(array('foo1' => 'bar1', 'foo2' => 'bar2'), $resource->getMetadata()); $this->assertFalse($resource->hasMetadata('foo3')); $this->assertEquals('foo', $resource->getType()); $this->assertTrue($resource->hasType()); $resource = new Resource(); $this->assertFalse($resource->hasType()); }
/** * @inheritdoc */ public function open(Resource $resource) { $file = $resource->getMetadata('file'); if (!is_file($file)) { throw new \InvalidArgumentException(sprintf('File "%s" not found.', $file)); } try { $annotation = $resource->hasMetadata('annotation') ? $resource->getMetadata('annotation') : ''; $data = $this->getData($file, $annotation); } catch (\Exception $e) { throw new \InvalidArgumentException($e->getMessage(), 0, $e); } $this->inMemoryReader = new InMemoryReader(); $this->inMemoryReader->open(new Resource(array('data' => $data), 'in_memory')); }
/** * @inheritdoc */ public function supports(Resource $resource, Resource $parentResource) { if ($resource->hasMetadata('file') && 0 === strpos(parse_url($resource->getMetadata('file'), PHP_URL_PATH), '/')) { return true; } return false; }
/** * @inheritdoc */ public function open(Resource $resource) { $this->data = $resource->getMetadata('data'); if (!is_array($this->data)) { throw new \InvalidArgumentException('Resource "data" metadata is not an array'); } }
/** * @inheritdoc */ public function read(Resource $resource) { $file = $resource->getMetadata('file'); if (!is_file($file)) { throw new \InvalidArgumentException(sprintf('File "%s" not found.', $file)); } return file_get_contents($file); }
/** * @inheritdoc */ public function supports($data, Resource $resource) { if ($resource->hasType('type')) { if ('ini' == $resource->getType()) { return true; } return false; } if ($resource->hasMetadata('file') && in_array(pathinfo($resource->getMetadata('file'), PATHINFO_EXTENSION), array('ini'))) { return true; } return false; }
private function calculateDirVersion(Resource $resource) { // Adjust finder $dir = $resource->getMetadata('dir'); if (!is_dir($dir)) { throw new \InvalidArgumentException(sprintf('Directory "%s" not found.', $dir)); } $this->finder->files()->in($dir); if ($resource->hasMetadata('filter')) { $this->finder->name($resource->getMetadata('filter')); } if ($resource->hasMetadata('depth')) { $this->finder->depth($resource->getMetadata('depth')); } else { $this->finder->depth('>= 0'); } $version = array(); /** @var \SplFileInfo $file */ foreach ($this->finder->getIterator() as $file) { $version[md5($file->getRealPath())] = filemtime($file); } return $version; }
/** * @inheritdoc */ public function open(Resource $resource) { $file = $resource->getMetadata('file'); if (!is_file($file)) { throw new \InvalidArgumentException(sprintf('File "%s" not found.', $file)); } $this->xmlReader = new \XmlReader(); $this->xmlReader->open($file); // Try to move pointer to first node inside root tag try { while ($this->xmlReader->read()) { $name = $this->xmlReader->name; $depth = $this->xmlReader->depth; if (1 == $depth && !in_array($name, array("", "#text"))) { break; } } } catch (\Exception $e) { throw new \InvalidArgumentException($e->getMessage(), 0, $e); } }
/** * @inheritdoc */ public function transform(Resource $resource, Resource $parentResource) { list($vendor, $path) = $this->parseFile($resource->getMetadata('file')); $file = sprintf("%s/%s/%s%s", dirname(dirname($this->file)), $vendor, $this->resolveSrc($vendor), $path); return new Resource(array('file' => $file)); }
/** * @param \Yosmanyga\Resource\Resource $resource * @param \SplFileInfo $file * @return \Yosmanyga\Resource\Resource */ private function convertResource(Resource $resource, \SplFileInfo $file) { return new Resource(array_merge($resource->getMetadata(), array('file' => $file->getRealpath())), $resource->getMetadata('type')); }
/** * @inheritdoc */ public function transform(Resource $resource, Resource $parentResource) { $file = sprintf("%s/%s", dirname($parentResource->getMetadata('file')), $resource->getMetadata('dir')); return new Resource(array('file' => $file)); }
/** * @param \Yosmanyga\Resource\Resource $resource * @return \Yosmanyga\Resource\Resource */ private function convertResource(Resource $resource) { return new Resource($resource->getMetadata(), $resource->getMetadata('type')); }