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\FileResource;
16 use Alchemy\Zippy\Archive\MemberInterface;
17 use Alchemy\Zippy\Exception\RuntimeException;
18 use Alchemy\Zippy\Exception\InvalidArgumentException;
19 use Alchemy\Zippy\Parser\ParserFactory;
20 use Alchemy\Zippy\Parser\ParserInterface;
21 use Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactory;
22 use Alchemy\Zippy\ProcessBuilder\ProcessBuilderFactoryInterface;
23 use Alchemy\Zippy\Resource\ResourceManager;
24 use Symfony\Component\Process\ExecutableFinder;
25 use Symfony\Component\Process\ProcessBuilder;
27 abstract class AbstractBinaryAdapter extends AbstractAdapter implements BinaryAdapterInterface
30 * The parser to use to parse command output
32 * @var ParserInterface
37 * The deflator process builder factory to use to build binary command line
39 * @var ProcessBuilderFactoryInterface
44 * The inflator process builder factory to use to build binary command line
46 * @var ProcessBuilderFactoryInterface
53 * @param ParserInterface $parser An output parser
54 * @param ResourceManager $manager A resource manager
55 * @param ProcessBuilderFactoryInterface $inflator A process builder factory for the inflator binary
56 * @param ProcessBuilderFactoryInterface $deflator A process builder factory for the deflator binary
58 public function __construct(
59 ParserInterface $parser,
60 ResourceManager $manager,
61 ProcessBuilderFactoryInterface $inflator,
62 ProcessBuilderFactoryInterface $deflator
64 $this->parser = $parser;
65 parent::__construct($manager);
66 $this->deflator = $deflator;
67 $this->inflator = $inflator;
73 public function getParser()
81 public function setParser(ParserInterface $parser)
83 $this->parser = $parser;
91 public function getDeflator()
93 return $this->deflator;
99 public function getInflator()
101 return $this->inflator;
107 public function setDeflator(ProcessBuilderFactoryInterface $processBuilder)
109 $this->deflator = $processBuilder;
114 public function setInflator(ProcessBuilderFactoryInterface $processBuilder)
116 $this->inflator = $processBuilder;
124 public function getInflatorVersion()
126 $this->requireSupport();
128 return $this->doGetInflatorVersion();
134 public function getDeflatorVersion()
136 $this->requireSupport();
138 return $this->doGetDeflatorVersion();
142 * Returns a new instance of the invoked adapter
144 * @param ExecutableFinder $finder
145 * @param ResourceManager $manager
146 * @param string|null $inflatorBinaryName The inflator binary name to use
147 * @param string|null $deflatorBinaryName The deflator binary name to use
149 * @return AbstractBinaryAdapter
151 public static function newInstance(
152 ExecutableFinder $finder,
153 ResourceManager $manager,
154 $inflatorBinaryName = null,
155 $deflatorBinaryName = null
157 $inflator = $inflatorBinaryName instanceof ProcessBuilderFactoryInterface ? $inflatorBinaryName : self::findABinary($inflatorBinaryName,
158 static::getDefaultInflatorBinaryName(), $finder);
159 $deflator = $deflatorBinaryName instanceof ProcessBuilderFactoryInterface ? $deflatorBinaryName : self::findABinary($deflatorBinaryName,
160 static::getDefaultDeflatorBinaryName(), $finder);
163 $outputParser = ParserFactory::create(static::getName());
164 } catch (InvalidArgumentException $e) {
165 throw new RuntimeException(sprintf(
166 'Failed to get a new instance of %s',
167 get_called_class()), $e->getCode(), $e
171 if (null === $inflator) {
172 throw new RuntimeException(sprintf('Unable to create the inflator'));
175 if (null === $deflator) {
176 throw new RuntimeException(sprintf('Unable to create the deflator'));
179 return new static($outputParser, $manager, $inflator, $deflator);
182 private static function findABinary($wish, array $defaults, ExecutableFinder $finder)
184 $possibles = $wish ? (array) $wish : $defaults;
188 foreach ($possibles as $possible) {
189 if (null !== $found = $finder->find($possible)) {
190 $binary = new ProcessBuilderFactory($found);
199 * Adds files to argument list
201 * @param MemberInterface[]|\SplFileInfo[]|string[] $files An array of files
202 * @param ProcessBuilder $builder A Builder instance
206 protected function addBuilderFileArgument(array $files, ProcessBuilder $builder)
210 array_walk($files, function($file) use ($builder, &$iterations) {
212 $file instanceof \SplFileInfo ?
213 $file->getRealPath() : ($file instanceof MemberInterface ? $file->getLocation() : $file)
219 return 0 !== $iterations;
222 protected function createResource($path)
224 return new FileResource($path);
228 * Fetch the inflator version after having check that the current adapter is supported
232 abstract protected function doGetInflatorVersion();
235 * Fetch the Deflator version after having check that the current adapter is supported
239 abstract protected function doGetDeflatorVersion();