/** * Rename a file or folder to a target destination * * @param string $source The source * @param string $target The target * * @return void */ public function rename($source, $target) { if (true === @rename($source, $target)) { return; } if (!function_exists('proc_open')) { return $this->copyThenRemove($source, $target); } if (defined('PHP_WINDOWS_VERSION_BUILD')) { // Try to copy & delete - this is a workaround for random "Access denied" errors. $command = sprintf('xcopy %s %s /E /I /Q', escapeshellarg($source), escapeshellarg($target)); $result = $this->console->createProcess($command)->run(); // clear stat cache because external processes aren't tracked by the php stat cache clearstatcache(); if (0 === $result) { $this->remove($source); return; } } else { // We do not use PHP's "rename" function here since it does not support // the case where $source, and $target are located on different partitions. $command = sprintf('mv %s %s', escapeshellarg($source), escapeshellarg($target)); try { $this->console->createProcess($command)->run(); // clear stat cache because external processes aren't tracked by the php stat cache clearstatcache(); return; } catch (\Exception $e) { // do copyThenRemove } } return $this->copyThenRemove($source, $target); }
/** * The preview for this task - this is called right before execution and * in preview mode * * @return void */ public function preview() { $message = $this->get('message'); if ($message) { $this->console->output($message); } }
/** * Job constructor. * * @param Console $console Console */ public function __construct(Console $console) { static $kite, $composer; $this->console = $console; $this->offsetSet('job', $this); $this->offsetSet('config', $console->getConfig()); if (!$kite) { $kite = array('path' => $path = dirname(__DIR__), 'dir' => $console->getFilesystem()->findShortestPath(getcwd(), $path)); } $this->offsetSet('kite', $kite); parent::__construct($this); if (!$composer) { $composer = $this->factory->createTask('Netresearch\\Kite\\Service\\Composer', $this); } $this->offsetSet('composer', $composer); }
/** * Executes the current command. * * This method is not abstract because you can use this class * as a concrete class. In this case, instead of defining the * execute() method, you set the code to execute by passing * a Closure to the setCode() method. * * @param InputInterface $input An InputInterface instance * @param OutputInterface $output An OutputInterface instance * * @return null|int null or 0 if everything went fine, or an error code * * @throws \LogicException When this abstract method is not implemented * * @see setCode() */ protected function execute(InputInterface $input, OutputInterface $output) { $job = $this->getJob(); try { $job->run(); } catch (\Exception $e) { if ($e instanceof ExitException && $e->getCode() === 0) { if ($e->getMessage()) { $output->writeln('<info>' . $e->getMessage() . '</info>'); } return 0; } // This doesn't go to the debug log, as $output->writeln and not $console->output is used: $this->getApplication()->renderException($e, $output instanceof ConsoleOutput ? $output->getErrorOutput() : $output); // But this one: $this->getApplication()->renderException($e, $this->console->getDebugOutput()); $exitCode = $e->getCode(); if (is_numeric($exitCode)) { $exitCode = (int) $exitCode; if (0 === $exitCode) { $exitCode = 1; } } else { $exitCode = 1; } return $exitCode; } }
/** * Get a job mock * * @return \PHPUnit_Framework_MockObject_MockObject|Job */ protected function getJobMock($questionCallback = null) { $application = new Application(); if ($questionCallback) { $questionHelperMock = $this->getMock('\\Symfony\\Component\\Console\\Helper\\QuestionHelper', ['ask']); $questionHelperMock->expects($this->any())->method('ask')->willReturnCallback($questionCallback); $application->getHelperSet()->set($questionHelperMock); } $console = new Console(new Config()); $console->setInput(new ArrayInput([])); $console->setOutput(new ConsoleOutput(ConsoleOutput::VERBOSITY_QUIET)); $console->setDebugOutput(new ConsoleOutput(ConsoleOutput::VERBOSITY_QUIET)); $console->setApplication($application); $job = new \Netresearch\Kite\Job($console); $job->get('composer')->invalidatePackages(); return $job; }
/** * Execute a shell command * * @param callable|null $callback A PHP callback to run whenever there is some * output available on STDOUT or STDERR * * @return mixed The output of the shell command or FALSE if the command returned a non-zero exit code and $ignoreErrors was enabled. */ public function run($callback = null) { $command = $this->getCommandLine(); $this->console->output('<cmd>' . $this->getWorkingDirectory() . ' > ' . $command . '</cmd>', OutputInterface::VERBOSITY_VERBOSE); if ($this->dryRun) { return null; } parent::run(function ($type, $buffer) use($callback) { if ($callback) { call_user_func($callback, $type, $buffer); } if (!$this->shy || $type !== self::OUT) { $this->console->output($buffer, $this->pt ? $this->console->getVerbosity() : OutputInterface::VERBOSITY_DEBUG, false, !$this->pt); } }); if ($this->getExitCode() !== 0) { throw new ProcessFailedException($this); } return trim($this->getOutput()); }
/** * Create a job * * @param string $job The job name * * @return Job */ public function createJob($job) { $jobInstance = new Job($this->console); return $jobInstance->setFromArray($this->console->getConfig()->getJobConfiguration($job)); }