4 * This file is part of Zippy.
6 * (c) Alchemy <info@alchemy.fr>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 namespace Alchemy\Zippy\Adapter;
15 use Alchemy\Zippy\Adapter\Resource\ResourceInterface;
16 use Alchemy\Zippy\Adapter\VersionProbe\VersionProbeInterface;
17 use Alchemy\Zippy\Archive\Archive;
18 use Alchemy\Zippy\Archive\ArchiveInterface;
19 use Alchemy\Zippy\Exception\RuntimeException;
20 use Alchemy\Zippy\Exception\InvalidArgumentException;
21 use Alchemy\Zippy\Resource\PathUtil;
22 use Alchemy\Zippy\Resource\ResourceManager;
24 abstract class AbstractAdapter implements AdapterInterface
26 /** @var ResourceManager */
32 * @var VersionProbeInterface
36 public function __construct(ResourceManager $manager)
38 $this->manager = $manager;
44 public function open($path)
46 $this->requireSupport();
48 return new Archive($this->createResource($path), $this, $this->manager);
54 public function create($path, $files = null, $recursive = true)
56 $this->requireSupport();
58 return $this->doCreate($this->makeTargetAbsolute($path), $files, $recursive);
64 public function listMembers(ResourceInterface $resource)
66 $this->requireSupport();
68 return $this->doListMembers($resource);
74 public function add(ResourceInterface $resource, $files, $recursive = true)
76 $this->requireSupport();
78 return $this->doAdd($resource, $files, $recursive);
84 public function remove(ResourceInterface $resource, $files)
86 $this->requireSupport();
88 return $this->doRemove($resource, $files);
94 public function extract(ResourceInterface $resource, $to = null)
96 $this->requireSupport();
98 return $this->doExtract($resource, $to);
104 public function extractMembers(ResourceInterface $resource, $members, $to = null, $overwrite = false)
106 $this->requireSupport();
108 return $this->doExtractMembers($resource, $members, $to, $overwrite);
112 * Returns the version probe used by this adapter
114 * @return VersionProbeInterface
116 public function getVersionProbe()
122 * Sets the version probe used by this adapter
124 * @param VersionProbeInterface $probe
126 * @return VersionProbeInterface
128 public function setVersionProbe(VersionProbeInterface $probe)
130 $this->probe = $probe;
138 public function isSupported()
141 throw new RuntimeException(sprintf(
142 'No version probe has been set on %s whereas it is required', get_class($this)
146 return VersionProbeInterface::PROBE_OK === $this->probe->getStatus();
150 * Throws an exception is the current adapter is not supported
152 * @throws RuntimeException
154 protected function requireSupport()
156 if (false === $this->isSupported()) {
157 throw new RuntimeException(sprintf('%s is not supported on your system', get_class($this)));
162 * Change current working directory to another
164 * @param string $target the target directory
166 * @return AdapterInterface
168 * @throws RuntimeException In case of failure
170 protected function chdir($target)
172 if (false === @chdir($target)) {
173 throw new RuntimeException(sprintf('Unable to chdir to `%s`', $target));
180 * Creates a resource given a path
182 * @param string $path
184 * @return ResourceInterface
186 abstract protected function createResource($path);
189 * Do the removal after having check that the current adapter is supported
191 * @param ResourceInterface $resource
192 * @param array $files
196 abstract protected function doRemove(ResourceInterface $resource, $files);
199 * Do the add after having check that the current adapter is supported
201 * @param ResourceInterface $resource
202 * @param array $files
203 * @param bool $recursive
207 abstract protected function doAdd(ResourceInterface $resource, $files, $recursive);
210 * Do the extract after having check that the current adapter is supported
212 * @param ResourceInterface $resource
215 * @return \SplFileInfo The extracted archive
217 abstract protected function doExtract(ResourceInterface $resource, $to);
220 * Do the extract members after having check that the current adapter is supported
222 * @param ResourceInterface $resource
223 * @param string|string[] $members
225 * @param bool $overwrite
227 * @return \SplFileInfo The extracted archive
229 abstract protected function doExtractMembers(ResourceInterface $resource, $members, $to, $overwrite = false);
232 * Do the list members after having check that the current adapter is supported
234 * @param ResourceInterface $resource
238 abstract protected function doListMembers(ResourceInterface $resource);
241 * Do the create after having check that the current adapter is supported
243 * @param string $path
244 * @param string $file
245 * @param bool $recursive
247 * @return ArchiveInterface
249 abstract protected function doCreate($path, $file, $recursive);
252 * Makes the target path absolute as the adapters might have a different directory
254 * @param string $path The path to convert
256 * @return string The absolute path
258 * @throws InvalidArgumentException In case the path is not writable or does not exist
260 private function makeTargetAbsolute($path)
262 $directory = dirname($path);
264 if (!is_dir($directory)) {
265 throw new InvalidArgumentException(sprintf('Target path %s does not exist.', $directory));
267 if (!is_writable($directory)) {
268 throw new InvalidArgumentException(sprintf('Target path %s is not writeable.', $directory));
271 return realpath($directory) . '/' . PathUtil::basename($path);