Deployment\Deployer::deploy PHP Method

deploy() public method

Synchronize remote and local.
public deploy ( ) : void
return void
    public function deploy()
    {
        $this->logger->log("Connecting to server");
        $this->server->connect();
        $this->remoteDir = $this->server->getDir();
        $runBefore = [NULL, NULL];
        foreach ($this->runBefore as $job) {
            $runBefore[is_string($job) && preg_match('#^local:#', $job)][] = $job;
        }
        if ($runBefore[1]) {
            $this->logger->log("\nLocal-jobs:");
            $this->runJobs($runBefore[1]);
            $this->logger->log('');
        }
        $remotePaths = $this->loadDeploymentFile();
        if (is_array($remotePaths)) {
            $this->logger->log("Loaded remote {$this->deploymentFile} file");
        } else {
            $this->logger->log("Remote {$this->deploymentFile} file not found");
            $remotePaths = [];
        }
        $this->logger->log("Scanning files in {$this->localDir}");
        $localPaths = $this->collectPaths();
        unset($localPaths["/{$this->deploymentFile}"], $remotePaths["/{$this->deploymentFile}"]);
        $toDelete = $this->allowDelete ? array_keys(array_diff_key($remotePaths, $localPaths)) : [];
        $toUpload = array_keys(array_diff_assoc($localPaths, $remotePaths));
        if ($localPaths !== $remotePaths) {
            // ignores allowDelete
            $deploymentFile = $this->writeDeploymentFile($localPaths);
            $toUpload[] = "/{$this->deploymentFile}";
            // must be last
        }
        if (!$toUpload && !$toDelete) {
            $this->logger->log('Already synchronized.', 'lime');
            return;
        } elseif ($this->testMode) {
            $this->logger->log("\nUploading:\n" . implode("\n", $toUpload), 'green', FALSE);
            $this->logger->log("\nDeleting:\n" . implode("\n", $toDelete), 'maroon', FALSE);
            if (isset($deploymentFile)) {
                unlink($deploymentFile);
            }
            return;
        }
        $this->logger->log("Creating remote file {$this->deploymentFile}.running");
        $runningFile = "{$this->remoteDir}/{$this->deploymentFile}.running";
        $this->server->createDir(str_replace('\\', '/', dirname($runningFile)));
        $this->server->writeFile(tempnam($this->tempDir, 'deploy'), $runningFile);
        if ($runBefore[0]) {
            $this->logger->log("\nBefore-jobs:");
            $this->runJobs($runBefore[0]);
        }
        if ($toUpload) {
            $this->logger->log("\nUploading:");
            $this->uploadPaths($toUpload);
            if ($this->runAfterUpload) {
                $this->logger->log("\nAfter-upload-jobs:");
                $this->runJobs($this->runAfterUpload);
            }
            $this->logger->log("\nRenaming:");
            $this->renamePaths($toUpload);
            unlink($deploymentFile);
        }
        if ($toDelete) {
            $this->logger->log("\nDeleting:");
            $this->deletePaths($toDelete);
        }
        foreach ((array) $this->toPurge as $path) {
            $this->logger->log("\nCleaning {$path}");
            $this->server->purge($this->remoteDir . '/' . $path, function ($path) {
                static $counter;
                $path = substr($path, strlen($this->remoteDir));
                $path = preg_match('#/(.{1,60})$#', $path, $m) ? $m[1] : substr(basename($path), 0, 60);
                $this->logger->progress(str_pad($path . ' ' . str_repeat('.', $counter++ % 30 + 60 - strlen($path)), 90));
            });
            $this->logger->progress(str_repeat(' ', 91));
        }
        if ($this->runAfter) {
            $this->logger->log("\nAfter-jobs:");
            $this->runJobs($this->runAfter);
        }
        $this->logger->log("\nDeleting remote file {$this->deploymentFile}.running");
        $this->server->removeFile($runningFile);
    }