magallanes/src/Task/BuiltIn/Composer/SelfUpdateTask.php

87 lines
2.2 KiB
PHP
Raw Normal View History

2017-02-10 16:48:16 +01:00
<?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;
2017-02-10 16:48:16 +01:00
use Symfony\Component\Process\Process;
use DateTime;
2017-02-10 16:48:16 +01:00
/**
* Composer Task - Self update
*
* @author Yanick Witschi <https://github.com/Toflar>
*/
2017-04-14 20:24:39 +02:00
class SelfUpdateTask extends AbstractComposerTask
2017-02-10 16:48:16 +01:00
{
public function getName()
{
return 'composer/self-update';
2017-02-10 16:48:16 +01:00
}
public function getDescription()
{
return '[Composer] Self Update';
2017-02-10 16:48:16 +01:00
}
public function execute()
{
$options = $this->getOptions();
$cmdVersion = sprintf('%s --version', $options['path']);
2017-02-10 16:48:16 +01:00
/** @var Process $process */
$process = $this->runtime->runCommand(trim($cmdVersion));
2017-02-10 16:48:16 +01:00
if (!$process->isSuccessful()) {
return false;
}
$buildDate = $this->getBuildDate($process->getOutput());
if (!$buildDate instanceof DateTime) {
return false;
2017-02-10 16:48:16 +01:00
}
$compareDate = $this->getCompareDate();
if ($buildDate >= $compareDate) {
throw new SkipException();
2017-02-10 16:48:16 +01:00
}
$cmdUpdate = sprintf('%s self-update', $options['path']);
/** @var Process $process */
$process = $this->runtime->runCommand(trim($cmdUpdate));
2017-02-10 16:48:16 +01:00
return $process->isSuccessful();
2017-02-10 16:48:16 +01:00
}
protected function getBuildDate($output)
2017-02-10 16:48:16 +01:00
{
$buildDate = null;
2018-11-20 14:06:12 +01:00
$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));
}
2017-02-10 16:48:16 +01:00
}
return $buildDate;
2017-02-10 16:48:16 +01:00
}
protected function getCompareDate()
2017-02-10 16:48:16 +01:00
{
$options = $this->getOptions();
$compareDate = new DateTime();
$compareDate->modify(sprintf('now -%d days', $options['days']));
return $compareDate;
2017-02-10 16:48:16 +01:00
}
2017-04-14 20:24:39 +02:00
protected function getComposerOptions()
2017-02-10 16:48:16 +01:00
{
2017-04-14 20:24:39 +02:00
return ['days' => 60];
2017-02-10 16:48:16 +01:00
}
}