/** * @override */ protected function execute(InputInterface $input, OutputInterface $output) { $verbose = OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity(); $phar = $input->getArgument('phar'); if ($verbose) { $output->writeln('Extracting files from the Phar...'); } if (false === is_file($phar)) { $output->writeln(sprintf('<error>The path "%s" is not a file or does not exist.</error>', $phar)); return 1; } if (null === ($out = $input->getOption('out'))) { $out = $phar . '-contents'; } $phar = new Phar($phar); $files = $input->getOption('pick') ?: null; // backslash paths causes segfault if ($files) { array_walk($files, function (&$file) { $file = str_replace('\\', '/', Path::canonical($file)); }); } $phar->extractTo($out, $files, true); unset($phar); if ($verbose) { $output->writeln('Done.'); } return 0; }
/** * @param string $template_dir * @return array */ private function getTemplateFilesRecursive($template_dir) { $template_pattern = $this->template_pattern; $files = array(); $itr = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($template_dir)); foreach ($itr as $file) { if ($file->isDir()) { continue; } $path = Path::canonical($file->getPathname()); if (preg_match($template_pattern, $path)) { $files[] = $path; } } return $files; }
/** * @override */ protected function execute(InputInterface $input, OutputInterface $output) { $verbose = OutputInterface::VERBOSITY_VERBOSE === $output->getVerbosity(); $phar = $input->getArgument('phar'); if ($verbose) { $output->writeln('Removing files from the Phar...'); } if (false === is_file($phar)) { $output->writeln(sprintf('<error>The path "%s" is not a file or does not exist.</error>', $phar)); return 1; } $phar = new Phar($phar); foreach ((array) $input->getArgument('file') as $file) { if (isset($phar[$file])) { $phar->delete(str_replace('\\', '/', Path::canonical($file))); } } unset($phar); if ($verbose) { $output->writeln('Done.'); } return 0; }
/** * Processes the Finders configuration list. * * @param array $config The configuration. * * @return Finder[] The list of Finders. * * @throws InvalidArgumentException If the configured method does not exist. */ private function processFinders(array $config) { $finders = array(); $filter = $this->getBlacklistFilter(); foreach ($config as $methods) { $finder = Finder::create()->files()->filter($filter)->ignoreVCS(true); if (isset($methods->in)) { $base = $this->getBasePath(); $methods->in = (array) $methods->in; array_walk($methods->in, function (&$directory) use($base) { $directory = Path::canonical($base . DIRECTORY_SEPARATOR . $directory); }); } foreach ($methods as $method => $arguments) { if (false === method_exists($finder, $method)) { throw new InvalidArgumentException(sprintf('The method "Finder::%s" does not exist.', $method)); } $arguments = (array) $arguments; foreach ($arguments as $argument) { $finder->{$method}($argument); } } $finders[] = $finder; } return $finders; }
/** * Similar to Phar::buildFromIterator(), except the files will be compacted * and their placeholders replaced. * * @param Traversable $iterator The iterator. * @param string $base The base directory path. * * @throws Exception\Exception * @throws UnexpectedValueException If the iterator value is unexpected. */ public function buildFromIterator(Traversable $iterator, $base = null) { if ($base) { $base = Path::canonical($base . DIRECTORY_SEPARATOR); } foreach ($iterator as $key => $value) { if (is_string($value)) { if (false === is_string($key)) { throw UnexpectedValueException::create('The key returned by the iterator (%s) is not a string.', gettype($key)); } $key = Path::canonical($key); $value = Path::canonical($value); if (is_dir($value)) { $this->phar->addEmptyDir($key); } else { $this->addFile($value, $key); } } elseif ($value instanceof SplFileInfo) { if (null === $base) { throw InvalidArgumentException::create('The $base argument is required for SplFileInfo values.'); } /** @var $value SplFileInfo */ $real = $value->getRealPath(); if (0 !== strpos($real, $base)) { throw UnexpectedValueException::create('The file "%s" is not in the base directory.', $real); } $local = str_replace($base, '', $real); if ($value->isDir()) { $this->phar->addEmptyDir($local); } else { $this->addFile($real, $local); } } else { throw UnexpectedValueException::create('The iterator value "%s" was not expected.', gettype($value)); } } }
/** * @override */ protected function execute(InputInterface $input, OutputInterface $output) { if (false === $input->getOption('stub') && null === $input->getArgument('local')) { $output->writeln('<error>The <info>local</info> argument is required.</error>'); return 1; } $this->config = $this->getConfig($input); $phar = $input->getArgument('phar'); $file = $input->getArgument('file'); // load bootstrap file if (null !== ($bootstrap = $this->config->getBootstrapFile())) { $this->config->loadBootstrap(); $this->putln('?', "Loading bootstrap file: {$bootstrap}"); unset($bootstrap); } $this->putln('*', 'Adding to the Phar...'); if (false === is_file($phar)) { $output->writeln(sprintf('<error>The path "%s" is not a file or does not exist.</error>', $phar)); return 1; } if (false === is_file($file)) { $output->writeln(sprintf('<error>The path "%s" is not a file or does not exist.</error>', $file)); return 1; } if (false == preg_match('/^\\w+:\\/\\//', $file)) { $file = realpath($file); } $this->box = Box::create($phar); // set replacement values, if any if (array() !== ($values = $this->config->getProcessedReplacements())) { $this->putln('?', 'Setting replacement values...'); if ($this->isVerbose()) { foreach ($values as $key => $value) { $this->putln('+', "{$key}: {$value}"); } } $this->box->setValues($values); unset($values, $key, $value); } // register configured compactors if (array() !== ($compactors = $this->config->getCompactors())) { $this->putln('?', 'Registering compactors...'); foreach ($compactors as $compactor) { $this->putln('+', get_class($compactor)); $this->box->addCompactor($compactor); } } // add the file $phar = $this->box->getPhar(); $local = str_replace('\\', '/', Path::canonical($input->getArgument('local'))); if (isset($phar[$local]) && false === $input->getOption('replace')) { $output->writeln(sprintf('<error>The file "%s" already exists in the Phar.</error>', $local)); return 1; } if ($input->getOption('binary')) { $this->putln('?', "Adding binary file: {$file}"); $phar->addFile($file, $local); } elseif ($input->getOption('stub')) { $this->putln('?', "Using stub file: {$file}"); $this->box->setStubUsingFile($file); } elseif ($input->getOption('main')) { $this->putln('?', "Adding main file: {$file}"); if (false === ($contents = @file_get_contents($file))) { $error = error_get_last(); throw new RuntimeException($error['message']); } $this->box->addFromString($local, preg_replace('/^#!.*\\s*/', '', $contents)); } else { $this->putln('?', "Adding file: {$file}"); $this->box->addFile($file, $local); } unset($this->box, $phar); $this->putln('*', 'Done.'); return 0; }
/** * Performs a battery of tests against the `Path::canonical()` method * using the `getCanonicalPaths()` data provider. Any edge cases found * should be submitted as patch that will add a new entry to the data * provider. * * @dataProvider getCanonicalPaths * * @param string $input The input path. * @param string $result The expected result. */ public function testCanonical($input, $result) { $this->assertEquals($result, Path::canonical($input)); }