Same name and namespace in other branches
  1. 8.x-1.x src/Commands/ColorboxCommands.php \Drupal\colorbox\Commands\ColorboxCommands::download() 1 comment

Download and install the Colorbox plugin.

@command colorbox:plugin @aliases colorboxplugin,colorbox-plugin

Parameters

mixed $path: Optional. A path where to install the Colorbox plugin. If omitted Drush will use the default location.

File

src/Commands/ColorboxCommands.php, line 48

Class

ColorboxCommands
A Drush commandfile.

Namespace

Drupal\colorbox\Commands

Code

public function download($path = '') {
    $fs = new Filesystem();
    if (empty($path)) {
        $path = DRUPAL_ROOT . '/libraries/colorbox';
    }
    // Create path if it doesn't exist
    // Exit with a message otherwise.
    if (!$fs->exists($path)) {
        $fs->mkdir($path);
    }
    else {
        $this->logger()
            ->notice(dt('Colorbox is already present at @path. No download required.', [
            '@path' => $path,
        ]));
        return;
    }
    // Load the colorbox defined library.
    if ($colorbox_library = $this->libraryDiscovery
        ->getLibraryByName('colorbox', 'colorbox')) {
        // Download the file.
        $client = new Client();
        $destination = tempnam(sys_get_temp_dir(), 'colorbox-tmp');
        try {
            $client->get($colorbox_library['remote'] . '/archive/master.zip', [
                'sink' => $destination,
            ]);
        } catch (RequestException $e) {
            // Remove the directory.
            $fs->remove($path);
            $this->logger()
                ->error(dt('Drush was unable to download the colorbox library from @remote. @exception', [
                '@remote' => $colorbox_library['remote'] . '/archive/master.zip',
                '@exception' => $e->getMessage(),
            ]));
            return;
        }
        // Move downloaded file.
        $fs->rename($destination, $path . '/colorbox.zip');
        // Unzip the file.
        $zip = new \ZipArchive();
        $res = $zip->open($path . '/colorbox.zip');
        if ($res === TRUE) {
            $zip->extractTo($path);
            $zip->close();
        }
        else {
            // Remove the directory if unzip fails and exit.
            $fs->remove($path);
            $this->logger()
                ->error(dt('Error: unable to unzip colorbox file.', []));
            return;
        }
        // Remove the downloaded zip file.
        $fs->remove($path . '/colorbox.zip');
        // Move the file.
        $fs->mirror($path . '/colorbox-master', $path, NULL, [
            'override' => TRUE,
        ]);
        $fs->remove($path . '/colorbox-master');
        // Success.
        $this->logger()
            ->success(dt('The colorbox library has been successfully downloaded to @path.', [
            '@path' => $path,
        ]));
    }
    else {
        $this->logger()
            ->error(dt('Drush was unable to load the colorbox library'));
    }
}