/**
  * {@inheritdoc}
  */
 public function teleport(Resource $resource, $context)
 {
     $response = $this->client->get($resource->getOriginal())->send();
     if (!$response->isSuccessful()) {
         throw new RuntimeException(sprintf('Unable to fetch %s', $resource->getOriginal()));
     }
     $response->getBody()->seek(0);
     $this->writeTarget($response->getBody(true), $resource, $context);
     return $this;
 }
 /**
  * Returns the appropriate TeleporterInterface given a Resource
  *
  * @param Resource $resource
  *
  * @return TeleporterInterface
  *
  * @throws InvalidArgumentException
  */
 public function fromResource(Resource $resource)
 {
     switch (true) {
         case is_resource($resource->getOriginal()):
             $teleporter = 'stream-teleporter';
             break;
         case is_string($resource->getOriginal()):
             $data = parse_url($resource->getOriginal());
             if (!isset($data['scheme']) || 'file' === $data['scheme']) {
                 $teleporter = 'local-teleporter';
             } elseif (in_array($data['scheme'], array('http', 'https'))) {
                 $teleporter = 'guzzle-teleporter';
             } else {
                 $teleporter = 'stream-teleporter';
             }
             break;
         default:
             throw new InvalidArgumentException('No teleporter found');
     }
     return $this[$teleporter];
 }
 /**
  * {@inheritdoc}
  */
 public function teleport(Resource $resource, $context)
 {
     $streamCreated = false;
     if (is_resource($resource->getOriginal())) {
         $stream = $resource->getOriginal();
     } else {
         $stream = @fopen($resource->getOriginal(), 'rb');
         if (!is_resource($stream)) {
             throw new InvalidArgumentException(sprintf('The stream or file "%s" could not be opened: ', $resource->getOriginal()));
         }
         $streamCreated = true;
     }
     $content = stream_get_contents($stream);
     if ($streamCreated) {
         fclose($stream);
     }
     $this->writeTarget($content, $resource, $context);
 }
Example #4
0
 /**
  * @return resource
  */
 public function getContentsAsStream()
 {
     $stream = is_resource($this->resource->getOriginal()) ? $this->resource->getOriginal() : @fopen($this->resource->getOriginal(), 'rb');
     return $stream;
 }
Example #5
0
 /**
  * @return \Guzzle\Http\Message\RequestInterface
  */
 private function buildRequest()
 {
     return $this->client->get($this->resource->getOriginal());
 }
 /**
  * @covers Alchemy\Zippy\Resource\Resource::canBeProcessedInPlace
  * @dataProvider provideProcessInPlaceData
  */
 public function testCanBeProcessedInPlace($expected, $context, $original, $target)
 {
     $resource = new Resource($original, $target);
     $this->assertInternalType('boolean', $resource->canBeProcessedInPlace($context));
     $this->assertEquals($expected, $resource->canBeProcessedInPlace($context));
 }
Example #7
0
 /**
  * Returns the relative target of a Resource
  *
  * @param String   $context
  * @param \Alchemy\Zippy\Resource\Resource $resource
  *
  * @return String
  */
 protected function getTarget($context, Resource $resource)
 {
     return sprintf('%s/%s', rtrim($context, '/'), $resource->getTarget());
 }