Update Composer, update everything

This commit is contained in:
Oliver Davies 2018-11-23 12:29:20 +00:00
parent ea3e94409f
commit dda5c284b6
19527 changed files with 1135420 additions and 351004 deletions

View file

@ -0,0 +1,20 @@
<?php
/*
* This file is part of Zippy.
*
* (c) Alchemy <info@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Zippy\Resource;
abstract class PathUtil
{
public static function basename($path)
{
return (false === $pos = strrpos(strtr($path, '\\', '/'), '/')) ? $path : substr($path, $pos + 1);
}
}

View file

@ -0,0 +1,59 @@
<?php
namespace Alchemy\Zippy\Resource\Reader\Guzzle;
use Alchemy\Zippy\Resource\Resource as ZippyResource;
use Alchemy\Zippy\Resource\ResourceReader;
use GuzzleHttp\ClientInterface;
class GuzzleReader implements ResourceReader
{
/**
* @var ClientInterface
*/
private $client;
/**
* @var \Alchemy\Zippy\Resource\Resource
*/
private $resource;
/**
* @param ZippyResource $resource
* @param ClientInterface $client
*/
public function __construct(ZippyResource $resource, ClientInterface $client = null)
{
$this->resource = $resource;
$this->client = $client;
}
/**
* @return string
*/
public function getContents()
{
return $this->buildRequest()->getBody()->getContents();
}
/**
* @return resource
*/
public function getContentsAsStream()
{
$response = $this->buildRequest()->getBody()->getContents();
$stream = fopen('php://temp', 'r+');
if ($response != '') {
fwrite($stream, $response);
fseek($stream, 0);
}
return $stream;
}
private function buildRequest()
{
return $this->client->request('GET', $this->resource->getOriginal());
}
}

View file

@ -0,0 +1,37 @@
<?php
namespace Alchemy\Zippy\Resource\Reader\Guzzle;
use Alchemy\Zippy\Resource\Resource as ZippyResource;
use Alchemy\Zippy\Resource\ResourceReader;
use Alchemy\Zippy\Resource\ResourceReaderFactory;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
class GuzzleReaderFactory implements ResourceReaderFactory
{
/**
* @var ClientInterface|null
*/
private $client = null;
public function __construct(ClientInterface $client = null)
{
$this->client = $client;
if (! $this->client) {
$this->client = new Client();
}
}
/**
* @param ZippyResource $resource
* @param string $context
*
* @return ResourceReader
*/
public function getReader(ZippyResource $resource, $context)
{
return new GuzzleReader($resource, $this->client);
}
}

View file

@ -0,0 +1,67 @@
<?php
namespace Alchemy\Zippy\Resource\Reader\Guzzle;
use Alchemy\Zippy\Resource\Resource as ZippyResource;
use Alchemy\Zippy\Resource\ResourceReader;
use Guzzle\Http\Client;
use Guzzle\Http\ClientInterface;
use Guzzle\Http\EntityBodyInterface;
class LegacyGuzzleReader implements ResourceReader
{
/**
* @var ClientInterface
*/
private $client;
/**
* @var \Alchemy\Zippy\Resource\Resource $resource
*/
private $resource;
/**
* This is necessary to prevent the underlying PHP stream from being destroyed
* @link https://github.com/guzzle/guzzle/issues/366#issuecomment-20295409
* @var EntityBodyInterface|null
*/
private $stream = null;
/**
* @param ZippyResource $resource
* @param ClientInterface $client
*/
public function __construct(ZippyResource $resource, ClientInterface $client = null)
{
$this->client = $client ?: new Client();
$this->resource = $resource;
}
/**
* @return string
*/
public function getContents()
{
return $this->buildRequest()->send()->getBody(true);
}
/**
* @return resource
*/
public function getContentsAsStream()
{
if (!$this->stream) {
$this->stream = $this->buildRequest()->send()->getBody(false);
}
return $this->stream->getStream();
}
/**
* @return \Guzzle\Http\Message\RequestInterface
*/
private function buildRequest()
{
return $this->client->get($this->resource->getOriginal());
}
}

View file

@ -0,0 +1,47 @@
<?php
namespace Alchemy\Zippy\Resource\Reader\Guzzle;
use Alchemy\Zippy\Resource\Resource as ZippyResource;
use Alchemy\Zippy\Resource\ResourceReader;
use Alchemy\Zippy\Resource\ResourceReaderFactory;
use Guzzle\Http\Client;
use Guzzle\Http\ClientInterface;
use Guzzle\Plugin\Backoff\BackoffPlugin;
use Symfony\Component\EventDispatcher\Event;
class LegacyGuzzleReaderFactory implements ResourceReaderFactory
{
/**
* @var ClientInterface|null
*/
private $client = null;
public function __construct(ClientInterface $client = null)
{
$this->client = $client;
if (!$this->client) {
$this->client = new Client();
$this->client->getEventDispatcher()->addListener('request.error', function(Event $event) {
// override guzzle default behavior of throwing exceptions
// when 4xx & 5xx responses are encountered
$event->stopPropagation();
}, -254);
$this->client->addSubscriber(BackoffPlugin::getExponentialBackoff(5, array(500, 502, 503, 408)));
}
}
/**
* @param ZippyResource $resource
* @param string $context
*
* @return ResourceReader
*/
public function getReader(ZippyResource $resource, $context)
{
return new LegacyGuzzleReader($resource, $this->client);
}
}

View file

@ -0,0 +1,41 @@
<?php
namespace Alchemy\Zippy\Resource\Reader\Stream;
use Alchemy\Zippy\Resource\Resource as ZippyResource;
use Alchemy\Zippy\Resource\ResourceReader;
class StreamReader implements ResourceReader
{
/**
* @var ZippyResource
*/
private $resource;
/**
* @param ZippyResource $resource
*/
public function __construct(ZippyResource $resource)
{
$this->resource = $resource;
}
/**
* @return string
*/
public function getContents()
{
return file_get_contents($this->resource->getOriginal());
}
/**
* @return resource
*/
public function getContentsAsStream()
{
$stream = is_resource($this->resource->getOriginal()) ?
$this->resource->getOriginal() : @fopen($this->resource->getOriginal(), 'rb');
return $stream;
}
}

View file

@ -0,0 +1,21 @@
<?php
namespace Alchemy\Zippy\Resource\Reader\Stream;
use Alchemy\Zippy\Resource\Resource as ZippyResource;
use Alchemy\Zippy\Resource\ResourceReader;
use Alchemy\Zippy\Resource\ResourceReaderFactory;
class StreamReaderFactory implements ResourceReaderFactory
{
/**
* @param ZippyResource $resource
* @param string $context
*
* @return ResourceReader
*/
public function getReader(ZippyResource $resource, $context)
{
return new StreamReader($resource);
}
}

View file

@ -0,0 +1,66 @@
<?php
/*
* This file is part of Zippy.
*
* (c) Alchemy <info@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Zippy\Resource;
class RequestMapper
{
private $locator;
/**
* Constructor
*
* @param TargetLocator $locator
*/
public function __construct(TargetLocator $locator)
{
$this->locator = $locator;
}
/**
* Maps resources request to a ResourceCollection
*
* @param $context
* @param array $resources
*
* @return ResourceCollection
*/
public function map($context, array $resources)
{
$data = array();
foreach ($resources as $location => $resource) {
if (is_int($location)) {
$data[] = new Resource($resource, $this->locator->locate($context, $resource));
} else {
$data[] = new Resource($resource, ltrim($location, '/'));
}
}
if (count($data) === 1) {
$context = $data[0]->getOriginal();
}
$collection = new ResourceCollection($context, $data, false);
return $collection;
}
/**
* Creates the default RequestMapper
*
* @return RequestMapper
*/
public static function create()
{
return new static(new TargetLocator());
}
}

View file

@ -0,0 +1,116 @@
<?php
/*
* This file is part of Zippy.
*
* (c) Alchemy <info@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Zippy\Resource;
class Resource
{
private $original;
private $target;
/**
* Constructor
*
* @param string $original
* @param string $target
*/
public function __construct($original, $target)
{
$this->original = $original;
$this->target = $target;
}
/**
* Returns the target
*
* @return string
*/
public function getTarget()
{
return $this->target;
}
/**
* Returns the original path
*
* @return string
*/
public function getOriginal()
{
return $this->original;
}
/**
* Returns whether the resource can be processed in place given a context or not.
*
* For example :
* - /path/to/file1 can be processed to file1 in /path/to context
* - /path/to/subdir/file2 can be processed to subdir/file2 in /path/to context
*
* @param string $context
*
* @return bool
*/
public function canBeProcessedInPlace($context)
{
if (!is_string($this->original)) {
return false;
}
if (!$this->isLocal()) {
return false;
}
$data = parse_url($this->original);
return sprintf('%s/%s', rtrim($context, '/'), $this->target) === $data['path'];
}
/**
* Returns a context for computing this resource in case it is possible.
*
* Useful to avoid teleporting.
*
* @return null|string
*/
public function getContextForProcessInSinglePlace()
{
if (!is_string($this->original)) {
return null;
}
if (!$this->isLocal()) {
return null;
}
if (PathUtil::basename($this->original) === $this->target) {
return dirname($this->original);
}
return null;
}
/**
* Returns true if the resource is local.
*
* @return bool
*/
private function isLocal()
{
if (!is_string($this->original)) {
return false;
}
$data = parse_url($this->original);
return isset($data['path']);
}
}

View file

@ -0,0 +1,89 @@
<?php
/*
* This file is part of Zippy.
*
* (c) Alchemy <info@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Zippy\Resource;
use Alchemy\Zippy\Exception\InvalidArgumentException;
use Doctrine\Common\Collections\ArrayCollection;
class ResourceCollection extends ArrayCollection
{
private $context;
/**
* @var bool
*/
private $temporary;
/**
* Constructor
* @param string $context
* @param Resource[] $elements An array of Resource
* @param bool $temporary
*/
public function __construct($context, array $elements, $temporary)
{
array_walk($elements, function($element) {
if (!$element instanceof Resource) {
throw new InvalidArgumentException('ResourceCollection only accept Resource elements');
}
});
$this->context = $context;
$this->temporary = (bool) $temporary;
parent::__construct($elements);
}
/**
* Returns the context related to the collection
*
* @return string
*/
public function getContext()
{
return $this->context;
}
/**
* Tells whether the collection is temporary or not.
*
* A ResourceCollection is temporary when it required a temporary folder to
* fetch data
*
* @return bool
*/
public function isTemporary()
{
return $this->temporary;
}
/**
* Returns true if all resources can be processed in place, false otherwise
*
* @return bool
*/
public function canBeProcessedInPlace()
{
if (count($this) === 1) {
if (null !== $context = $this->first()->getContextForProcessInSinglePlace()) {
$this->context = $context;
return true;
}
}
foreach ($this as $resource) {
if (!$resource->canBeProcessedInPlace($this->context)) {
return false;
}
}
return true;
}
}

View file

@ -0,0 +1,13 @@
<?php
namespace Alchemy\Zippy\Resource;
use Alchemy\Zippy\Resource\Resource AS ZippyResource;
class ResourceLocator
{
public function mapResourcePath(ZippyResource $resource, $context)
{
return rtrim($context, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $resource->getTarget();
}
}

View file

@ -0,0 +1,105 @@
<?php
/*
* This file is part of Zippy.
*
* (c) Alchemy <info@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Zippy\Resource;
use Alchemy\Zippy\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOException as SfIOException;
class ResourceManager
{
private $mapper;
private $teleporter;
private $filesystem;
/**
* Constructor
*
* @param RequestMapper $mapper
* @param ResourceTeleporter $teleporter
* @param Filesystem $filesystem
*/
public function __construct(RequestMapper $mapper, ResourceTeleporter $teleporter, Filesystem $filesystem)
{
$this->mapper = $mapper;
$this->filesystem = $filesystem;
$this->teleporter = $teleporter;
}
/**
* Handles an archival request.
*
* The request is an array of string|streams to compute in a context (current
* working directory most of the time)
* Some keys can be associative. In these cases, the key is used the target
* for the file.
*
* @param string $context
* @param array $request
*
* @return ResourceCollection
*
* @throws IOException In case of write failure
*/
public function handle($context, array $request)
{
$collection = $this->mapper->map($context, $request);
if (!$collection->canBeProcessedInPlace()) {
$context = sprintf('%s/%s', sys_get_temp_dir(), uniqid('zippy_'));
try {
$this->filesystem->mkdir($context);
} catch (SfIOException $e) {
throw new IOException(sprintf('Could not create temporary folder %s', $context), $e->getCode(), $e);
}
foreach ($collection as $resource) {
$this->teleporter->teleport($context, $resource);
}
$collection = new ResourceCollection($context, $collection->toArray(), true);
}
return $collection;
}
/**
* This method must be called once the ResourceCollection has been processed.
*
* It will remove temporary files
*
* @todo this should be done in the __destruct method of ResourceCollection
*
* @param ResourceCollection $collection
*/
public function cleanup(ResourceCollection $collection)
{
if ($collection->isTemporary()) {
try {
$this->filesystem->remove($collection->getContext());
} catch (IOException $e) {
// log this ?
}
}
}
/**
* Creates a default ResourceManager
*
* @return ResourceManager
*/
public static function create()
{
return new static(RequestMapper::create(), ResourceTeleporter::create(), new Filesystem());
}
}

View file

@ -0,0 +1,16 @@
<?php
namespace Alchemy\Zippy\Resource;
interface ResourceReader
{
/**
* @return string
*/
public function getContents();
/**
* @return resource
*/
public function getContentsAsStream();
}

View file

@ -0,0 +1,16 @@
<?php
namespace Alchemy\Zippy\Resource;
use Alchemy\Zippy\Resource\Resource as ZippyResource;
interface ResourceReaderFactory
{
/**
* @param ZippyResource $resource
* @param string $context
*
* @return ResourceReader
*/
public function getReader(ZippyResource $resource, $context);
}

View file

@ -0,0 +1,55 @@
<?php
/*
* This file is part of Zippy.
*
* (c) Alchemy <info@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Zippy\Resource;
use Alchemy\Zippy\Resource\Resource as ZippyResource;
class ResourceTeleporter
{
private $container;
/**
* Constructor
*
* @param TeleporterContainer $container
*/
public function __construct(TeleporterContainer $container)
{
$this->container = $container;
}
/**
* Teleports a resource to its target in the context
*
* @param string $context
* @param ZippyResource $resource
*
* @return ResourceTeleporter
*/
public function teleport($context, ZippyResource $resource)
{
$this
->container
->fromResource($resource)
->teleport($resource, $context);
return $this;
}
/**
* Creates the ResourceTeleporter with the default TeleporterContainer
*
* @return ResourceTeleporter
*/
public static function create()
{
return new static(TeleporterContainer::load());
}
}

View file

@ -0,0 +1,8 @@
<?php
namespace Alchemy\Zippy\Resource;
interface ResourceWriter
{
public function writeFromReader(ResourceReader $reader, $target);
}

View file

@ -0,0 +1,158 @@
<?php
/*
* This file is part of Zippy.
*
* (c) Alchemy <info@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Zippy\Resource;
use Alchemy\Zippy\Exception\TargetLocatorException;
class TargetLocator
{
/**
* Locates the target for a resource in a context
*
* For example, adding /path/to/file where the context (current working
* directory) is /path/to will return `file` as target
*
* @param string $context
* @param string|resource $resource
*
* @return string
*
* @throws TargetLocatorException when the resource is invalid
*/
public function locate($context, $resource)
{
switch (true) {
case is_resource($resource):
return $this->locateResource($resource);
case is_string($resource):
return $this->locateString($context, $resource);
case $resource instanceof \SplFileInfo:
return $this->locateString($context, $resource->getRealPath());
default:
throw new TargetLocatorException($resource, 'Unknown resource format');
}
}
/**
* Locate the target for a resource.
*
* @param resource $resource
*
* @return string
*
* @throws TargetLocatorException
*/
private function locateResource($resource)
{
$meta = stream_get_meta_data($resource);
$data = parse_url($meta['uri']);
if (!isset($data['path'])) {
throw new TargetLocatorException($resource, 'Unable to retrieve path from resource');
}
return PathUtil::basename($data['path']);
}
/**
* Locate the target for a string.
*
* @param $context
* @param string $resource
*
* @return string
*
* @throws TargetLocatorException
*/
private function locateString($context, $resource)
{
$url = parse_url($resource);
if (isset($url['scheme']) && $this->isLocalFilesystem($url['scheme'])) {
$resource = $url['path'] = $this->cleanupPath($url['path']);
}
// resource is a URI
if (isset($url['scheme'])) {
if ($this->isLocalFilesystem($url['scheme']) && $this->isFileInContext($url['path'], $context)) {
return $this->getRelativePathFromContext($url['path'], $context);
}
return PathUtil::basename($resource);
}
// resource is a local path
if ($this->isFileInContext($resource, $context)) {
$resource = $this->cleanupPath($resource);
return $this->getRelativePathFromContext($resource, $context);
} else {
return PathUtil::basename($resource);
}
}
/**
* Removes backward path sequences (..)
*
* @param string $path
*
* @return string
*
* @throws TargetLocatorException In case the path is invalid
*/
private function cleanupPath($path)
{
if (false === $cleanPath = realpath($path)) {
throw new TargetLocatorException($path, sprintf('%s is an invalid location', $path));
}
return $cleanPath;
}
/**
* Checks whether the path belong to the context
*
* @param string $path A resource path
* @param string $context
*
* @return bool
*/
private function isFileInContext($path, $context)
{
return 0 === strpos($path, $context);
}
/**
* Gets the relative path from the context for the given path
*
* @param string $path A resource path
* @param string $context
*
* @return string
*/
private function getRelativePathFromContext($path, $context)
{
return ltrim(str_replace($context, '', $path), '/\\');
}
/**
* Checks if a scheme refers to a local filesystem
*
* @param string $scheme
*
* @return bool
*/
private function isLocalFilesystem($scheme)
{
return 'plainfile' === $scheme || 'file' === $scheme;
}
}

View file

@ -0,0 +1,64 @@
<?php
/*
* This file is part of Zippy.
*
* (c) Alchemy <info@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Zippy\Resource\Teleporter;
use Alchemy\Zippy\Exception\IOException;
use Alchemy\Zippy\Resource\Resource as ZippyResource;
/**
* Class AbstractTeleporter
* @package Alchemy\Zippy\Resource\Teleporter
*
* @deprecated Typehint against TeleporterInterface instead and use GenericTeleporter
* with custom reader/writers instead. This class will be removed in v0.5.x
*/
abstract class AbstractTeleporter implements TeleporterInterface
{
/**
* Writes the target
*
* @param string $data
* @param ZippyResource $resource
* @param string $context
*
* @return TeleporterInterface
*
* @throws IOException
*/
protected function writeTarget($data, ZippyResource $resource, $context)
{
$target = $this->getTarget($context, $resource);
if (!file_exists(dirname($target)) && false === mkdir(dirname($target))) {
throw new IOException(sprintf('Could not create parent directory %s', dirname($target)));
}
if (false === file_put_contents($target, $data)) {
throw new IOException(sprintf('Could not write to %s', $target));
}
return $this;
}
/**
* Returns the relative target of a Resource
*
* @param string $context
* @param ZippyResource $resource
*
* @return string
*/
protected function getTarget($context, ZippyResource $resource)
{
return sprintf('%s/%s', rtrim($context, '/'), $resource->getTarget());
}
}

View file

@ -0,0 +1,60 @@
<?php
namespace Alchemy\Zippy\Resource\Teleporter;
use Alchemy\Zippy\Exception\InvalidArgumentException;
use Alchemy\Zippy\Exception\IOException;
use Alchemy\Zippy\Resource\Resource as ZippyResource;
use Alchemy\Zippy\Resource\ResourceLocator;
use Alchemy\Zippy\Resource\ResourceReaderFactory;
use Alchemy\Zippy\Resource\ResourceWriter;
class GenericTeleporter implements TeleporterInterface
{
/**
* @var ResourceReaderFactory
*/
private $readerFactory;
/**
* @var ResourceWriter
*/
private $resourceWriter;
/**
* @var ResourceLocator
*/
private $resourceLocator;
/**
* @param ResourceReaderFactory $readerFactory
* @param ResourceWriter $resourceWriter
* @param ResourceLocator $resourceLocator
*/
public function __construct(
ResourceReaderFactory $readerFactory,
ResourceWriter $resourceWriter,
ResourceLocator $resourceLocator = null
) {
$this->readerFactory = $readerFactory;
$this->resourceWriter = $resourceWriter;
$this->resourceLocator = $resourceLocator ?: new ResourceLocator();
}
/**
* Teleports a file from a destination to an other
*
* @param ZippyResource $resource A Resource
* @param string $context The current context
*
* @throws IOException when file could not be written on local
* @throws InvalidArgumentException when path to file is not valid
*/
public function teleport(ZippyResource $resource, $context)
{
$reader = $this->readerFactory->getReader($resource, $context);
$target = $this->resourceLocator->mapResourcePath($resource, $context);
$this->resourceWriter->writeFromReader($reader, $target);
}
}

View file

@ -0,0 +1,45 @@
<?php
/*
* This file is part of Zippy.
*
* (c) Alchemy <info@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Zippy\Resource\Teleporter;
use Alchemy\Zippy\Resource\Reader\Guzzle\GuzzleReaderFactory;
use Alchemy\Zippy\Resource\ResourceLocator;
use Alchemy\Zippy\Resource\ResourceReaderFactory;
use Alchemy\Zippy\Resource\Writer\FilesystemWriter;
/**
* Guzzle Teleporter implementation for HTTP resources
*
* @deprecated Use \Alchemy\Zippy\Resource\GenericTeleporter instead. This class will be removed in v0.5.x
*/
class GuzzleTeleporter extends GenericTeleporter
{
/**
* @param ResourceReaderFactory $readerFactory
* @param ResourceLocator $resourceLocator
*/
public function __construct(ResourceReaderFactory $readerFactory = null, ResourceLocator $resourceLocator = null)
{
parent::__construct($readerFactory ?: new GuzzleReaderFactory(), new FilesystemWriter(), $resourceLocator);
}
/**
* Creates the GuzzleTeleporter
*
* @deprecated This method will be removed in v0.5.x
* @return GuzzleTeleporter
*/
public static function create()
{
return new static();
}
}

View file

@ -0,0 +1,51 @@
<?php
/*
* This file is part of Zippy.
*
* (c) Alchemy <info@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Zippy\Resource\Teleporter;
use Alchemy\Zippy\Resource\Reader\Guzzle\LegacyGuzzleReaderFactory;
use Alchemy\Zippy\Resource\ResourceLocator;
use Alchemy\Zippy\Resource\ResourceReaderFactory;
use Alchemy\Zippy\Resource\Writer\FilesystemWriter;
use Guzzle\Http\Client;
/**
* Guzzle Teleporter implementation for HTTP resources
*
* @deprecated Use \Alchemy\Zippy\Resource\GenericTeleporter instead. This class will be removed in v0.5.x
*/
class LegacyGuzzleTeleporter extends GenericTeleporter
{
/**
* @param Client $client
* @param ResourceReaderFactory $readerFactory
* @param ResourceLocator $resourceLocator
*/
public function __construct(
Client $client = null,
ResourceReaderFactory $readerFactory = null,
ResourceLocator $resourceLocator = null
) {
parent::__construct($readerFactory ?: new LegacyGuzzleReaderFactory($client), new FilesystemWriter(),
$resourceLocator);
}
/**
* Creates the GuzzleTeleporter
*
* @deprecated
* @return LegacyGuzzleTeleporter
*/
public static function create()
{
return new static();
}
}

View file

@ -0,0 +1,82 @@
<?php
/*
* This file is part of Zippy.
*
* (c) Alchemy <info@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Zippy\Resource\Teleporter;
use Alchemy\Zippy\Exception\InvalidArgumentException;
use Alchemy\Zippy\Exception\IOException;
use Alchemy\Zippy\Resource\Resource as ZippyResource;
use Alchemy\Zippy\Resource\ResourceLocator;
use Symfony\Component\Filesystem\Exception\IOException as SfIOException;
use Symfony\Component\Filesystem\Filesystem;
/**
* This class transports an object using the local filesystem
*/
class LocalTeleporter extends AbstractTeleporter
{
/**
* @var Filesystem
*/
private $filesystem;
/**
* @var ResourceLocator
*/
private $resourceLocator;
/**
* Constructor
*
* @param Filesystem $filesystem
*/
public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
$this->resourceLocator = new ResourceLocator();
}
/**
* {@inheritdoc}
*/
public function teleport(ZippyResource $resource, $context)
{
$target = $this->resourceLocator->mapResourcePath($resource, $context);
$path = $resource->getOriginal();
if (!file_exists($path)) {
throw new InvalidArgumentException(sprintf('Invalid path %s', $path));
}
try {
if (is_file($path)) {
$this->filesystem->copy($path, $target);
} elseif (is_dir($path)) {
$this->filesystem->mirror($path, $target);
} else {
throw new InvalidArgumentException(sprintf('Invalid file or directory %s', $path));
}
} catch (SfIOException $e) {
throw new IOException(sprintf('Could not write %s', $target), $e->getCode(), $e);
}
}
/**
* Creates the LocalTeleporter
*
* @return LocalTeleporter
* @deprecated This method will be removed in a future release (0.5.x)
*/
public static function create()
{
return new static(new Filesystem());
}
}

View file

@ -0,0 +1,38 @@
<?php
/*
* This file is part of Zippy.
*
* (c) Alchemy <info@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Zippy\Resource\Teleporter;
use Alchemy\Zippy\Resource\Reader\Stream\StreamReaderFactory;
use Alchemy\Zippy\Resource\ResourceLocator;
use Alchemy\Zippy\Resource\Writer\StreamWriter;
/**
* This class transport an object using php stream wrapper
*/
class StreamTeleporter extends GenericTeleporter
{
public function __construct()
{
parent::__construct(new StreamReaderFactory(), new StreamWriter(), new ResourceLocator());
}
/**
* Creates the StreamTeleporter
*
* @return StreamTeleporter
* @deprecated This method will be removed in a future release (0.5.x)
*/
public static function create()
{
return new static();
}
}

View file

@ -0,0 +1,30 @@
<?php
/*
* This file is part of Zippy.
*
* (c) Alchemy <info@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Zippy\Resource\Teleporter;
use Alchemy\Zippy\Exception\InvalidArgumentException;
use Alchemy\Zippy\Exception\IOException;
use Alchemy\Zippy\Resource\Resource as ZippyResource;
interface TeleporterInterface
{
/**
* Teleports a file from a destination to an other
*
* @param ZippyResource $resource A Resource
* @param string $context The current context
*
* @throws IOException when file could not be written on local
* @throws InvalidArgumentException when path to file is not valid
*/
public function teleport(ZippyResource $resource, $context);
}

View file

@ -0,0 +1,189 @@
<?php
/*
* This file is part of Zippy.
*
* (c) Alchemy <info@alchemy.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Zippy\Resource;
use Alchemy\Zippy\Exception\InvalidArgumentException;
use Alchemy\Zippy\Resource\Reader\Guzzle\GuzzleReaderFactory;
use Alchemy\Zippy\Resource\Reader\Guzzle\LegacyGuzzleReaderFactory;
use Alchemy\Zippy\Resource\Resource as ZippyResource;
use Alchemy\Zippy\Resource\Teleporter\GenericTeleporter;
use Alchemy\Zippy\Resource\Teleporter\LocalTeleporter;
use Alchemy\Zippy\Resource\Teleporter\StreamTeleporter;
use Alchemy\Zippy\Resource\Teleporter\TeleporterInterface;
use Alchemy\Zippy\Resource\Writer\FilesystemWriter;
use Symfony\Component\Filesystem\Filesystem;
/**
* A container of TeleporterInterface
*/
class TeleporterContainer implements \ArrayAccess, \Countable
{
/**
* @var TeleporterInterface[]
*/
private $teleporters = array();
/**
* @var callable[]
*/
private $factories = array();
/**
* Returns the appropriate TeleporterInterface for a given Resource
*
* @param ZippyResource $resource
*
* @return TeleporterInterface
*/
public function fromResource(ZippyResource $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')) && isset($this->factories['guzzle-teleporter'])) {
$teleporter = 'guzzle-teleporter';
} else {
$teleporter = 'stream-teleporter';
}
break;
default:
throw new InvalidArgumentException('No teleporter found');
}
return $this->getTeleporter($teleporter);
}
private function getTeleporter($typeName)
{
if (!isset($this->teleporters[$typeName])) {
$factory = $this->factories[$typeName];
$this->teleporters[$typeName] = $factory();
}
return $this->teleporters[$typeName];
}
/**
* Instantiates TeleporterContainer and register default teleporters
*
* @return TeleporterContainer
*/
public static function load()
{
$container = new static();
$container->factories['stream-teleporter'] = function () {
return new StreamTeleporter();
};
$container->factories['local-teleporter'] = function () {
return new LocalTeleporter(new Filesystem());
};
if (class_exists('GuzzleHttp\Client')) {
$container->factories['guzzle-teleporter'] = function () {
return new GenericTeleporter(
new GuzzleReaderFactory(),
new FilesystemWriter(),
new ResourceLocator()
);
};
}
elseif (class_exists('Guzzle\Http\Client')) {
$container->factories['guzzle-teleporter'] = function () {
return new GenericTeleporter(
new LegacyGuzzleReaderFactory(),
new FilesystemWriter(),
new ResourceLocator()
);
};
}
return $container;
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Whether a offset exists
*
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
*
* @param mixed $offset <p>
* An offset to check for.
* </p>
*
* @return bool true on success or false on failure.
* </p>
* <p>
* The return value will be casted to boolean if non-boolean was returned.
*/
public function offsetExists($offset)
{
return isset($this->teleporters[$offset]);
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Offset to retrieve
* @link http://php.net/manual/en/arrayaccess.offsetget.php
* @param mixed $offset <p>
* The offset to retrieve.
* </p>
* @return mixed Can return all value types.
*/
public function offsetGet($offset)
{
return $this->getTeleporter($offset);
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Offset to set
* @link http://php.net/manual/en/arrayaccess.offsetset.php
* @param mixed $offset <p>
* The offset to assign the value to.
* </p>
* @param mixed $value <p>
* The value to set.
* </p>
* @return void
*/
public function offsetSet($offset, $value)
{
throw new \BadMethodCallException();
}
/**
* (PHP 5 &gt;= 5.0.0)<br/>
* Offset to unset
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @param mixed $offset <p>
* The offset to unset.
* </p>
* @return void
*/
public function offsetUnset($offset)
{
throw new \BadMethodCallException();
}
public function count()
{
return count($this->teleporters);
}
}

View file

@ -0,0 +1,23 @@
<?php
namespace Alchemy\Zippy\Resource\Writer;
use Alchemy\Zippy\Resource\ResourceReader;
use Alchemy\Zippy\Resource\ResourceWriter;
class FilesystemWriter implements ResourceWriter
{
/**
* @param ResourceReader $reader
* @param string $target
*/
public function writeFromReader(ResourceReader $reader, $target)
{
$directory = dirname($target);
if (!is_dir($directory)) {
mkdir($directory, 0777, true);
}
file_put_contents($target, $reader->getContentsAsStream());
}
}

View file

@ -0,0 +1,27 @@
<?php
namespace Alchemy\Zippy\Resource\Writer;
use Alchemy\Zippy\Resource\ResourceReader;
use Alchemy\Zippy\Resource\ResourceWriter;
class StreamWriter implements ResourceWriter
{
/**
* @param ResourceReader $reader
* @param string $target
*/
public function writeFromReader(ResourceReader $reader, $target)
{
$directory = dirname($target);
if (!is_dir($directory)) {
mkdir($directory, 0777, true);
}
$targetResource = fopen($target, 'w+');
$sourceResource = $reader->getContentsAsStream();
stream_copy_to_stream($sourceResource, $targetResource);
fclose($targetResource);
}
}