magallanes/src/Task/BuiltIn/Composer/SelfUpdateTask.php
2018-11-20 14:06:12 +01:00

87 lines
2.2 KiB
PHP

<?php
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mage\Task\BuiltIn\Composer;
use Mage\Task\Exception\SkipException;
use Symfony\Component\Process\Process;
use DateTime;
/**
* Composer Task - Self update
*
* @author Yanick Witschi <https://github.com/Toflar>
*/
class SelfUpdateTask extends AbstractComposerTask
{
public function getName()
{
return 'composer/self-update';
}
public function getDescription()
{
return '[Composer] Self Update';
}
public function execute()
{
$options = $this->getOptions();
$cmdVersion = sprintf('%s --version', $options['path']);
/** @var Process $process */
$process = $this->runtime->runCommand(trim($cmdVersion));
if (!$process->isSuccessful()) {
return false;
}
$buildDate = $this->getBuildDate($process->getOutput());
if (!$buildDate instanceof DateTime) {
return false;
}
$compareDate = $this->getCompareDate();
if ($buildDate >= $compareDate) {
throw new SkipException();
}
$cmdUpdate = sprintf('%s self-update', $options['path']);
/** @var Process $process */
$process = $this->runtime->runCommand(trim($cmdUpdate));
return $process->isSuccessful();
}
protected function getBuildDate($output)
{
$buildDate = null;
$output = explode("\n", $output);
foreach ($output as $row) {
if (strpos($row, 'Composer version ') === 0) {
$buildDate = DateTime::createFromFormat('Y-m-d H:i:s', substr(trim($row), -19));
}
}
return $buildDate;
}
protected function getCompareDate()
{
$options = $this->getOptions();
$compareDate = new DateTime();
$compareDate->modify(sprintf('now -%d days', $options['days']));
return $compareDate;
}
protected function getComposerOptions()
{
return ['days' => 60];
}
}