deployphp/deployer

View on GitHub
bin/dep

Summary

Maintainability
Test Coverage
#!/usr/bin/env php
<?php
/* (c) Anton Medvedev <anton@medv.io>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

// Check PHP version
if (PHP_VERSION_ID < 80200) {
    fwrite(STDERR, "PHP 8.2 or higher is required.\n");
    exit(1);
}

// Detect deploy.php location
$deployFile = null;
foreach ($argv as $i => $arg) {
    if (preg_match('/^(-f|--file)$/', $arg, $match) && $i + 1 < count($argv)) {
        $deployFile = $argv[$i + 1];
        break;
    }
    if (preg_match('/^--file=(?<file>.+)$/', $arg, $match)) {
        $deployFile = $match['file'];
        break;
    }
    if (preg_match('/^-f=?(?<file>.+)$/', $arg, $match)) {
        $deployFile = $match['file'];
        break;
    }
}
if (!empty($deployFile)) {
    $deployFile = realpath($deployFile);
}
$lookUp = function (string $name): ?string {
    $dir = getcwd();
    for ($i = 0; $i < 10; $i++) {
        $path = "$dir/$name";
        if (is_readable($path)) {
            return $path;
        }
        $dir = dirname($dir);
    }
    return '';
};
if (empty($deployFile)) {
    $deployFile = $lookUp('deploy.php');
}
if (empty($deployFile)) {
    $deployFile = $lookUp('deploy.yaml');
}
if (empty($deployFile)) {
    $deployFile = $lookUp('deploy.yml');
}

// Detect autoload location
$autoload = [
    __DIR__ . '/../vendor/autoload.php', // The dep located at "deployer.phar/bin" or in development.
    __DIR__ . '/../../../autoload.php', // The dep located at "vendor/deployer/deployer/bin".
    __DIR__ . '/../autoload.php', // The dep located at "vendor/bin".
];
$includes = [
    __DIR__ . '/..',
    __DIR__ . '/../../../deployer/deployer',
    __DIR__ . '/../deployer/deployer',
];
$includePath = false;
for ($i = 0; $i < count($autoload); $i++) {
    if (file_exists($autoload[$i]) && is_dir($includes[$i])) {
        require $autoload[$i];
        $includePath = $includes[$i];
        break;
    }
}
if (empty($includePath)) {
    fwrite(STDERR, "Error: The `autoload.php` file not found in:\n");
    for ($i = 0; $i < count($autoload); $i++) {
        $a = file_exists($autoload[$i]) ? 'true' : 'false';
        $b = is_dir($includes[$i]) ? 'true' : 'false';
        fwrite(STDERR, "  - file_exists($autoload[$i]) = $a\n");
        fwrite(STDERR, "    is_dir($includes[$i]) = $b\n");
    }
    exit(1);
}

// Errors to exception
set_error_handler(function ($severity, $message, $filename, $lineno) {
    if (error_reporting() == 0) {
        return;
    }
    if (error_reporting() & $severity) {
        throw new ErrorException($message, 0, $severity, $filename, $lineno);
    }
});

// Enable recipe loading
set_include_path($includePath . PATH_SEPARATOR . get_include_path());

// Deployer constants
define('DEPLOYER', true);
define('DEPLOYER_BIN', __FILE__);
define('DEPLOYER_DEPLOY_FILE', $deployFile);

Deployer\Deployer::run('master', $deployFile);