diff --git a/UPGRADE.md b/UPGRADE.md index ebcdc47b..70826680 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,3 +1,40 @@ +# Upgrade from 4.x to 5.x + +1. Servers to Hosts + + * Refactor `server($name, $hostname)` to `host($hostname)` + * Refactor `localServer($name)` to `localhost()` + * Rename `serverList($file)` to `inventory($file)` + +2. Configuration options + + * Rename `{{server.name}}` to `{{hostname}}` + +3. DotArray syntax + + In v5 access to nested arrays in config via dot notation was removed. + If you was using it, consider to move to plain config options. + + Refactor this: + + ```php + set('a', ['b' => 1]); + + // ... + + get('a.b'); + ``` + + To: + + ```php + set('a_b', 1); + + // ... + + get('a_b'); + ``` + # Upgrade from 3.x to 4.x 1. Namespace for functions diff --git a/src/Host/FileLoader.php b/src/Host/FileLoader.php index ea84e90c..6adcc317 100644 --- a/src/Host/FileLoader.php +++ b/src/Host/FileLoader.php @@ -22,6 +22,10 @@ class FileLoader */ public function load($file) { + if (!file_exists($file) || !is_readable($file)) { + throw new ConfigurationException("File `$file` doesn't exists or doesn't readable."); + } + $data = Yaml::parse(file_get_contents($file)); if (!is_array($data)) { diff --git a/src/Utility/Proxy.php b/src/Utility/Proxy.php new file mode 100644 index 00000000..b0b11535 --- /dev/null +++ b/src/Utility/Proxy.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Deployer\Utility; + +class Proxy +{ + private $objects; + + public function __construct(array $objects) + { + $this->objects = $objects; + } + + public function __call($name, $arguments) + { + foreach ($this->objects as $object) { + $object->$name(...$arguments); + } + return $this; + } +} \ No newline at end of file diff --git a/src/functions.php b/src/functions.php index 508c7376..87567937 100644 --- a/src/functions.php +++ b/src/functions.php @@ -13,6 +13,7 @@ use Deployer\Task\Context; use Deployer\Task\GroupTask; use Deployer\Task\Task as T; use Deployer\Type\Result; +use Deployer\Utility\Proxy; use Monolog\Logger; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -30,12 +31,22 @@ use Symfony\Component\Finder\Finder; // execution stage. They are acts like two different function, but have same name. Example of such function // is set() func. This function determine in which stage it was called by Context::get() method. -function host($hostname, $alias = null) +function host(...$hostnames) { $deployer = Deployer::get(); - $host = new Host($hostname); - $deployer->hosts->set($alias ?: $hostname, $host); - return $host; + + if (count($hostnames) === 1) { + $host = new Host($hostnames[0]); + $deployer->hosts->set($hostnames[0], $host); + return $host; + } else { + $hosts = array_map(function ($hostname) use ($deployer) { + $host = new Host($hostname); + $deployer->hosts->set($hostname, $host); + return $host; + }, $hostnames); + return new Proxy($hosts); + } } function localhost($alias = 'localhost') @@ -51,7 +62,7 @@ function localhost($alias = 'localhost') * * @param string $file */ -function hostsList($file) +function inventory($file) { $deployer = Deployer::get(); $fileLoader = new FileLoader();