mirror of
https://github.com/hauke68/Magallanes.git
synced 2026-08-28 18:18:57 +02:00
Change structure.
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
<?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;
|
||||
|
||||
use Mage\Runtime\Runtime;
|
||||
|
||||
/**
|
||||
* Abstract base class for Magallanes Tasks
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
abstract class AbstractTask
|
||||
{
|
||||
/**
|
||||
* @var array Task custom options
|
||||
*/
|
||||
protected $options = [];
|
||||
|
||||
/**
|
||||
* @var Runtime
|
||||
*/
|
||||
protected $runtime;
|
||||
|
||||
/**
|
||||
* Get the Name/Code of the Task
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getName();
|
||||
|
||||
/**
|
||||
* Get a short Description of the Task
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function getDescription();
|
||||
|
||||
/**
|
||||
* Executes the Command
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
abstract public function execute();
|
||||
|
||||
/**
|
||||
* Set additional Options for the Task
|
||||
*
|
||||
* @param array $options Options
|
||||
* @return AbstractTask
|
||||
*/
|
||||
public function setOptions($options = [])
|
||||
{
|
||||
if (!is_array($options)) {
|
||||
$options = [];
|
||||
}
|
||||
$this->options = $options;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Runtime instance
|
||||
*
|
||||
* @param Runtime $runtime
|
||||
* @return AbstractTask
|
||||
*/
|
||||
public function setRuntime(Runtime $runtime)
|
||||
{
|
||||
$this->runtime = $runtime;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?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 Symfony\Component\Process\Process;
|
||||
use Mage\Task\AbstractTask;
|
||||
|
||||
/**
|
||||
* Composer Task - Generate Autoload
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class DumpAutoloadTask extends AbstractTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'composer/dump-autoload';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return '[Composer] Dump Autoload';
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$options = $this->getOptions();
|
||||
$command = sprintf('%s dump-autoload %s', $options['path'], $options['flags']);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runCommand(trim($command));
|
||||
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
|
||||
protected function getOptions()
|
||||
{
|
||||
$userOptions = $this->runtime->getConfigOptions('composer', []);
|
||||
$options = array_merge(
|
||||
['path' => 'composer', 'flags' => '--optimize'],
|
||||
(is_array($userOptions) ? $userOptions : []),
|
||||
$this->options
|
||||
);
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?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 Symfony\Component\Process\Process;
|
||||
use Mage\Task\AbstractTask;
|
||||
|
||||
/**
|
||||
* Composer Task - Install Vendors
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class InstallTask extends AbstractTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'composer/install';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return '[Composer] Install';
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$options = $this->getOptions();
|
||||
$command = sprintf('%s install %s', $options['path'], $options['flags']);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runCommand(trim($command));
|
||||
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
|
||||
protected function getOptions()
|
||||
{
|
||||
$userOptions = $this->runtime->getConfigOptions('composer', []);
|
||||
$options = array_merge(
|
||||
['path' => 'composer', 'flags' => '--optimize-autoloader'],
|
||||
(is_array($userOptions) ? $userOptions : []),
|
||||
$this->options
|
||||
);
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?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\Deploy\Release;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
use Mage\Task\AbstractTask;
|
||||
|
||||
/**
|
||||
* Release Task - Cleanup old releases
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class CleanupTask extends AbstractTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'deploy/release/cleanup';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return '[Release] Cleaning up old Releases';
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/');
|
||||
$currentReleaseId = $this->runtime->getReleaseId();
|
||||
$maxReleases = $this->runtime->getEnvironmentConfig('releases');
|
||||
|
||||
$cmdListReleases = sprintf('ls -1 %s/releases', $hostPath);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runRemoteCommand($cmdListReleases, false);
|
||||
if ($process->isSuccessful()) {
|
||||
$releases = $process->getOutput();
|
||||
$releases = explode(PHP_EOL, trim($releases));
|
||||
|
||||
if (count($releases) > $maxReleases) {
|
||||
sort($releases);
|
||||
$releasesToDelete = array_slice($releases, 0, count($releases) - $maxReleases);
|
||||
foreach ($releasesToDelete as $releaseId) {
|
||||
if ($releaseId != $currentReleaseId) {
|
||||
$cmdDeleteRelease = sprintf('rm -rf %s/releases/%s', $hostPath, $releaseId);
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runRemoteCommand($cmdDeleteRelease, false);
|
||||
if (!$process->isSuccessful()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?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\Deploy\Release;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
use Mage\Task\AbstractTask;
|
||||
|
||||
/**
|
||||
* Release Task - Create the Release Directory
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class PrepareTask extends AbstractTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'deploy/release/prepare';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return '[Release] Preparing Release';
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/');
|
||||
|
||||
$cmdMakeDir = sprintf('mkdir -p %s/releases/%s', $hostPath, $this->runtime->getReleaseId());
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runRemoteCommand($cmdMakeDir, false);
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?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\Deploy;
|
||||
|
||||
use Mage\Task\Exception\ErrorException;
|
||||
use Mage\Task\ExecuteOnRollbackInterface;
|
||||
use Symfony\Component\Process\Process;
|
||||
use Mage\Task\AbstractTask;
|
||||
|
||||
/**
|
||||
* Release Task - Create the Symlink
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class ReleaseTask extends AbstractTask implements ExecuteOnRollbackInterface
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'deploy/release';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return '[Release] Creating Symlink';
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
if (!$this->runtime->getEnvironmentConfig('releases', false)) {
|
||||
throw new ErrorException('This task is only available with releases enabled', 40);
|
||||
}
|
||||
|
||||
$hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/');
|
||||
$releaseId = $this->runtime->getReleaseId();
|
||||
|
||||
$cmdLinkRelease = sprintf('cd %s && ln -snf releases/%s current', $hostPath, $releaseId);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runRemoteCommand($cmdLinkRelease, false);
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?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\Deploy;
|
||||
|
||||
use Mage\Task\Exception\ErrorException;
|
||||
use Symfony\Component\Process\Process;
|
||||
use Mage\Task\AbstractTask;
|
||||
|
||||
/**
|
||||
* Rsync Task - Copy files with Rsync
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class RsyncTask extends AbstractTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'deploy/rsync';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return '[Deploy] Copying files with Rsync';
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$flags = $this->runtime->getConfigOptions('rsync', '-avz');
|
||||
$sshConfig = $this->runtime->getSSHConfig();
|
||||
$user = $this->runtime->getEnvironmentConfig('user', $this->runtime->getCurrentUser());
|
||||
$host = $this->runtime->getWorkingHost();
|
||||
$hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/');
|
||||
$targetDir = rtrim($hostPath, '/');
|
||||
|
||||
if ($this->runtime->getEnvironmentConfig('releases', false)) {
|
||||
throw new ErrorException('Can\'t be used with Releases, use "deploy/targz/copy"');
|
||||
}
|
||||
|
||||
$excludes = $this->getExcludes();
|
||||
$cmdRsync = sprintf('rsync -e "ssh -p %d %s" %s %s ./ %s@%s:%s', $sshConfig['port'], $sshConfig['flags'], $flags, $excludes, $user, $host, $targetDir);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runLocalCommand($cmdRsync, 600);
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
|
||||
protected function getExcludes()
|
||||
{
|
||||
$excludes = $this->runtime->getEnvironmentConfig('exclude', []);
|
||||
$excludes = array_merge(['.git'], $excludes);
|
||||
|
||||
foreach ($excludes as &$exclude) {
|
||||
$exclude = '--exclude=' . $exclude;
|
||||
}
|
||||
|
||||
return implode(' ', $excludes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
<?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\Deploy\TarGz;
|
||||
|
||||
use Mage\Task\Exception\ErrorException;
|
||||
use Symfony\Component\Process\Process;
|
||||
use Mage\Task\AbstractTask;
|
||||
|
||||
/**
|
||||
* TarGz Task - Delete temporal Tar
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class CleanupTask extends AbstractTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'deploy/targz/cleanup';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return '[Deploy] Cleanup TarGZ file';
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
if (!$this->runtime->getEnvironmentConfig('releases', false)) {
|
||||
throw new ErrorException('This task is only available with releases enabled', 40);
|
||||
}
|
||||
|
||||
$tarGzLocal = $this->runtime->getVar('targz_local');
|
||||
|
||||
$cmdDeleteTarGz = sprintf('rm %s', $tarGzLocal);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runLocalCommand($cmdDeleteTarGz);
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
<?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\Deploy\TarGz;
|
||||
|
||||
use Mage\Task\Exception\ErrorException;
|
||||
use Symfony\Component\Process\Process;
|
||||
use Mage\Task\AbstractTask;
|
||||
|
||||
/**
|
||||
* TarGz Task - Copy Tar
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class CopyTask extends AbstractTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'deploy/targz/copy';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return '[Deploy] Copying files with TarGZ';
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
if (!$this->runtime->getEnvironmentConfig('releases', false)) {
|
||||
throw new ErrorException('This task is only available with releases enabled', 40);
|
||||
}
|
||||
|
||||
$user = $this->runtime->getEnvironmentConfig('user', $this->runtime->getCurrentUser());
|
||||
$host = $this->runtime->getWorkingHost();
|
||||
$sshConfig = $sshConfig = $this->runtime->getSSHConfig();
|
||||
$hostPath = rtrim($this->runtime->getEnvironmentConfig('host_path'), '/');
|
||||
$currentReleaseId = $this->runtime->getReleaseId();
|
||||
|
||||
$targetDir = sprintf('%s/releases/%s', $hostPath, $currentReleaseId);
|
||||
|
||||
$tarGzLocal = $this->runtime->getVar('targz_local');
|
||||
$tarGzRemote = basename($tarGzLocal);
|
||||
|
||||
$cmdCopy = sprintf('scp -P %d %s %s %s@%s:%s/%s', $sshConfig['port'], $sshConfig['flags'], $tarGzLocal, $user, $host, $targetDir, $tarGzRemote);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runLocalCommand($cmdCopy, 300);
|
||||
if ($process->isSuccessful()) {
|
||||
$cmdUnTar = sprintf('cd %s && tar xfz %s', $targetDir, $tarGzRemote);
|
||||
$process = $this->runtime->runRemoteCommand($cmdUnTar, false, 600);
|
||||
if ($process->isSuccessful()) {
|
||||
$cmdDelete = sprintf('rm %s/%s', $targetDir, $tarGzRemote);
|
||||
$process = $this->runtime->runRemoteCommand($cmdDelete, false);
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?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\Deploy\TarGz;
|
||||
|
||||
use Mage\Task\Exception\ErrorException;
|
||||
use Symfony\Component\Process\Process;
|
||||
use Mage\Task\AbstractTask;
|
||||
|
||||
/**
|
||||
* TarGz Task - Create temporal Tar
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class PrepareTask extends AbstractTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'deploy/targz/prepare';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return '[Deploy] Preparing TarGz file';
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
if (!$this->runtime->getEnvironmentConfig('releases', false)) {
|
||||
throw new ErrorException('This task is only available with releases enabled', 40);
|
||||
}
|
||||
|
||||
$tarGzLocal = $this->runtime->getTempFile();
|
||||
$this->runtime->setVar('targz_local', $tarGzLocal);
|
||||
|
||||
$excludes = $this->getExcludes();
|
||||
$cmdTarGz = sprintf('tar cfz %s %s ./', $tarGzLocal, $excludes);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runLocalCommand($cmdTarGz, 300);
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
|
||||
protected function getExcludes()
|
||||
{
|
||||
$excludes = $this->runtime->getEnvironmentConfig('exclude', []);
|
||||
$excludes = array_merge(['.git'], $excludes);
|
||||
|
||||
foreach ($excludes as &$exclude) {
|
||||
$exclude = '--exclude="' . $exclude . '"';
|
||||
}
|
||||
|
||||
return implode(' ', $excludes);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?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\FS;
|
||||
|
||||
use Mage\Task\Exception\ErrorException;
|
||||
use Mage\Task\AbstractTask;
|
||||
|
||||
/**
|
||||
* File System Task - Abstract Base class for File Operations
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
abstract class AbstractFileTask extends AbstractTask
|
||||
{
|
||||
/**
|
||||
* Returns the Task options
|
||||
*
|
||||
* @return array
|
||||
* @throws ErrorException
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
$mandatory = $this->getParameters();
|
||||
|
||||
foreach ($mandatory as $parameter) {
|
||||
if (!array_key_exists($parameter, $this->options)) {
|
||||
throw new ErrorException(sprintf('Parameter "%s" is not defined', $parameter));
|
||||
}
|
||||
}
|
||||
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the mandatory parameters
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract protected function getParameters();
|
||||
|
||||
/**
|
||||
* Returns a file with the placeholders replaced
|
||||
*
|
||||
* @param string $file
|
||||
* @return string
|
||||
* @throws ErrorException
|
||||
*/
|
||||
protected function getFile($file)
|
||||
{
|
||||
$mapping = [
|
||||
'%environment%' => $this->runtime->getEnvironment(),
|
||||
];
|
||||
|
||||
if ($this->runtime->getWorkingHost()) {
|
||||
$mapping['%host%'] = $this->runtime->getWorkingHost();
|
||||
}
|
||||
|
||||
if ($this->runtime->getReleaseId()) {
|
||||
$mapping['%release%'] = $this->runtime->getReleaseId();
|
||||
}
|
||||
|
||||
$options = $this->getOptions();
|
||||
return str_replace(
|
||||
array_keys($mapping),
|
||||
array_values($mapping),
|
||||
$options[$file]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?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\FS;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* File System Task - Copy a File
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class CopyTask extends AbstractFileTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'fs/copy';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
try {
|
||||
return sprintf('[FS] Copy "%s" to "%s"', $this->getFile('from'), $this->getFile('to'));
|
||||
} catch (Exception $exception) {
|
||||
return '[FS] Copy [missing parameters]';
|
||||
}
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$copyFrom = $this->getFile('from');
|
||||
$copyTo = $this->getFile('to');
|
||||
|
||||
$cmd = sprintf('cp -p %s %s', $copyFrom, $copyTo);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runCommand($cmd);
|
||||
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
|
||||
protected function getParameters()
|
||||
{
|
||||
return ['from', 'to'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?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\FS;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* File System Task - Symlink a File
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class LinkTask extends AbstractFileTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'fs/link';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
try {
|
||||
return sprintf('[FS] Link "%s" to "%s"', $this->getFile('from'), $this->getFile('to'));
|
||||
} catch (Exception $exception) {
|
||||
return '[FS] Link [missing parameters]';
|
||||
}
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$linkFrom = $this->getFile('from');
|
||||
$linkTo = $this->getFile('to');
|
||||
|
||||
$cmd = sprintf('ln -snf %s %s', $linkFrom, $linkTo);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runCommand($cmd);
|
||||
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
|
||||
protected function getParameters()
|
||||
{
|
||||
return ['from', 'to'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?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\FS;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* File System Task - Move a File
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class MoveTask extends AbstractFileTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'fs/move';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
try {
|
||||
return sprintf('[FS] Move "%s" to "%s"', $this->getFile('from'), $this->getFile('to'));
|
||||
} catch (Exception $exception) {
|
||||
return '[FS] Move [missing parameters]';
|
||||
}
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$moveFrom = $this->getFile('from');
|
||||
$moveTo = $this->getFile('to');
|
||||
|
||||
$cmd = sprintf('mv %s %s', $moveFrom, $moveTo);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runCommand($cmd);
|
||||
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
|
||||
protected function getParameters()
|
||||
{
|
||||
return ['from', 'to'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?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\FS;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* File System Task - Remove a File
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class RemoveTask extends AbstractFileTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'fs/remove';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
try {
|
||||
return sprintf('[FS] Remove "%s"', $this->getFile('file'));
|
||||
} catch (Exception $exception) {
|
||||
return '[FS] Remove [missing parameters]';
|
||||
}
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$file = $this->getFile('file');
|
||||
|
||||
$cmd = sprintf('rm %s', $file);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runCommand($cmd);
|
||||
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
|
||||
protected function getParameters()
|
||||
{
|
||||
return ['file'];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?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\Git;
|
||||
|
||||
use Mage\Task\Exception\SkipException;
|
||||
use Symfony\Component\Process\Process;
|
||||
use Mage\Task\AbstractTask;
|
||||
|
||||
/**
|
||||
* Git Task - Checkout Branch
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class ChangeBranchTask extends AbstractTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'git/change-branch';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
$options = $this->getOptions();
|
||||
$branch = $options['branch'];
|
||||
|
||||
if ($this->runtime->getVar('git_revert_branch', false)) {
|
||||
$branch = $this->runtime->getVar('git_revert_branch');
|
||||
}
|
||||
|
||||
return sprintf('[Git] Change Branch (%s)', $branch);
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$options = $this->getOptions();
|
||||
$branch = $this->runtime->getVar('git_revert_branch', false);
|
||||
|
||||
if ($branch === false) {
|
||||
$cmdGetCurrent = sprintf('%s branch | grep "*"', $options['path']);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runLocalCommand($cmdGetCurrent);
|
||||
if (!$process->isSuccessful()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$currentBranch = str_replace('* ', '', trim($process->getOutput()));
|
||||
if ($currentBranch == $options['branch']) {
|
||||
throw new SkipException();
|
||||
}
|
||||
|
||||
$branch = $options['branch'];
|
||||
$this->runtime->setVar('git_revert_branch', $currentBranch);
|
||||
}
|
||||
|
||||
$cmdChange = sprintf('%s checkout %s', $options['path'], $branch);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runLocalCommand($cmdChange);
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
|
||||
protected function getOptions()
|
||||
{
|
||||
$config = $this->runtime->getEnvironmentConfig();
|
||||
$branch = 'master';
|
||||
if (array_key_exists('branch', $config)) {
|
||||
$branch = $config['branch'];
|
||||
}
|
||||
|
||||
$options = array_merge(
|
||||
['path' => 'git', 'branch' => $branch],
|
||||
$this->options
|
||||
);
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?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\Git;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
use Mage\Task\AbstractTask;
|
||||
|
||||
/**
|
||||
* Git Task - Pull
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class UpdateTask extends AbstractTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'git/update';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return '[Git] Update';
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$options = $this->getOptions();
|
||||
$command = $options['path'] . ' pull';
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runLocalCommand($command);
|
||||
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
|
||||
protected function getOptions()
|
||||
{
|
||||
$config = $this->runtime->getEnvironmentConfig();
|
||||
$branch = 'master';
|
||||
if (array_key_exists('branch', $config)) {
|
||||
$branch = $config['branch'];
|
||||
}
|
||||
|
||||
$options = array_merge(
|
||||
['path' => 'git', 'branch' => $branch],
|
||||
$this->options
|
||||
);
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?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\Symfony;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
use Mage\Task\AbstractTask;
|
||||
|
||||
/**
|
||||
* Symfony Task - Dump Assetics
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class AsseticDumpTask extends AbstractTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'symfony/assetic-dump';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return '[Symfony] Assetic Dump';
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$options = $this->getOptions();
|
||||
$command = sprintf('%s assetic:dump --env=%s %s', $options['console'], $options['env'], $options['flags']);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runCommand(trim($command));
|
||||
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
|
||||
protected function getOptions()
|
||||
{
|
||||
$userGlobalOptions = $this->runtime->getConfigOptions('symfony', []);
|
||||
$userEnvOptions = $this->runtime->getEnvironmentConfig('symfony', []);
|
||||
$options = array_merge(
|
||||
['console' => 'bin/console', 'env' => 'dev', 'flags' => ''],
|
||||
(is_array($userGlobalOptions) ? $userGlobalOptions : []),
|
||||
(is_array($userEnvOptions) ? $userEnvOptions : []),
|
||||
$this->options
|
||||
);
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?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\Symfony;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
use Mage\Task\AbstractTask;
|
||||
|
||||
/**
|
||||
* Symfony Task - Install Assets
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class AssetsInstallTask extends AbstractTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'symfony/assets-install';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return '[Symfony] Assets Install';
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$options = $this->getOptions();
|
||||
$command = sprintf('%s assets:install %s --env=%s %s', $options['console'], $options['target'], $options['env'], $options['flags']);
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runCommand(trim($command));
|
||||
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
|
||||
protected function getOptions()
|
||||
{
|
||||
$userGlobalOptions = $this->runtime->getConfigOptions('symfony', []);
|
||||
$userEnvOptions = $this->runtime->getEnvironmentConfig('symfony', []);
|
||||
$options = array_merge(
|
||||
['console' => 'bin/console', 'env' => 'dev', 'target' => 'web', 'flags' => '--symlink --relative'],
|
||||
(is_array($userGlobalOptions) ? $userGlobalOptions : []),
|
||||
(is_array($userEnvOptions) ? $userEnvOptions : []),
|
||||
$this->options
|
||||
);
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?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\Symfony;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
use Mage\Task\AbstractTask;
|
||||
|
||||
/**
|
||||
* Symfony Task - Clear Cache
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class CacheClearTask extends AbstractTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'symfony/cache-clear';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return '[Symfony] Cache Clear';
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$options = $this->getOptions();
|
||||
$command = $options['console'] . ' cache:clear --env=' . $options['env'] . ' ' . $options['flags'];
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runCommand(trim($command));
|
||||
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
|
||||
protected function getOptions()
|
||||
{
|
||||
$userGlobalOptions = $this->runtime->getConfigOptions('symfony', []);
|
||||
$userEnvOptions = $this->runtime->getEnvironmentConfig('symfony', []);
|
||||
$options = array_merge(
|
||||
['console' => 'bin/console', 'env' => 'dev', 'flags' => ''],
|
||||
(is_array($userGlobalOptions) ? $userGlobalOptions : []),
|
||||
(is_array($userEnvOptions) ? $userEnvOptions : []),
|
||||
$this->options
|
||||
);
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
<?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\Symfony;
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
use Mage\Task\AbstractTask;
|
||||
|
||||
/**
|
||||
* Symfony Task - Cache Warmup
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class CacheWarmupTask extends AbstractTask
|
||||
{
|
||||
public function getName()
|
||||
{
|
||||
return 'symfony/cache-warmup';
|
||||
}
|
||||
|
||||
public function getDescription()
|
||||
{
|
||||
return '[Symfony] Cache Warmup';
|
||||
}
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$options = $this->getOptions();
|
||||
$command = $options['console'] . ' cache:warmup --env=' . $options['env'] . ' ' . $options['flags'];
|
||||
|
||||
/** @var Process $process */
|
||||
$process = $this->runtime->runCommand(trim($command));
|
||||
|
||||
return $process->isSuccessful();
|
||||
}
|
||||
|
||||
protected function getOptions()
|
||||
{
|
||||
$userGlobalOptions = $this->runtime->getConfigOptions('symfony', []);
|
||||
$userEnvOptions = $this->runtime->getEnvironmentConfig('symfony', []);
|
||||
$options = array_merge(
|
||||
['console' => 'bin/console', 'env' => 'dev', 'flags' => ''],
|
||||
(is_array($userGlobalOptions) ? $userGlobalOptions : []),
|
||||
(is_array($userEnvOptions) ? $userEnvOptions : []),
|
||||
$this->options
|
||||
);
|
||||
|
||||
return $options;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?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\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* The Task Failed, and it has a Custom Message
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class ErrorException extends Exception
|
||||
{
|
||||
public function getTrimmedMessage($maxLength = 60)
|
||||
{
|
||||
$message = $this->getMessage();
|
||||
|
||||
if (strlen($message) > $maxLength) {
|
||||
$message = substr($message, 0, $maxLength) . '...';
|
||||
}
|
||||
|
||||
return $message;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?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\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* The Task will be Skipped
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class SkipException extends Exception
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
* The Task will be Executed in a Rollback
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
interface ExecuteOnRollbackInterface
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
<?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;
|
||||
|
||||
use Mage\Runtime\Runtime;
|
||||
use Mage\Runtime\Exception\RuntimeException;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
use Symfony\Component\Finder\SplFileInfo;
|
||||
use ReflectionClass;
|
||||
|
||||
/**
|
||||
* Task Factory
|
||||
*
|
||||
* @author Andrés Montañez <andresmontanez@gmail.com>
|
||||
*/
|
||||
class TaskFactory
|
||||
{
|
||||
/**
|
||||
* @var Runtime
|
||||
*/
|
||||
protected $runtime;
|
||||
|
||||
/**
|
||||
* @var array Registered Tasks
|
||||
*/
|
||||
protected $registeredTasks = [];
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Runtime $runtime
|
||||
*/
|
||||
public function __construct(Runtime $runtime)
|
||||
{
|
||||
$this->runtime = $runtime;
|
||||
$this->loadBuiltInTasks();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a Task
|
||||
*
|
||||
* @param AbstractTask $task
|
||||
*/
|
||||
public function add(AbstractTask $task)
|
||||
{
|
||||
$task->setRuntime($this->runtime);
|
||||
$this->registeredTasks[$task->getName()] = $task;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a Task by it's registered Name/Code, or it can be a Class Name,
|
||||
* in that case the class will be instantiated
|
||||
*
|
||||
* @param string $name Name/Code or Class of the Task
|
||||
* @return AbstractTask
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function get($name)
|
||||
{
|
||||
$options = [];
|
||||
if (is_array($name)) {
|
||||
$options = $name;
|
||||
list($name) = array_keys($name);
|
||||
$options = $options[$name];
|
||||
}
|
||||
|
||||
if (array_key_exists($name, $this->registeredTasks)) {
|
||||
/** @var AbstractTask $task */
|
||||
$task = $this->registeredTasks[$name];
|
||||
$task->setOptions($options);
|
||||
return $task;
|
||||
} elseif (class_exists($name)) {
|
||||
$reflex = new ReflectionClass($name);
|
||||
if ($reflex->isInstantiable()) {
|
||||
$task = new $name();
|
||||
if ($task instanceof AbstractTask) {
|
||||
$task->setOptions($options);
|
||||
$this->add($task);
|
||||
return $task;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new RuntimeException(sprintf('Invalid task name "%s"', $name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Load BuiltIn Tasks
|
||||
*/
|
||||
protected function loadBuiltInTasks()
|
||||
{
|
||||
$finder = new Finder();
|
||||
$finder->files()->in(__DIR__ . '/BuiltIn')->name('*Task.php');
|
||||
|
||||
/** @var SplFileInfo $file */
|
||||
foreach ($finder as $file) {
|
||||
$class = substr('\\Mage\\Task\\BuiltIn\\' . str_replace('/', '\\', $file->getRelativePathname()), 0, -4);
|
||||
if (class_exists($class)) {
|
||||
$reflex = new ReflectionClass($class);
|
||||
if ($reflex->isInstantiable()) {
|
||||
$task = new $class();
|
||||
if ($task instanceof AbstractTask) {
|
||||
$this->add($task);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user