|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Deployer plugin for PHPCensor |
| 4 | + * |
| 5 | + * @author Alexey Boyko <ket4yiit@gmail.com> |
| 6 | + * @license MIT |
| 7 | + * https://github.com/ket4yii/phpcensor-deployer-plugin/blob/master/LICENSE |
| 8 | + * |
| 9 | + * @link https://github.com/ket4yii/phpcensor-deployer-plugin |
| 10 | + * @see http://deployer.org |
| 11 | + */ |
| 12 | + |
| 13 | +namespace Ket4yii\PHPCensor\Deployer\Plugin; |
| 14 | + |
| 15 | +use PHPCensor\Builder; |
| 16 | +use PHPCensor\Model\Build; |
| 17 | + |
| 18 | +class Deployer implements \PHPCensor\Plugin |
| 19 | +{ |
| 20 | + |
| 21 | + protected $phpcensor; |
| 22 | + protected $build; |
| 23 | + protected $config; |
| 24 | + protected $dep; |
| 25 | + protected $branch; |
| 26 | + |
| 27 | + /** |
| 28 | + * Standard Constructor |
| 29 | + * |
| 30 | + * $options['directory'] Output Directory. Default: %BUILDPATH% |
| 31 | + * $options['filename'] Phar Filename. Default: build.phar |
| 32 | + * $options['regexp'] Regular Expression Filename Capture. Default: /\.php$/ |
| 33 | + * $options['stub'] Stub Content. No Default Value |
| 34 | + * |
| 35 | + * @param Builder $phpcensor PHPCensor instance |
| 36 | + * @param Build $build Build instance |
| 37 | + * @param array $options Plugin options |
| 38 | + */ |
| 39 | + public function __construct( |
| 40 | + Builder $phpcensor, |
| 41 | + Build $build, |
| 42 | + array $options = array() |
| 43 | + ) { |
| 44 | + $this->phpcensor = $phpcensor; |
| 45 | + $this->build = $build; |
| 46 | + $this->config = $options; |
| 47 | + |
| 48 | + $this->dep = $this->phpcensor->findBinary('dep'); |
| 49 | + $this->branch = $this->build->getBranch(); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * PHPCensor plugin executor. |
| 54 | + * |
| 55 | + * @return bool Did plugin execute successfully |
| 56 | + */ |
| 57 | + public function execute() |
| 58 | + { |
| 59 | + if (($validationResult = $this->validateConfig()) !== null) { |
| 60 | + $this->phpcensor->log($validationResult['message']); |
| 61 | + |
| 62 | + return $validationResult['successful']; |
| 63 | + } |
| 64 | + |
| 65 | + $branchConfig = $this->config[$this->branch]; |
| 66 | + $options = $this->getOptions($branchConfig); |
| 67 | + |
| 68 | + $deployerCmd = "$this->dep$options"; |
| 69 | + |
| 70 | + return $this->phpcensor->executeCommand($deployerCmd); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Validate config. |
| 75 | + * |
| 76 | + * $validationRes['message'] Message to log |
| 77 | + * $validationRes['successful'] Plugin status that is connected with error |
| 78 | + * |
| 79 | + * @return array validation result |
| 80 | + */ |
| 81 | + protected function validateConfig() |
| 82 | + { |
| 83 | + if (empty($this->config)) { |
| 84 | + return [ |
| 85 | + 'message' => 'Can\'t find configuration for plugin!', |
| 86 | + 'successful' => false |
| 87 | + ]; |
| 88 | + } |
| 89 | + |
| 90 | + if (empty($this->config[$this->branch])) { |
| 91 | + return [ |
| 92 | + 'message' => 'There is no specified config for this branch.', |
| 93 | + 'successful' => true |
| 94 | + ]; |
| 95 | + } |
| 96 | + |
| 97 | + $branchConf = $this->config[$this->branch]; |
| 98 | + |
| 99 | + if (empty($branchConf['stage'])) { |
| 100 | + return [ |
| 101 | + 'message' => 'There is no stage for this branch', |
| 102 | + 'successful' => false |
| 103 | + ]; |
| 104 | + } |
| 105 | + |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Get verbosity flag. |
| 111 | + * |
| 112 | + * @param string $verbosity User defined verbosity level |
| 113 | + * |
| 114 | + * @return string Verbosity flag |
| 115 | + */ |
| 116 | + protected function getVerbosityOption($verbosity) |
| 117 | + { |
| 118 | + $LOG_LEVEL_ENUM = [ |
| 119 | + 'verbose' =>'v', |
| 120 | + 'very verbose' => 'vv', |
| 121 | + 'debug' => 'vvv', |
| 122 | + 'quiet' => 'q' |
| 123 | + ]; |
| 124 | + |
| 125 | + $verbosity = strtolower(trim($verbosity)); |
| 126 | + |
| 127 | + if ($verbosity !== 'normal') { |
| 128 | + return '-' . $LOG_LEVEL_ENUM[$verbosity]; |
| 129 | + } else { |
| 130 | + return ''; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Make deployer options from config |
| 136 | + * |
| 137 | + * @param array $config Deployer configration array |
| 138 | + * |
| 139 | + * @return string Deployer options |
| 140 | + */ |
| 141 | + protected function getOptions($config) |
| 142 | + { |
| 143 | + $options = []; |
| 144 | + |
| 145 | + if ($config['task'] != null) { |
| 146 | + $options[] = $config['task']; |
| 147 | + } else { |
| 148 | + $options[] = 'deploy'; |
| 149 | + } |
| 150 | + |
| 151 | + if ($config['stage'] != null) { |
| 152 | + $options[] = $config['stage']; |
| 153 | + } |
| 154 | + |
| 155 | + if ($config['verbosity'] != null) { |
| 156 | + $options[] = $this->getVerbosityOption($config['verbosity']); |
| 157 | + } |
| 158 | + |
| 159 | + if ($config['file'] != null) { |
| 160 | + $options[] = '--file=' . $config['file']; |
| 161 | + } |
| 162 | + |
| 163 | + return implode('', $options); |
| 164 | + } |
| 165 | +} |
0 commit comments