From b1b9643eb25ba1990015ab6ebd1954ed64b44c6f Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Fri, 26 May 2017 08:57:28 +0200 Subject: [PATCH] Add travis tests --- .php_cs | 40 +++++------- .travis.yml | 42 +++++++++++++ src/Command/DeployCommand.php | 4 +- src/Command/RollbackCommand.php | 2 +- src/Configuration/AbstractConfiguration.php | 2 +- src/Configuration/ConfigurationAdapter.php | 4 +- src/Configuration/CustomConfiguration.php | 6 +- src/Configuration/DefaultConfiguration.php | 68 ++++++++++----------- src/Context.php | 18 +++--- src/Deployer/AbstractDeployer.php | 26 ++++---- src/Deployer/CustomDeployer.php | 4 +- src/Deployer/DefaultDeployer.php | 58 +++++++++--------- src/Helper/Str.php | 14 ++--- src/Logger.php | 10 +-- src/Requirement/AbstractRequirement.php | 6 +- src/Requirement/AllowsLoginViaSsh.php | 4 +- src/Requirement/CommandExists.php | 6 +- src/Server/Server.php | 22 +++---- src/Server/ServerRepository.php | 8 +-- src/Task/Task.php | 10 +-- src/Task/TaskCompleted.php | 10 +-- src/Task/TaskRunner.php | 4 +- 22 files changed, 200 insertions(+), 168 deletions(-) create mode 100644 .travis.yml diff --git a/.php_cs b/.php_cs index e24c6a1..a13d02c 100644 --- a/.php_cs +++ b/.php_cs @@ -1,39 +1,29 @@ - -For the full copyright and license information, please view the LICENSE -file that was distributed with this source code. -COMMENT; +$finder = PhpCsFixer\Finder::create() + ->in(__DIR__) + ->ignoreDotFiles(true) + ->ignoreVCS(true) + ->exclude(array('doc', 'vendor')) + ->files() + ->name('*.php') +; return PhpCsFixer\Config::create() + ->setUsingCache(true) ->setRiskyAllowed(true) + ->setFinder($finder) ->setRules([ '@Symfony' => true, - '@Symfony:risky' => true, 'array_syntax' => ['syntax' => 'short'], - 'header_comment' => ['header' => $fileHeaderComment, 'separate' => 'both'], - 'linebreak_after_opening_tag' => true, - 'list_syntax' => ['syntax' => 'short'], - 'mb_str_functions' => true, - 'no_php4_constructor' => true, - 'no_unreachable_default_argument_value' => true, + 'binary_operator_spaces' => array( + 'align_double_arrow' => false, + ), + 'combine_consecutive_unsets' => true, 'no_useless_else' => true, 'no_useless_return' => true, - 'ordered_class_elements' => true, 'ordered_imports' => true, - 'php_unit_strict' => true, - 'phpdoc_order' => true, - 'pow_to_exponentiation' => true, - 'random_api_migration' => true, - 'return_type_declaration' => ['space_before' => 'one'], - 'semicolon_after_instruction' => true, + 'phpdoc_summary' => false, 'strict_comparison' => true, - 'strict_param' => true, - 'ternary_to_null_coalescing' => true, ]) - ->setCacheFile(__DIR__.'/.php_cs.cache') ; diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..ad6cc0f --- /dev/null +++ b/.travis.yml @@ -0,0 +1,42 @@ +language: php + +sudo: false + +git: + depth: 1 + +cache: + directories: + - $HOME/.composer/cache/files + +matrix: + fast_finish: true + include: + - php: nightly + env: + - SYMFONY_VERSION="dev-master" + - CHECK_PHP_SYNTAX="no" + - php: 7.1 + env: + - SYMFONY_VERSION="3.2.*" + - CHECK_PHP_SYNTAX="yes" + allow_failures: + - php: nightly + +before_install: + - stty cols 120 + - INI_FILE=~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini + - echo memory_limit = -1 >> $INI_FILE + - echo session.gc_probability = 0 >> $INI_FILE + - echo opcache.enable_cli = 1 >> $INI_FILE + - rm -f ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini + - composer self-update + - if [[ "$SYMFONY_VERSION" != "" ]]; then composer require "symfony/symfony:${SYMFONY_VERSION}" --no-update; fi; + +install: + - if [[ "$CHECK_PHP_SYNTAX" == "yes" ]]; then composer require --dev --no-update friendsofphp/php-cs-fixer; fi; + - composer update --prefer-dist --no-interaction --no-suggest --no-progress --ansi + +script: + - vendor/bin/phpunit + - if [[ "$CHECK_PHP_SYNTAX" == "yes" ]]; then php vendor/bin/php-cs-fixer --no-interaction --dry-run --diff -v fix; fi; diff --git a/src/Command/DeployCommand.php b/src/Command/DeployCommand.php index f39c893..823cf27 100644 --- a/src/Command/DeployCommand.php +++ b/src/Command/DeployCommand.php @@ -80,7 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $deployer->doDeploy(); } - private function getDefaultConfigPath(string $stageName) : string + private function getDefaultConfigPath(string $stageName): string { $symfonyVersion = Kernel::MAJOR_VERSION; $defaultConfigPaths = [ @@ -96,7 +96,7 @@ private function getDefaultConfigPath(string $stageName) : string return $defaultConfigPaths[$symfonyVersion]; } - private function createDefaultConfigFile(InputInterface $input, OutputInterface $output, string $defaultConfigPath, string $stageName) : void + private function createDefaultConfigFile(InputInterface $input, OutputInterface $output, string $defaultConfigPath, string $stageName): void { $helper = $this->getHelper('question'); $question = new ConfirmationQuestion(sprintf("\n WARNING There is no config file to deploy '%s' stage.\nDo you want to create a minimal config file for it? [Y/n] ", $stageName), true); diff --git a/src/Command/RollbackCommand.php b/src/Command/RollbackCommand.php index aad8181..d846395 100644 --- a/src/Command/RollbackCommand.php +++ b/src/Command/RollbackCommand.php @@ -75,7 +75,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $deployer->doRollback(); } - private function getDefaultConfigPath(string $stageName) : string + private function getDefaultConfigPath(string $stageName): string { $symfonyVersion = Kernel::MAJOR_VERSION; $defaultConfigPaths = [ diff --git a/src/Configuration/AbstractConfiguration.php b/src/Configuration/AbstractConfiguration.php index 1630f0e..4ac0e20 100644 --- a/src/Configuration/AbstractConfiguration.php +++ b/src/Configuration/AbstractConfiguration.php @@ -47,5 +47,5 @@ public function useSshAgentForwarding(bool $useIt) $this->useSshAgentForwarding = $useIt; } - abstract protected function getReservedServerProperties() : array; + abstract protected function getReservedServerProperties(): array; } diff --git a/src/Configuration/ConfigurationAdapter.php b/src/Configuration/ConfigurationAdapter.php index 78a036f..c977f80 100644 --- a/src/Configuration/ConfigurationAdapter.php +++ b/src/Configuration/ConfigurationAdapter.php @@ -30,7 +30,7 @@ public function __construct(AbstractConfiguration $config) $this->config = $config; } - public function __toString() : string + public function __toString(): string { return Str::formatAsTable($this->getOptions()->all()); } @@ -44,7 +44,7 @@ public function get(string $optionName) return $this->getOptions()->get($optionName); } - private function getOptions() : ParameterBag + private function getOptions(): ParameterBag { if (null !== $this->options) { return $this->options; diff --git a/src/Configuration/CustomConfiguration.php b/src/Configuration/CustomConfiguration.php index a6f5e5c..02d2f8b 100644 --- a/src/Configuration/CustomConfiguration.php +++ b/src/Configuration/CustomConfiguration.php @@ -21,7 +21,7 @@ class CustomConfiguration extends AbstractConfiguration { // this proxy method is needed because the autocompletion breaks // if the parent method is used directly - public function server(string $sshDsn, array $roles = [Server::ROLE_APP], array $properties = []) : self + public function server(string $sshDsn, array $roles = [Server::ROLE_APP], array $properties = []): self { parent::server($sshDsn, $roles, $properties); @@ -30,14 +30,14 @@ public function server(string $sshDsn, array $roles = [Server::ROLE_APP], array // this proxy method is needed because the autocompletion breaks // if the parent method is used directly - public function useSshAgentForwarding(bool $useIt) : self + public function useSshAgentForwarding(bool $useIt): self { parent::useSshAgentForwarding($useIt); return $this; } - protected function getReservedServerProperties() : array + protected function getReservedServerProperties(): array { return []; } diff --git a/src/Configuration/DefaultConfiguration.php b/src/Configuration/DefaultConfiguration.php index 6f84b96..5640676 100644 --- a/src/Configuration/DefaultConfiguration.php +++ b/src/Configuration/DefaultConfiguration.php @@ -66,7 +66,7 @@ public function __construct(string $localProjectDir) // this proxy method is needed because the autocompletion breaks // if the parent method is used directly - public function server(string $sshDsn, array $roles = [Server::ROLE_APP], array $properties = []) : self + public function server(string $sshDsn, array $roles = [Server::ROLE_APP], array $properties = []): self { parent::server($sshDsn, $roles, $properties); @@ -75,28 +75,28 @@ public function server(string $sshDsn, array $roles = [Server::ROLE_APP], array // this proxy method is needed because the autocompletion breaks // if the parent method is used directly - public function useSshAgentForwarding(bool $useIt) : self + public function useSshAgentForwarding(bool $useIt): self { parent::useSshAgentForwarding($useIt); return $this; } - public function symfonyEnvironment(string $name) : self + public function symfonyEnvironment(string $name): self { $this->symfonyEnvironment = $name; return $this; } - public function keepReleases(int $numReleases) : self + public function keepReleases(int $numReleases): self { $this->keepReleases = $numReleases; return $this; } - public function repositoryUrl(string $url) : self + public function repositoryUrl(string $url): self { // SSH agent forwarding only works when using SSH URLs, not https URLs. Check these URLs: // https://github.com// @@ -116,63 +116,63 @@ public function repositoryUrl(string $url) : self return $this; } - public function repositoryBranch(string $branchName) : self + public function repositoryBranch(string $branchName): self { $this->repositoryBranch = $branchName; return $this; } - public function updateRemoteComposerBinary(bool $updateBeforeInstall) : self + public function updateRemoteComposerBinary(bool $updateBeforeInstall): self { $this->updateRemoteComposerBinary = $updateBeforeInstall; return $this; } - public function remoteComposerBinaryPath(string $path) : self + public function remoteComposerBinaryPath(string $path): self { $this->remoteComposerBinaryPath = $path; return $this; } - public function composerInstallFlags(string $flags) : self + public function composerInstallFlags(string $flags): self { $this->composerInstallFlags = $flags; return $this; } - public function composerOptimizeFlags(string $flags) : self + public function composerOptimizeFlags(string $flags): self { $this->composerOptimizeFlags = $flags; return $this; } - public function installWebAssets(bool $install) : self + public function installWebAssets(bool $install): self { $this->installWebAssets = $install; return $this; } - public function dumpAsseticAssets(bool $dump) : self + public function dumpAsseticAssets(bool $dump): self { $this->dumpAsseticAssets = $dump; return $this; } - public function warmupCache(bool $warmUp) : self + public function warmupCache(bool $warmUp): self { $this->warmupCache = $warmUp; return $this; } - public function consoleBinaryPath(string $path) : self + public function consoleBinaryPath(string $path): self { $this->consoleBinaryPath = $path; @@ -180,7 +180,7 @@ public function consoleBinaryPath(string $path) : self } // Relative to the project root directory - public function binDir(string $path) : self + public function binDir(string $path): self { $this->validatePathIsRelativeToProject($path, __METHOD__); $this->binDir = rtrim($path, '/'); @@ -189,7 +189,7 @@ public function binDir(string $path) : self } // Relative to the project root directory - public function configDir(string $path) : self + public function configDir(string $path): self { $this->validatePathIsRelativeToProject($path, __METHOD__); $this->configDir = rtrim($path, '/'); @@ -198,7 +198,7 @@ public function configDir(string $path) : self } // Relative to the project root directory - public function cacheDir(string $path) : self + public function cacheDir(string $path): self { $this->validatePathIsRelativeToProject($path, __METHOD__); $this->cacheDir = rtrim($path, '/'); @@ -206,7 +206,7 @@ public function cacheDir(string $path) : self return $this; } - public function deployDir(string $path) : self + public function deployDir(string $path): self { $this->deployDir = rtrim($path, '/'); @@ -214,7 +214,7 @@ public function deployDir(string $path) : self } // Relative to the project root directory - public function logDir(string $path) : self + public function logDir(string $path): self { $this->validatePathIsRelativeToProject($path, __METHOD__); $this->logDir = rtrim($path, '/'); @@ -223,7 +223,7 @@ public function logDir(string $path) : self } // Relative to the project root directory - public function srcDir(string $path) : self + public function srcDir(string $path): self { $this->validatePathIsRelativeToProject($path, __METHOD__); $this->srcDir = rtrim($path, '/'); @@ -232,7 +232,7 @@ public function srcDir(string $path) : self } // Relative to the project root directory - public function templatesDir(string $path) : self + public function templatesDir(string $path): self { $this->validatePathIsRelativeToProject($path, __METHOD__); $this->templatesDir = rtrim($path, '/'); @@ -241,7 +241,7 @@ public function templatesDir(string $path) : self } // Relative to the project root directory - public function webDir(string $path) : self + public function webDir(string $path): self { $this->validatePathIsRelativeToProject($path, __METHOD__); $this->webDir = rtrim($path, '/'); @@ -251,7 +251,7 @@ public function webDir(string $path) : self // Relative to the project root directory // the $paths can be glob() patterns, so this method needs to resolve them - public function controllersToRemove(array $paths) : self + public function controllersToRemove(array $paths): self { $absoluteGlobPaths = array_map(function ($globPath) { return $this->localProjectDir.DIRECTORY_SEPARATOR.$globPath; @@ -275,7 +275,7 @@ public function controllersToRemove(array $paths) : self } // Relative to the project root directory - public function writableDirs(array $paths) : self + public function writableDirs(array $paths): self { foreach ($paths as $path) { $this->validatePathIsRelativeToProject($path, __METHOD__); @@ -285,32 +285,32 @@ public function writableDirs(array $paths) : self return $this; } - public function fixPermissionsWithChmod(string $mode = '0777') : self + public function fixPermissionsWithChmod(string $mode = '0777'): self { $this->permissionMethod = 'chmod'; $this->permissionMode = $mode; } - public function fixPermissionsWithChown(string $webServerUser) : self + public function fixPermissionsWithChown(string $webServerUser): self { $this->permissionMethod = 'chown'; $this->permissionUser = $webServerUser; } - public function fixPermissionsWithChgrp(string $webServerGroup) : self + public function fixPermissionsWithChgrp(string $webServerGroup): self { $this->permissionMethod = 'chgrp'; $this->permissionGroup = $webServerGroup; } - public function fixPermissionsWithAcl(string $webServerUser) : self + public function fixPermissionsWithAcl(string $webServerUser): self { $this->permissionMethod = 'acl'; $this->permissionUser = $webServerUser; } // Relative to the project root directory - public function sharedFilesAndDirs(array $paths) : self + public function sharedFilesAndDirs(array $paths): self { $this->sharedDirs = []; $this->sharedFiles = []; @@ -329,7 +329,7 @@ public function sharedFilesAndDirs(array $paths) : self // the $homepageUrl (e.g. 'https://symfony.com') is needed because OPcache contents can't // be deleted from the terminal and deployer must make a HTTP request to a real website URL - public function resetOpCacheFor(string $homepageUrl) : self + public function resetOpCacheFor(string $homepageUrl): self { if (!Str::startsWith($homepageUrl, 'http')) { throw new InvalidConfigurationException(sprintf('The value of %s option must be the valid URL of your homepage (it must start with http:// or https://).', Option::resetOpCacheFor)); @@ -340,12 +340,12 @@ public function resetOpCacheFor(string $homepageUrl) : self return $this; } - protected function getReservedServerProperties() : array + protected function getReservedServerProperties(): array { return [Property::bin_dir, Property::config_dir, Property::console_bin, Property::cache_dir, Property::deploy_dir, Property::log_dir, Property::src_dir, Property::templates_dir, Property::web_dir]; } - private function setDefaultConfiguration(int $symfonyVersion) : void + private function setDefaultConfiguration(int $symfonyVersion): void { if (2 === $symfonyVersion) { $this->setDirs('app', 'app/config', 'app/cache', 'app/logs', 'src', 'app/Resources/views', 'web'); @@ -368,7 +368,7 @@ private function setDefaultConfiguration(int $symfonyVersion) : void } } - private function setDirs(string $binDir, string $configDir, string $cacheDir, string $logDir, string $srcDir, string $templatesDir, string $webDir) : void + private function setDirs(string $binDir, string $configDir, string $cacheDir, string $logDir, string $srcDir, string $templatesDir, string $webDir): void { $this->binDir = $binDir; $this->configDir = $configDir; @@ -379,7 +379,7 @@ private function setDirs(string $binDir, string $configDir, string $cacheDir, st $this->webDir = $webDir; } - private function validatePathIsRelativeToProject($path, $methodName) : void + private function validatePathIsRelativeToProject($path, $methodName): void { if (!is_readable($this->localProjectDir.DIRECTORY_SEPARATOR.$path)) { throw new InvalidConfigurationException(sprintf('The "%s" value given in %s() is not relative to the project root directory or is not readable.', $path, $methodName)); diff --git a/src/Context.php b/src/Context.php index f073600..28cd5b4 100644 --- a/src/Context.php +++ b/src/Context.php @@ -42,7 +42,7 @@ public function __construct(InputInterface $input, OutputInterface $output, stri $this->localHost = $this->createLocalHost(); } - public function __toString() : string + public function __toString(): string { return sprintf( 'dry-run = %s, debug = %s, logFile = %s, localHost = Object(Server), input = Object(InputInterface), output = Object(OutputInterface)', @@ -52,42 +52,42 @@ public function __toString() : string ); } - public function getLocalHost() : Server + public function getLocalHost(): Server { return $this->localHost; } - public function getLogFilePath() : string + public function getLogFilePath(): string { return $this->logFilePath; } - public function getLocalProjectRootDir() : string + public function getLocalProjectRootDir(): string { return $this->localHost->get(Property::project_dir); } - public function isDryRun() : bool + public function isDryRun(): bool { return $this->dryRun; } - public function isDebug() : bool + public function isDebug(): bool { return $this->debug; } - public function getInput() : InputInterface + public function getInput(): InputInterface { return $this->input; } - public function getOutput() : OutputInterface + public function getOutput(): OutputInterface { return $this->output; } - private function createLocalHost() : Server + private function createLocalHost(): Server { $localhost = new Server('localhost'); $localhost->set(Property::project_dir, $this->projectDir); diff --git a/src/Deployer/AbstractDeployer.php b/src/Deployer/AbstractDeployer.php index e958566..ed6c00c 100644 --- a/src/Deployer/AbstractDeployer.php +++ b/src/Deployer/AbstractDeployer.php @@ -35,7 +35,7 @@ abstract class AbstractDeployer /** @var ConfigurationAdapter */ private $config; - abstract public function getRequirements() : array; + abstract public function getRequirements(): array; abstract public function deploy(); @@ -48,7 +48,7 @@ final public function getConfig(string $name) return $this->config->get($name); } - final public function doDeploy() : void + final public function doDeploy(): void { try { $this->log('Executing beforeStartingDeploy hook'); @@ -74,7 +74,7 @@ final public function doDeploy() : void $this->log(sprintf('[OK] Deployment was successful')); } - final public function doRollback() : void + final public function doRollback(): void { try { $this->log('Executing beforeStartingRollback hook'); @@ -129,7 +129,7 @@ public function beforeFinishingRollback() $this->log('

Nothing to execute'); } - public function initialize(Context $context) : void + public function initialize(Context $context): void { $this->context = $context; $this->logger = new Logger($context); @@ -147,22 +147,22 @@ abstract protected function getConfigBuilder(); abstract protected function configure(); - final protected function getContext() : Context + final protected function getContext(): Context { return $this->context; } - final protected function getServers() : ServerRepository + final protected function getServers(): ServerRepository { return $this->config->get('servers'); } - final protected function log(string $message) : void + final protected function log(string $message): void { $this->logger->log($message); } - final protected function runLocal(string $command) : TaskCompleted + final protected function runLocal(string $command): TaskCompleted { $task = new Task([$this->getContext()->getLocalHost()], $command, $this->getCommandEnvVars()); @@ -172,14 +172,14 @@ final protected function runLocal(string $command) : TaskCompleted /** * @return TaskCompleted[] */ - final protected function runRemote(string $command, array $roles = [Server::ROLE_APP]) : array + final protected function runRemote(string $command, array $roles = [Server::ROLE_APP]): array { $task = new Task($this->getServers()->findByRoles($roles), $command, $this->getCommandEnvVars()); return $this->taskRunner->run($task); } - final protected function runOnServer(string $command, Server $server) : TaskCompleted + final protected function runOnServer(string $command, Server $server): TaskCompleted { $task = new Task([$server], $command, $this->getCommandEnvVars()); @@ -189,7 +189,7 @@ final protected function runOnServer(string $command, Server $server) : TaskComp // this method checks that any file or directory that goes into "rm -rf" command is // relative to the project dir. This safeguard will prevent catastrophic errors // related to removing the wrong file or directory on the server. - final protected function safeDelete(Server $server, array $absolutePaths) : void + final protected function safeDelete(Server $server, array $absolutePaths): void { $deployDir = $server->get(Property::deploy_dir); $pathsToDelete = []; @@ -208,7 +208,7 @@ final protected function safeDelete(Server $server, array $absolutePaths) : void $this->runOnServer(sprintf('rm -rf %s', implode(' ', $pathsToDelete)), $server); } - private function getCommandEnvVars() : array + private function getCommandEnvVars(): array { $symfonyEnvironment = $this->getConfig(Option::symfonyEnvironment); $envVars = null !== $symfonyEnvironment ? ['SYMFONY_ENV' => $symfonyEnvironment] : []; @@ -216,7 +216,7 @@ private function getCommandEnvVars() : array return $envVars; } - private function checkRequirements() : void + private function checkRequirements(): void { /** @var AbstractRequirement[] $requirements */ $requirements = $this->getRequirements(); diff --git a/src/Deployer/CustomDeployer.php b/src/Deployer/CustomDeployer.php index 6aef610..c185800 100644 --- a/src/Deployer/CustomDeployer.php +++ b/src/Deployer/CustomDeployer.php @@ -22,12 +22,12 @@ */ abstract class CustomDeployer extends AbstractDeployer { - public function getConfigBuilder() : CustomConfiguration + public function getConfigBuilder(): CustomConfiguration { return new CustomConfiguration(); } - public function getRequirements() : array + public function getRequirements(): array { return [ new CommandExists([$this->getContext()->getLocalHost()], 'ssh'), diff --git a/src/Deployer/DefaultDeployer.php b/src/Deployer/DefaultDeployer.php index 5f71666..8eff126 100644 --- a/src/Deployer/DefaultDeployer.php +++ b/src/Deployer/DefaultDeployer.php @@ -25,12 +25,12 @@ abstract class DefaultDeployer extends AbstractDeployer private $remoteProjectDirHasBeenCreated = false; private $remoteSymLinkHasBeenCreated = false; - public function getConfigBuilder() : DefaultConfiguration + public function getConfigBuilder(): DefaultConfiguration { return new DefaultConfiguration($this->getContext()->getLocalProjectRootDir()); } - public function getRequirements() : array + public function getRequirements(): array { $requirements = []; $localhost = $this->getContext()->getLocalHost(); @@ -49,7 +49,7 @@ public function getRequirements() : array return $requirements; } - final public function deploy() : void + final public function deploy(): void { $this->initializeServerOptions(); $this->createRemoteDirectoryLayout(); @@ -88,7 +88,7 @@ final public function deploy() : void $this->doKeepReleases(); } - final public function cancelDeploy() : void + final public function cancelDeploy(): void { if (!$this->remoteSymLinkHasBeenCreated && !$this->remoteProjectDirHasBeenCreated) { $this->log('

No changes need to be reverted on remote servers (neither the remote project dir nor the symlink were created)'); @@ -103,7 +103,7 @@ final public function cancelDeploy() : void } } - final public function rollback() : void + final public function rollback(): void { $this->initializeServerOptions(); @@ -140,7 +140,7 @@ public function beforeRollingBack() $this->log('

Nothing to execute'); } - private function doCheckPreviousReleases() : void + private function doCheckPreviousReleases(): void { $this->log('

Getting the previous releases dirs'); $results = $this->runRemote('ls -r1 {{ deploy_dir }}/releases'); @@ -158,20 +158,20 @@ private function doCheckPreviousReleases() : void } } - private function doSymlinkToPreviousRelease() : void + private function doSymlinkToPreviousRelease(): void { $this->log('

Reverting the current symlink to the previous version'); $this->runRemote('export _previous_release_dirname=$(ls -r1 {{ deploy_dir }}/releases | head -n 2 | tail -n 1) && rm -f {{ deploy_dir }}/current && ln -s {{ deploy_dir }}/releases/$_previous_release_dirname {{ deploy_dir }}/current'); } - private function doDeleteLastReleaseDirectory() : void + private function doDeleteLastReleaseDirectory(): void { // this is needed to avoid rolling back in the future to this version $this->log('

Deleting the last release directory'); $this->runRemote('export _last_release_dirname=$(ls -r1 {{ deploy_dir }}/releases | head -n 1) && rm -fr {{ deploy_dir }}/releases/$_last_release_dirname'); } - private function initializeServerOptions() : void + private function initializeServerOptions(): void { $this->log('

Initializing server options'); @@ -191,7 +191,7 @@ private function initializeServerOptions() : void } } - private function initializeDirectoryLayout(Server $server) : void + private function initializeDirectoryLayout(Server $server): void { $this->log('

Initializing server directory layout'); @@ -211,7 +211,7 @@ private function initializeDirectoryLayout(Server $server) : void // this is needed because it's common for Smyfony projects to use binary directories // different from their Symfony version. For example: Symfony 2 projects that upgrade // to Symfony 3 but still use app/console instead of bin/console - private function findConsoleBinaryPath(Server $server) : string + private function findConsoleBinaryPath(Server $server): string { $symfonyConsoleBinaries = ['{{ project_dir }}/app/console', '{{ project_dir }}/bin/console']; foreach ($symfonyConsoleBinaries as $consoleBinary) { @@ -226,7 +226,7 @@ private function findConsoleBinaryPath(Server $server) : string } } - private function createRemoteDirectoryLayout() : void + private function createRemoteDirectoryLayout(): void { $this->log('

Creating the remote directory layout'); $this->runRemote('mkdir -p {{ deploy_dir }} && mkdir -p {{ deploy_dir }}/releases && mkdir -p {{ deploy_dir }}/shared'); @@ -240,7 +240,7 @@ private function createRemoteDirectoryLayout() : void } } - private function doGetcodeRevision() : string + private function doGetcodeRevision(): string { $this->log('

Getting the revision ID of the code repository'); $result = $this->runLocal(sprintf('git ls-remote %s %s', $this->getConfig(Option::repositoryUrl), $this->getConfig(Option::repositoryBranch))); @@ -253,7 +253,7 @@ private function doGetcodeRevision() : string return $revision; } - private function doUpdateCode() : void + private function doUpdateCode(): void { $repositoryRevision = $this->doGetcodeRevision(); @@ -264,19 +264,19 @@ private function doUpdateCode() : void $this->runRemote(sprintf('cp -RPp {{ deploy_dir }}/repo/* {{ project_dir }}')); } - private function doCreateCacheDir() : void + private function doCreateCacheDir(): void { $this->log('

Creating cache directory'); $this->runRemote('if [ -d {{ cache_dir }} ]; then rm -rf {{ cache_dir }}; fi; mkdir -p {{ cache_dir }}'); } - private function doCreateLogDir() : void + private function doCreateLogDir(): void { $this->log('

Creating log directory'); $this->runRemote('if [ -d {{ log_dir }} ] ; then rm -rf {{ log_dir }}; fi; mkdir -p {{ log_dir }}'); } - private function doCreateSharedDirs() : void + private function doCreateSharedDirs(): void { $this->log('

Creating symlinks for shared directories'); foreach ($this->getConfig(Option::sharedDirs) as $sharedDir) { @@ -286,7 +286,7 @@ private function doCreateSharedDirs() : void } } - private function doCreateSharedFiles() : void + private function doCreateSharedFiles(): void { $this->log('

Creating symlinks for shared files'); foreach ($this->getConfig(Option::sharedFiles) as $sharedFile) { @@ -299,7 +299,7 @@ private function doCreateSharedFiles() : void // this method was inspired by https://github.com/deployphp/deployer/blob/master/recipe/deploy/writable.php // (c) Anton Medvedev - private function doSetPermissions() : void + private function doSetPermissions(): void { $permissionMethod = $this->getConfig(Option::permissionMethod); $writableDirs = implode(' ', $this->getConfig(Option::writableDirs)); @@ -333,7 +333,7 @@ private function doSetPermissions() : void throw new InvalidConfigurationException(sprintf('The "%s" permission method is not valid. Select one of the supported methods.', $permissionMethod)); } - private function doInstallDependencies() : void + private function doInstallDependencies(): void { if (true === $this->getConfig(Option::updateRemoteComposerBinary)) { $this->log('

Self Updating the Composer binary'); @@ -344,7 +344,7 @@ private function doInstallDependencies() : void $this->runRemote(sprintf('%s install %s', $this->getConfig(Option::remoteComposerBinaryPath), $this->getConfig(Option::composerInstallFlags))); } - private function doInstallWebAssets() : void + private function doInstallWebAssets(): void { if (true !== $this->getConfig(Option::installWebAssets)) { return; @@ -354,7 +354,7 @@ private function doInstallWebAssets() : void $this->runRemote(sprintf('{{ console_bin }} assets:install {{ web_dir }} --symlink --no-debug --env=%s', $this->getConfig(Option::symfonyEnvironment))); } - private function doDumpAsseticAssets() : void + private function doDumpAsseticAssets(): void { if (true !== $this->getConfig(Option::dumpAsseticAssets)) { return; @@ -364,7 +364,7 @@ private function doDumpAsseticAssets() : void $this->runRemote(sprintf('{{ console_bin }} assetic:dump --no-debug --env=%s', $this->getConfig(Option::symfonyEnvironment))); } - private function doWarmupCache() : void + private function doWarmupCache(): void { if (true !== $this->getConfig(Option::warmupCache)) { return; @@ -375,7 +375,7 @@ private function doWarmupCache() : void $this->runRemote('chmod -R g+w {{ cache_dir }}'); } - private function doClearControllers() : void + private function doClearControllers(): void { $this->log('

Clearing controllers'); foreach ($this->getServers()->findByRoles([Server::ROLE_APP]) as $server) { @@ -387,19 +387,19 @@ private function doClearControllers() : void } } - private function doOptimizeComposer() : void + private function doOptimizeComposer(): void { $this->log('

Optimizing Composer autoloader'); $this->runRemote(sprintf('%s dump-autoload %s', $this->getConfig(Option::remoteComposerBinaryPath), $this->getConfig(Option::composerOptimizeFlags))); } - private function doCreateSymlink() : void + private function doCreateSymlink(): void { $this->log('

Updating the symlink'); $this->runRemote('rm -f {{ deploy_dir }}/current && ln -s {{ project_dir }} {{ deploy_dir }}/current'); } - private function doResetOpCache() : void + private function doResetOpCache(): void { if (null === $homepageUrl = $this->getConfig(Option::resetOpCacheFor)) { return; @@ -410,7 +410,7 @@ private function doResetOpCache() : void $this->runRemote(sprintf('echo " {{ web_dir }}/%s && wget %s/%s && rm -f {{ web_dir }}/%s', $phpScriptPath, $homepageUrl, $phpScriptPath, $phpScriptPath)); } - private function doKeepReleases() : void + private function doKeepReleases(): void { if (-1 === $this->getConfig(Option::keepReleases)) { $this->log('

No releases to delete'); @@ -424,7 +424,7 @@ private function doKeepReleases() : void } } - private function deleteOldReleases(Server $server, array $releaseDirs) : void + private function deleteOldReleases(Server $server, array $releaseDirs): void { foreach ($releaseDirs as $releaseDir) { if (!preg_match('/\d{14}/', $releaseDir)) { diff --git a/src/Helper/Str.php b/src/Helper/Str.php index 2c235cd..5d68cb7 100644 --- a/src/Helper/Str.php +++ b/src/Helper/Str.php @@ -17,27 +17,27 @@ */ class Str { - public static function startsWith(string $haystack, string $needle) : bool + public static function startsWith(string $haystack, string $needle): bool { return '' !== $needle && 0 === mb_strpos($haystack, $needle); } - public static function endsWith(string $haystack, string $needle) : bool + public static function endsWith(string $haystack, string $needle): bool { return $needle === mb_substr($haystack, -mb_strlen($needle)); } - public static function contains(string $haystack, string $needle) : bool + public static function contains(string $haystack, string $needle): bool { return '' !== $needle && false !== mb_strpos($haystack, $needle); } - public static function lineSeparator(string $char = '-') : string + public static function lineSeparator(string $char = '-'): string { return str_repeat($char, 80); } - public static function prefix($text, string $prefix) : string + public static function prefix($text, string $prefix): string { $text = is_array($text) ? $text : explode(PHP_EOL, $text); @@ -46,7 +46,7 @@ public static function prefix($text, string $prefix) : string }, $text)); } - public static function stringify($value) : string + public static function stringify($value): string { if (is_resource($value)) { return 'PHP Resource'; @@ -67,7 +67,7 @@ public static function stringify($value) : string return (string) $value; } - public static function formatAsTable(array $data, bool $sortKeys = true) : string + public static function formatAsTable(array $data, bool $sortKeys = true): string { if ($sortKeys) { ksort($data); diff --git a/src/Logger.php b/src/Logger.php index 1db1311..a9bbd4e 100644 --- a/src/Logger.php +++ b/src/Logger.php @@ -33,7 +33,7 @@ public function __construct(Context $context) $this->initializeLogFile(); } - public function log(string $message) : void + public function log(string $message): void { $isPriorityMessage = Str::startsWith($message, '

'); $isResultMessage = Str::contains($message, '') || Str::contains($message, ''); @@ -44,7 +44,7 @@ public function log(string $message) : void $this->writeToLogFile($message); } - private function createOutputFormatter() : OutputFormatter + private function createOutputFormatter(): OutputFormatter { return new OutputFormatter(true, [ 'command' => new OutputFormatterStyle('yellow', null), @@ -61,19 +61,19 @@ private function createOutputFormatter() : OutputFormatter ]); } - private function initializeLogFile() : void + private function initializeLogFile(): void { (new Filesystem())->dumpFile($this->logFilePath, ''); $this->writeToLogFile(sprintf("%s\nDeployment started at %s\n%s", Str::lineSeparator('='), date('r'), Str::lineSeparator('='))); } - private function writeToLogFile(string $message) : void + private function writeToLogFile(string $message): void { $loggedMessage = $this->processLogMessageForFile($message); file_put_contents($this->logFilePath, $loggedMessage.PHP_EOL, FILE_APPEND); } - private function processLogMessageForFile(string $message) : string + private function processLogMessageForFile(string $message): string { $replacements = [ '/(.*)<\/>/' => '"$1"', diff --git a/src/Requirement/AbstractRequirement.php b/src/Requirement/AbstractRequirement.php index 62cb9ec..7f31ee7 100644 --- a/src/Requirement/AbstractRequirement.php +++ b/src/Requirement/AbstractRequirement.php @@ -24,12 +24,12 @@ public function __construct(array $servers) $this->servers = $servers; } - public function getServers() : array + public function getServers(): array { return $this->servers; } - abstract public function getChecker() : Task; + abstract public function getChecker(): Task; - abstract public function getMessage() : string; + abstract public function getMessage(): string; } diff --git a/src/Requirement/AllowsLoginViaSsh.php b/src/Requirement/AllowsLoginViaSsh.php index cfc0eda..814567c 100644 --- a/src/Requirement/AllowsLoginViaSsh.php +++ b/src/Requirement/AllowsLoginViaSsh.php @@ -15,12 +15,12 @@ class AllowsLoginViaSsh extends AbstractRequirement { - public function getMessage() : string + public function getMessage(): string { return '[OK] The server allows to login via SSH from the local machine'; } - public function getChecker() : Task + public function getChecker(): Task { $shellCommand = sprintf('echo %s', mt_rand()); diff --git a/src/Requirement/CommandExists.php b/src/Requirement/CommandExists.php index 32a3e21..d985eb4 100644 --- a/src/Requirement/CommandExists.php +++ b/src/Requirement/CommandExists.php @@ -23,19 +23,19 @@ public function __construct(array $servers, string $commandName) $this->commandName = $commandName; } - public function getMessage() : string + public function getMessage(): string { return sprintf('[OK] %s command exists', $this->commandName); } - public function getChecker() : Task + public function getChecker(): Task { $shellCommand = sprintf('%s %s', $this->isWindows() ? 'where' : 'which', $this->commandName); return new Task($this->getServers(), $shellCommand); } - private function isWindows() : bool + private function isWindows(): bool { return '\\' === DIRECTORY_SEPARATOR; } diff --git a/src/Server/Server.php b/src/Server/Server.php index f402181..676a9a1 100644 --- a/src/Server/Server.php +++ b/src/Server/Server.php @@ -43,17 +43,17 @@ public function __construct(string $dsn, array $roles = [self::ROLE_APP], array $this->port = $params['port'] ?? null; } - public function __toString() : string + public function __toString(): string { return sprintf('%s%s', $this->getUser() ? $this->getUser().'@' : '', $this->getHost()); } - public function isLocalHost() : bool + public function isLocalHost(): bool { return in_array($this->getHost(), self::LOCALHOST_ADDRESSES, true); } - public function resolveProperties(string $expression) : string + public function resolveProperties(string $expression): string { $definedProperties = $this->properties; $resolved = preg_replace_callback('/(\{\{\s*(?.+)\s*\}\})/U', function (array $matches) use ($definedProperties, $expression) { @@ -68,7 +68,7 @@ public function resolveProperties(string $expression) : string return $resolved; } - public function getProperties() : array + public function getProperties(): array { return $this->properties->all(); } @@ -78,17 +78,17 @@ public function get(string $propertyName, $default = null) return $this->properties->get($propertyName, $default); } - public function set(string $propertyName, $value) : void + public function set(string $propertyName, $value): void { $this->properties->set($propertyName, $value); } - public function has(string $propertyName) : bool + public function has(string $propertyName): bool { return $this->properties->has($propertyName); } - public function getSshConnectionString() : string + public function getSshConnectionString(): string { if ($this->isLocalHost()) { return ''; @@ -102,22 +102,22 @@ public function getSshConnectionString() : string ); } - public function getRoles() : array + public function getRoles(): array { return $this->roles; } - public function getUser() : ?string + public function getUser(): ?string { return $this->user; } - public function getHost() : string + public function getHost(): string { return $this->host; } - public function getPort() : ?int + public function getPort(): ?int { return $this->port; } diff --git a/src/Server/ServerRepository.php b/src/Server/ServerRepository.php index ae69642..d0c9b21 100644 --- a/src/Server/ServerRepository.php +++ b/src/Server/ServerRepository.php @@ -20,17 +20,17 @@ class ServerRepository /** @var Server[] $servers */ private $servers = []; - public function __toString() : string + public function __toString(): string { return implode(', ', $this->servers); } - public function add(Server $server) : void + public function add(Server $server): void { $this->servers[] = $server; } - public function findAll() : array + public function findAll(): array { return $this->servers; } @@ -38,7 +38,7 @@ public function findAll() : array /** * @return Server[] */ - public function findByRoles(array $roles) : array + public function findByRoles(array $roles): array { return array_filter($this->servers, function (Server $server) use ($roles) { return !empty(array_intersect($roles, $server->getRoles())); diff --git a/src/Task/Task.php b/src/Task/Task.php index df7f19e..007da0a 100644 --- a/src/Task/Task.php +++ b/src/Task/Task.php @@ -34,12 +34,12 @@ public function __construct(array $servers, string $shellCommand, array $envVars /** * @return Server[] */ - public function getServers() : array + public function getServers(): array { return $this->servers; } - public function isLocal() : bool + public function isLocal(): bool { foreach ($this->servers as $server) { if (!$server->isLocalHost()) { @@ -50,17 +50,17 @@ public function isLocal() : bool return true; } - public function isRemote() : bool + public function isRemote(): bool { return !$this->isLocal(); } - public function getShellCommand() : string + public function getShellCommand(): string { return $this->shellCommand; } - public function getEnvVars() : array + public function getEnvVars(): array { return $this->envVars; } diff --git a/src/Task/TaskCompleted.php b/src/Task/TaskCompleted.php index cee9d9f..6af3c44 100644 --- a/src/Task/TaskCompleted.php +++ b/src/Task/TaskCompleted.php @@ -30,27 +30,27 @@ public function __construct(Server $server, string $output, int $exitCode) $this->exitCode = $exitCode; } - public function isSuccessful() : bool + public function isSuccessful(): bool { return 0 === $this->exitCode; } - public function getServer() : Server + public function getServer(): Server { return $this->server; } - public function getOutput() : string + public function getOutput(): string { return $this->output; } - public function getTrimmedOutput() : string + public function getTrimmedOutput(): string { return trim($this->output); } - public function getExitCode() : int + public function getExitCode(): int { return $this->exitCode; } diff --git a/src/Task/TaskRunner.php b/src/Task/TaskRunner.php index d66877f..5189dba 100644 --- a/src/Task/TaskRunner.php +++ b/src/Task/TaskRunner.php @@ -31,7 +31,7 @@ public function __construct(bool $isDryRun, Logger $logger) /** * @return TaskCompleted[] */ - public function run(Task $task) : array + public function run(Task $task): array { $results = []; foreach ($task->getServers() as $server) { @@ -41,7 +41,7 @@ public function run(Task $task) : array return $results; } - private function doRun(Server $server, string $shellCommand, array $envVars) : TaskCompleted + private function doRun(Server $server, string $shellCommand, array $envVars): TaskCompleted { if ($server->has(Property::project_dir)) { $shellCommand = sprintf('cd %s && %s', $server->get(Property::project_dir), $shellCommand);