Skip to content

Commit

Permalink
Merge 6dda228 into 929e171
Browse files Browse the repository at this point in the history
  • Loading branch information
simonschaufi committed Jul 15, 2020
2 parents 929e171 + 6dda228 commit 7cbc0fc
Show file tree
Hide file tree
Showing 13 changed files with 923 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/Application/Laravel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
declare(strict_types=1);

namespace TYPO3\Surf\Application;

/*
* This file is part of TYPO3 Surf.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

use TYPO3\Surf\Domain\Model\Deployment;
use TYPO3\Surf\Domain\Model\Workflow;
use TYPO3\Surf\Task\Laravel\ClearAuthResetsTask;
use TYPO3\Surf\Task\Laravel\CreateDirectoriesTask;
use TYPO3\Surf\Task\Laravel\EnvAwareTask;
use TYPO3\Surf\Task\Laravel\FlushCachesTask;
use TYPO3\Surf\Task\Laravel\MigrateTask;
use TYPO3\Surf\Task\Laravel\StorageLinkTask;
use TYPO3\Surf\Task\Laravel\SymlinkStorageTask;
use TYPO3\Surf\Task\Laravel\WarmUpCachesTask;

class Laravel extends BaseApplication
{
/**
* Constructor
* @param string $name
*/
public function __construct($name = 'Laravel')
{
parent::__construct($name);
$this->options = array_merge($this->options, [
'webDirectory' => 'public',
'rsyncExcludes' => [
'.git',
'*.example',
'*.lock',
'.editorconfig',
'.gitattributes',
'.gitignore',
'.styleci.yml',
'/package.json',
'/package-lock.json',
'/phpunit.xml',
'/README.md',
'/storage',
'/tests',
'/server.php',
'/webpack.mix.js',
'{webDirectory}/storage',
]
]);
}

/**
* Register tasks for this application
*
* @param Workflow $workflow
* @param Deployment $deployment
* @return void
*/
public function registerTasks(Workflow $workflow, Deployment $deployment): void
{
parent::registerTasks($workflow, $deployment);

$workflow
->addTask(CreateDirectoriesTask::class, 'initialize', $this)
->afterStage('update', [
EnvAwareTask::class,
SymlinkStorageTask::class
], $this)
->afterStage('migrate', MigrateTask::class, $this)
->afterStage('finalize', [
FlushCachesTask::class,
WarmUpCachesTask::class
], $this)
->afterStage('switch', StorageLinkTask::class, $this)
->afterStage('cleanup', ClearAuthResetsTask::class, $this)
;
}
}
165 changes: 165 additions & 0 deletions src/Task/Laravel/AbstractCliTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php
declare(strict_types=1);

namespace TYPO3\Surf\Task\Laravel;

/*
* This file is part of TYPO3 Surf.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

use TYPO3\Flow\Utility\Files;
use TYPO3\Surf\Domain\Model\Application;
use TYPO3\Surf\Domain\Model\Deployment;
use TYPO3\Surf\Domain\Model\Node;
use TYPO3\Surf\Domain\Model\Task;
use TYPO3\Surf\Domain\Service\ShellCommandServiceAwareInterface;
use TYPO3\Surf\Domain\Service\ShellCommandServiceAwareTrait;

abstract class AbstractCliTask extends Task implements ShellCommandServiceAwareInterface
{
use ShellCommandServiceAwareTrait;

/**
* The working directory. Either local or remote, and probably in a special application root directory
*
* @var string
*/
protected $workingDirectory;

/**
* Localhost or deployment target node
*
* @var Node
*/
protected $targetNode;

/**
* Execute this task
*
* @param array $cliArguments
* @param Node $node
* @param Application $application
* @param Deployment $deployment
* @param array $options
* @return bool|mixed
* @throws \TYPO3\Surf\Exception\TaskExecutionException
*/
protected function executeCliCommand(
array $cliArguments,
Node $node,
Application $application,
Deployment $deployment,
array $options = []
) {
$this->determineWorkingDirectoryAndTargetNode($node, $application, $deployment, $options);
$phpBinaryPathAndFilename = $options['phpBinaryPathAndFilename'] ?? 'php';
$commandPrefix = $phpBinaryPathAndFilename . ' ';

return $this->shell->executeOrSimulate(
[
'cd ' . escapeshellarg($this->workingDirectory),
$commandPrefix . implode(' ', array_map('escapeshellarg', $cliArguments))
],
$this->targetNode,
$deployment
);
}

/**
* Simulate this task
*
* @param Node $node
* @param Application $application
* @param Deployment $deployment
* @param array $options
* @return void
*/
public function simulate(Node $node, Application $application, Deployment $deployment, array $options = [])
{
$this->execute($node, $application, $deployment, $options);
}

/**
* Determines the path to the working directory and the target node by given options
*
* @param Node $node
* @param Application $application
* @param Deployment $deployment
* @param array $options
*/
protected function determineWorkingDirectoryAndTargetNode(
Node $node,
Application $application,
Deployment $deployment,
array $options = []
) {
if (!isset($this->workingDirectory, $this->targetNode)) {
if (isset($options['useApplicationWorkspace']) && $options['useApplicationWorkspace'] === true) {
$this->workingDirectory = $deployment->getWorkspacePath($application);
$node = $deployment->getNode('localhost');
} else {
$this->workingDirectory = $deployment->getApplicationReleasePath($application);
}
$this->targetNode = $node;
}
}

/**
* Checks if a given directory exists.
*
* @param string $directory
* @param Node $node
* @param Application $application
* @param Deployment $deployment
* @param array $options
* @return bool
* @throws \TYPO3\Surf\Exception\TaskExecutionException
*/
protected function directoryExists(
$directory,
Node $node,
Application $application,
Deployment $deployment,
array $options = []
) {
$this->determineWorkingDirectoryAndTargetNode($node, $application, $deployment, $options);
$directory = Files::concatenatePaths([$this->workingDirectory, $directory]);
return $this->shell->executeOrSimulate(
'test -d ' . escapeshellarg($directory),
$this->targetNode,
$deployment,
true
) !== false;
}

/**
* Checks if a given file exists.
*
* @param string $pathAndFileName
* @param Node $node
* @param Application $application
* @param Deployment $deployment
* @param array $options
* @return bool
* @throws \TYPO3\Surf\Exception\TaskExecutionException
*/
protected function fileExists(
$pathAndFileName,
Node $node,
Application $application,
Deployment $deployment,
array $options = []
) {
$this->determineWorkingDirectoryAndTargetNode($node, $application, $deployment, $options);
$pathAndFileName = $this->workingDirectory . '/' . $pathAndFileName;
return $this->shell->executeOrSimulate(
'test -f ' . escapeshellarg($pathAndFileName),
$this->targetNode,
$deployment,
true
) !== false;
}
}
48 changes: 48 additions & 0 deletions src/Task/Laravel/ClearAuthResetsTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);

namespace TYPO3\Surf\Task\Laravel;

/*
* This file is part of TYPO3 Surf.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

use TYPO3\Surf\Application\Laravel;
use TYPO3\Surf\Domain\Model\Application;
use TYPO3\Surf\Domain\Model\Deployment;
use TYPO3\Surf\Domain\Model\Node;
use TYPO3\Surf\Exception\TaskExecutionException;
use Webmozart\Assert\Assert;

class ClearAuthResetsTask extends AbstractCliTask
{
/**
* Execute this task
*
* @param Node $node
* @param Application $application
* @param Deployment $deployment
* @param array $options
* @return void
* @throws TaskExecutionException
*/
public function execute(Node $node, Application $application, Deployment $deployment, array $options = [])
{
Assert::isInstanceOf(
$application,
Laravel::class,
sprintf('Laravel application needed for %s, got "%s"', get_class($this), get_class($application))
);

$this->executeCliCommand(
['artisan', 'auth:clear-resets'],
$node,
$application,
$deployment,
$options
);
}
}
43 changes: 43 additions & 0 deletions src/Task/Laravel/CreateDirectoriesTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);

namespace TYPO3\Surf\Task\Laravel;

/*
* This file is part of TYPO3 Surf.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

use TYPO3\Surf\Domain\Model\Application;
use TYPO3\Surf\Domain\Model\Deployment;
use TYPO3\Surf\Domain\Model\Node;
use TYPO3\Surf\Exception\InvalidConfigurationException;

class CreateDirectoriesTask extends \TYPO3\Surf\Task\Generic\CreateDirectoriesTask
{
/**
* Execute this task
*
* @param Node $node
* @param Application $application
* @param Deployment $deployment
* @param array $options
* @throws InvalidConfigurationException
*/
public function execute(Node $node, Application $application, Deployment $deployment, array $options = [])
{
$options = [
'directories' => [
'shared/storage/app/public',
'shared/storage/framework/cache/data',
'shared/storage/framework/sessions',
'shared/storage/framework/testing',
'shared/storage/framework/views',
],
'baseDirectory' => $application->getDeploymentPath()
];
parent::execute($node, $application, $deployment, $options);
}
}
53 changes: 53 additions & 0 deletions src/Task/Laravel/EnvAwareTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);

namespace TYPO3\Surf\Task\Laravel;

/*
* This file is part of TYPO3 Surf.
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

use TYPO3\Surf\Domain\Model\Application;
use TYPO3\Surf\Domain\Model\Deployment;
use TYPO3\Surf\Domain\Model\Node;
use TYPO3\Surf\Domain\Model\Task;
use TYPO3\Surf\Domain\Service\ShellCommandServiceAwareInterface;
use TYPO3\Surf\Domain\Service\ShellCommandServiceAwareTrait;
use TYPO3\Surf\Exception\TaskExecutionException;

class EnvAwareTask extends Task implements ShellCommandServiceAwareInterface
{
use ShellCommandServiceAwareTrait;

/**
* Execute this task
*
* @param Node $node
* @param Application $application
* @param Deployment $deployment
* @param array $options
*
* @throws TaskExecutionException
*/
public function execute(Node $node, Application $application, Deployment $deployment, array $options = [])
{
$sharedPath = $application->getSharedPath();
$releasePath = $deployment->getApplicationReleasePath($application);

$result = $this->shell->execute(sprintf('test -f %s/.env', $sharedPath), $node, $deployment, true);
if ($result === false) {
throw new TaskExecutionException('.env file in "' . $sharedPath . '" does not exist on node ' . $node->getName(), 1582080037);
}

$command = sprintf(
'cp %s %s',
escapeshellarg($sharedPath . '/.env'),
escapeshellarg($releasePath . '/.env')
);

$this->shell->executeOrSimulate($command, $node, $deployment);
}
}

0 comments on commit 7cbc0fc

Please sign in to comment.