2015-04-25 03:57:40 +02:00
|
|
|
<?php namespace System\Console;
|
|
|
|
|
2015-05-02 12:29:22 +10:00
|
|
|
use File;
|
|
|
|
use Cms\Classes\Theme;
|
|
|
|
use Cms\Classes\ThemeManager;
|
|
|
|
use System\Classes\UpdateManager;
|
2015-04-25 03:57:40 +02:00
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
2015-05-02 12:29:22 +10:00
|
|
|
use Exception;
|
2015-04-25 03:57:40 +02:00
|
|
|
|
2017-03-16 17:08:20 +11:00
|
|
|
/**
|
|
|
|
* Console command to install a new theme.
|
|
|
|
*
|
|
|
|
* This adds a new theme by requesting it from the October marketplace.
|
|
|
|
*
|
|
|
|
* @package october\system
|
|
|
|
* @author Alexey Bobkov, Samuel Georges
|
|
|
|
*/
|
2015-04-25 03:57:40 +02:00
|
|
|
class ThemeInstall extends Command
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The console command name.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $name = 'theme:install';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'Install a theme from the October marketplace.';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-07-14 16:28:47 +10:00
|
|
|
public function handle()
|
2015-04-25 03:57:40 +02:00
|
|
|
{
|
2015-04-26 20:16:38 +02:00
|
|
|
$themeName = $this->argument('name');
|
2015-04-25 03:57:40 +02:00
|
|
|
$argDirName = $this->argument('dirName');
|
|
|
|
|
2015-04-26 20:16:38 +02:00
|
|
|
if ($argDirName && $themeName == $argDirName) {
|
|
|
|
$argDirName = null;
|
2015-04-25 03:57:40 +02:00
|
|
|
}
|
|
|
|
|
2015-04-26 20:16:38 +02:00
|
|
|
if ($argDirName) {
|
|
|
|
if (!preg_match('/^[a-z0-9\_\-]+$/i', $argDirName)) {
|
|
|
|
return $this->error('Invalid destination directory name.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Theme::exists($argDirName)) {
|
2015-05-02 12:29:22 +10:00
|
|
|
return $this->error(sprintf('A theme named %s already exists.', $argDirName));
|
2015-04-26 20:16:38 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-25 03:57:40 +02:00
|
|
|
|
|
|
|
try {
|
2015-04-26 20:16:38 +02:00
|
|
|
$themeManager = ThemeManager::instance();
|
|
|
|
$updateManager = UpdateManager::instance();
|
|
|
|
|
2015-04-25 03:57:40 +02:00
|
|
|
$themeDetails = $updateManager->requestThemeDetails($themeName);
|
|
|
|
|
2015-04-26 20:16:38 +02:00
|
|
|
if ($themeManager->isInstalled($themeDetails['code'])) {
|
|
|
|
return $this->error(sprintf('The theme %s is already installed.', $themeDetails['code']));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Theme::exists($themeDetails['code'])) {
|
2015-05-02 12:29:22 +10:00
|
|
|
return $this->error(sprintf('A theme named %s already exists.', $themeDetails['code']));
|
2015-04-25 03:57:40 +02:00
|
|
|
}
|
|
|
|
|
2015-05-02 12:29:22 +10:00
|
|
|
$fields = ['Name', 'Description', 'Author', 'URL', ''];
|
|
|
|
|
2015-04-25 03:57:40 +02:00
|
|
|
$this->info(sprintf(
|
2015-05-02 12:29:22 +10:00
|
|
|
implode(': %s'.PHP_EOL, $fields),
|
2015-04-25 03:57:40 +02:00
|
|
|
$themeDetails['code'],
|
|
|
|
$themeDetails['description'],
|
|
|
|
$themeDetails['author'],
|
2015-05-02 12:29:22 +10:00
|
|
|
$themeDetails['product_url']
|
|
|
|
));
|
2015-04-25 03:57:40 +02:00
|
|
|
|
|
|
|
if (!$this->confirm('Do you wish to continue? [Y|n]', true)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->info('Downloading theme...');
|
|
|
|
$updateManager->downloadTheme($themeDetails['code'], $themeDetails['hash']);
|
|
|
|
|
|
|
|
$this->info('Extracting theme...');
|
|
|
|
$updateManager->extractTheme($themeDetails['code'], $themeDetails['hash']);
|
|
|
|
|
|
|
|
$dirName = $this->themeCodeToDir($themeDetails['code']);
|
2015-04-26 20:16:38 +02:00
|
|
|
|
2015-04-25 03:57:40 +02:00
|
|
|
if ($argDirName) {
|
2015-04-26 20:16:38 +02:00
|
|
|
/*
|
|
|
|
* Move downloaded theme to a new directory.
|
|
|
|
* Basically we're renaming it.
|
|
|
|
*/
|
2015-04-25 03:57:40 +02:00
|
|
|
File::move(themes_path().'/'.$dirName, themes_path().'/'.$argDirName);
|
2015-04-26 20:16:38 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Let's make sure to unflag the 'old' theme as
|
|
|
|
* installed so it can be re-installed later.
|
|
|
|
*/
|
|
|
|
$themeManager->setUninstalled($themeDetails['code']);
|
|
|
|
|
2015-04-25 03:57:40 +02:00
|
|
|
$dirName = $argDirName;
|
|
|
|
}
|
|
|
|
|
2015-04-25 04:10:51 +02:00
|
|
|
$this->info(sprintf('The theme %s has been installed. (now %s)', $themeDetails['code'], $dirName));
|
2015-04-25 03:57:40 +02:00
|
|
|
}
|
2015-05-02 12:29:22 +10:00
|
|
|
catch (Exception $ex) {
|
|
|
|
$this->error($ex->getMessage());
|
2015-04-25 03:57:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Theme code to dir.
|
|
|
|
*
|
|
|
|
* @param string $themeCode
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function themeCodeToDir($themeCode)
|
|
|
|
{
|
|
|
|
return strtolower(str_replace('.', '-', $themeCode));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the console command arguments.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getArguments()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['name', InputArgument::REQUIRED, 'The name of the theme. Eg: AuthorName.ThemeName'],
|
|
|
|
['dirName', InputArgument::OPTIONAL, 'Destination directory name for the theme installation.'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|