/** * @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; }
/** * Loads the configuration file and returns it. * * @param string $file The configuration file path. * * @return Configuration The configuration settings. */ public function loadFile($file = null) { if (null === $file) { $file = $this->getDefaultPath(); } $json = $this->json->decodeFile($file); if (isset($json->import)) { if (!Path::isAbsolute($json->import)) { $json->import = Path::join(array(dirname($file), $json->import)); } $json = (object) array_merge((array) $this->json->decodeFile($json->import), (array) $json); } $this->json->validate($this->json->decodeFile(CRATE_SCHEMA_FILE), $json); return new Configuration($file, $json); }
/** * @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::split()` method using * the `getSplitPaths()` data provider. Any edge cases found should be * submitted as patch that will add a new entry to the data provider. * * @dataProvider getSplitPaths * * @param string $input The input path. * @param string $result The expected result. */ public function testSplit($input, $result) { $this->assertEquals($result, Path::split($input)); }
/** * Creates a new configuration loader. * * @param string $dir The configuration directory path. * * @return array The loader, configuration directory path, and supported * file extensions. */ private function createLoader($dir = null) { if (null === $dir || '.' === $dir) { $dir = Path::current(); } $extensions = array(); $locator = new FileLocator($dir); $loaders = array('\\KHerGe\\Box\\Config\\Loader\\JsonFileLoader' => null, '\\KHerGe\\Box\\Config\\Loader\\PhpFileLoader' => null, '\\KHerGe\\Box\\Config\\Loader\\YamlFileLoader' => null); /** @var AbstractFileLoader $loader */ foreach ($loaders as $class => &$loader) { $loader = new $class($locator); $extensions = array_merge($extensions, $loader->getSupportedExtensions()); } return array(new DelegatingLoader(new LoaderResolver($loaders)), $dir, $extensions); }