Cleanup police: using traits for GPM

This commit is contained in:
Djamil Legato
2014-09-04 20:34:36 -07:00
parent b0f37079b0
commit 24d1d4774e
7 changed files with 90 additions and 195 deletions

10
bin/gpm
View File

@@ -29,10 +29,10 @@ $grav = Grav::instance(['loader' => $autoload]);
$app = new Application('Grav Package Manager', '0.1.0');
$app->addCommands(array(
new \Grav\Console\Gpm\FetchCommand($grav),
new \Grav\Console\Gpm\IndexCommand($grav),
new \Grav\Console\Gpm\InfoCommand($grav),
new \Grav\Console\Gpm\InstallCommand($grav),
new \Grav\Console\Gpm\UpdateCommand($grav),
new \Grav\Console\Gpm\FetchCommand(),
new \Grav\Console\Gpm\IndexCommand(),
new \Grav\Console\Gpm\InfoCommand(),
new \Grav\Console\Gpm\InstallCommand(),
new \Grav\Console\Gpm\UpdateCommand(),
));
$app->run();

View File

@@ -0,0 +1,58 @@
<?php
namespace Grav\Console;
use Grav\Common\GravTrait;
use Grav\Common\Cache;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\OutputInterface;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Input\ArrayInput;
trait ConsoleTrait
{
use GravTrait;
protected $repository = 'http://getgrav.org/downloads';
protected $argv;
protected $input;
protected $output;
/**
* Set colors style definition for the formatter.
*/
public function setupConsole($input, $output)
{
self::$grav['config']->set('system.cache.driver', 'default');
$this->argv = $_SERVER['argv'][0];
$this->input = $input;
$this->output = $output;
$this->output->getFormatter()->setStyle('normal', new OutputFormatterStyle('white'));
$this->output->getFormatter()->setStyle('yellow', new OutputFormatterStyle('yellow', null, array('bold')));
$this->output->getFormatter()->setStyle('red', new OutputFormatterStyle('red', null, array('bold')));
$this->output->getFormatter()->setStyle('cyan', new OutputFormatterStyle('cyan', null, array('bold')));
$this->output->getFormatter()->setStyle('green', new OutputFormatterStyle('green', null, array('bold')));
$this->output->getFormatter()->setStyle('magenta', new OutputFormatterStyle('magenta', null, array('bold')));
$this->output->getFormatter()->setStyle('white', new OutputFormatterStyle('white', null, array('bold')));
}
/**
* Fetches the data from getgrav.org
* @return string Returns the data fetched or from cache in JSON format
*/
public function fetchData()
{
$fetchCommand = $this->getApplication()->find('fetch');
$args = new ArrayInput(array('command' => 'fetch', '-f' => $this->input->getOption('force')));
$commandExec = $fetchCommand->run($args, $this->output);
if ($commandExec != 0){
$URL = parse_url($this->repository, PHP_URL_HOST);
$this->output->writeln("<red>Error:</red> An error occured while trying to fetch data from <cyan>$URL</cyan>");
exit;
}
return self::$grav['cache']->fetch(md5('cli:gpm'));
}
}

View File

@@ -1,8 +1,7 @@
<?php
namespace Grav\Console\Gpm;
use Grav\Common\Grav;
use Grav\Common\Cache;
use Grav\Console\ConsoleTrait;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
@@ -12,26 +11,14 @@ use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Helper\ProgressBar;
class FetchCommand extends Command {
use ConsoleTrait;
protected $input;
protected $output;
protected $cache;
protected $argv;
protected $progress;
protected $repository = 'http://getgrav.org/downloads';
protected $pkg_types = array('plugins', 'themes');
public function __construct(Grav $grav){
$this->grav = $grav;
// just for the gpm cli we force the filesystem driver cache
$this->grav['config']->set('system.cache.driver', 'default');
$this->cache = $this->grav['cache']->fetch(md5('cli:gpm'));
parent::__construct();
}
protected function configure() {
protected function configure()
{
$this
->setName("fetch")
->addOption(
@@ -46,26 +33,25 @@ class FetchCommand extends Command {
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$this->setupConsole($input, $output);
$this->setColors();
$this->cache = self::$grav['cache']->fetch(md5('cli:gpm'));
$this->fetch($this->output);
}
private function fetch()
{
if (!$this->cache || $this->input->getOption('force')){
$data = $this->fetch_data();
$data = $this->fetchData();
$date = new \DateTime();
$this->grav['cache']->save(md5('cli:gpm'), $data, 86400);
self::$grav['cache']->save(md5('cli:gpm'), $data, 86400);
$date = $date->modify('+1 day')->format('D, d M Y H:i:s');
$this->cache = $data;
$this->output->writeln("Data cached until <cyan>".$date."</cyan>\n");
}
}
private function fetch_data()
private function fetchData()
{
$this->output->writeln("");
$this->output->writeln('Fetching data from <cyan>getgrav.org</cyan>');
@@ -96,16 +82,6 @@ class FetchCommand extends Command {
return $response;
}
private function setColors()
{
$this->output->getFormatter()->setStyle('normal', new OutputFormatterStyle('white'));
$this->output->getFormatter()->setStyle('red', new OutputFormatterStyle('red', null, array('bold')));
$this->output->getFormatter()->setStyle('cyan', new OutputFormatterStyle('cyan', null, array('bold')));
$this->output->getFormatter()->setStyle('green', new OutputFormatterStyle('green', null, array('bold')));
$this->output->getFormatter()->setStyle('magenta', new OutputFormatterStyle('magenta', null, array('bold')));
$this->output->getFormatter()->setStyle('white', new OutputFormatterStyle('white', null, array('bold')));
}
private function getCurl($progress = false)
{
$curl = curl_init();

View File

@@ -1,34 +1,20 @@
<?php
namespace Grav\Console\Gpm;
use Grav\Common\Grav;
use Grav\Common\Cache;
use Grav\Console\ConsoleTrait;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
class IndexCommand extends Command {
use ConsoleTrait;
protected $input;
protected $ouput;
protected $data;
protected $argv;
protected $destination;
public function __construct(Grav $grav){
$this->grav = $grav;
// just for the gpm cli we force the filesystem driver cache
$this->grav['config']->set('system.cache.driver', 'default');
$this->argv = $_SERVER['argv'][0];
parent::__construct();
}
protected function configure() {
$this
->setName("index")
@@ -51,22 +37,11 @@ class IndexCommand extends Command {
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$this->setupConsole($input, $output);
$this->destination = realpath($this->input->getOption('destination'));
$this->setColors();
$fetchCommand = $this->getApplication()->find('fetch');
$args = new ArrayInput(array('command' => 'fetch', '-f' => $input->getOption('force')));
$commandExec = $fetchCommand->run($args, $output);
if ($commandExec != 0){
$output->writeln("<red>Error:</red> An error occured while trying to fetch data from <cyan>getgrav.org</cyan>");
exit;
}
$this->data = $this->grav['cache']->fetch(md5('cli:gpm'));
$this->data = $this->fetchData();
$this->output->writeln('');
@@ -91,17 +66,6 @@ class IndexCommand extends Command {
$this->output->writeln('');
}
private function setColors()
{
$this->output->getFormatter()->setStyle('normal', new OutputFormatterStyle('white'));
$this->output->getFormatter()->setStyle('yellow', new OutputFormatterStyle('yellow', null, array('bold')));
$this->output->getFormatter()->setStyle('red', new OutputFormatterStyle('red', null, array('bold')));
$this->output->getFormatter()->setStyle('cyan', new OutputFormatterStyle('cyan', null, array('bold')));
$this->output->getFormatter()->setStyle('green', new OutputFormatterStyle('green', null, array('bold')));
$this->output->getFormatter()->setStyle('magenta', new OutputFormatterStyle('magenta', null, array('bold')));
$this->output->getFormatter()->setStyle('white', new OutputFormatterStyle('white', null, array('bold')));
}
private function versionDetails($package)
{
$localVersion = @file_get_contents($this->destination.DS.$package->install_path.DS."VERSION");

View File

@@ -1,8 +1,7 @@
<?php
namespace Grav\Console\Gpm;
use Grav\Common\Grav;
use Grav\Common\Cache;
use Grav\Console\ConsoleTrait;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
@@ -12,21 +11,7 @@ use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
class InfoCommand extends Command {
protected $input;
protected $ouput;
protected $data;
protected $argv;
public function __construct(Grav $grav){
$this->grav = $grav;
// just for the gpm cli we force the filesystem driver cache
$this->grav['config']->set('system.cache.driver', 'default');
$this->argv = $_SERVER['argv'][0];
parent::__construct();
}
use ConsoleTrait;
protected function configure() {
$this
@@ -49,23 +34,10 @@ class InfoCommand extends Command {
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$this->setupConsole($input, $output);
$userPackage = false;
$this->setColors();
$fetchCommand = $this->getApplication()->find('fetch');
$args = new ArrayInput(array('command' => 'fetch', '-f' => $input->getOption('force')));
$commandExec = $fetchCommand->run($args, $output);
if ($commandExec != 0){
$output->writeln("<red>Error:</red> An error occured while trying to fetch data from <cyan>getgrav.org</cyan>");
exit;
}
$this->data = $this->grav['cache']->fetch(md5('cli:gpm'));
$this->data = $this->fetchData();
$this->output->writeln('');
@@ -109,14 +81,4 @@ class InfoCommand extends Command {
$this->output->writeln('');
}
private function setColors()
{
$this->output->getFormatter()->setStyle('normal', new OutputFormatterStyle('white'));
$this->output->getFormatter()->setStyle('red', new OutputFormatterStyle('red', null, array('bold')));
$this->output->getFormatter()->setStyle('cyan', new OutputFormatterStyle('cyan', null, array('bold')));
$this->output->getFormatter()->setStyle('green', new OutputFormatterStyle('green', null, array('bold')));
$this->output->getFormatter()->setStyle('magenta', new OutputFormatterStyle('magenta', null, array('bold')));
$this->output->getFormatter()->setStyle('white', new OutputFormatterStyle('white', null, array('bold')));
}
}

View File

@@ -1,8 +1,7 @@
<?php
namespace Grav\Console\Gpm;
use Grav\Common\Grav;
use Grav\Common\Cache;
use Grav\Console\ConsoleTrait;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Input\ArrayInput;
@@ -13,24 +12,12 @@ use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
class InstallCommand extends Command {
use ConsoleTrait;
protected $input;
protected $ouput;
protected $data;
protected $argv;
protected $destination;
protected $file;
public function __construct(Grav $grav){
$this->grav = $grav;
// just for the gpm cli we force the filesystem driver cache
$this->grav['config']->set('system.cache.driver', 'default');
$this->argv = $_SERVER['argv'][0];
parent::__construct();
}
protected function configure() {
$this
->setName("install")
@@ -64,8 +51,8 @@ class InstallCommand extends Command {
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$this->setupConsole($input, $output);
$this->destination = realpath($this->input->getOption('destination'));
$packages_to_install = array_map('strtolower', $this->input->getArgument('package'));
@@ -275,20 +262,6 @@ class InstallCommand extends Command {
}
}
private function fetchData()
{
$fetchCommand = $this->getApplication()->find('fetch');
$args = new ArrayInput(array('command' => 'fetch', '-f' => $this->input->getOption('force')));
$commandExec = $fetchCommand->run($args, $this->output);
if ($commandExec != 0){
$this->output->writeln("<red>Error:</red> An error occured while trying to fetch data from <cyan>getgrav.org</cyan>");
exit;
}
return $this->grav['cache']->fetch(md5('cli:gpm'));
}
private function getCurl($url)
{
$curl = curl_init();

View File

@@ -1,8 +1,7 @@
<?php
namespace Grav\Console\Gpm;
use Grav\Common\Grav;
use Grav\Common\Cache;
use Grav\Console\ConsoleTrait;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Input\ArrayInput;
@@ -13,26 +12,15 @@ use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
class UpdateCommand extends Command {
protected $input;
protected $ouput;
use ConsoleTrait;
protected $data;
protected $extensions;
protected $updatable;
protected $argv;
protected $destination;
protected $file;
protected $types = array('plugins', 'themes');
public function __construct(Grav $grav){
$this->grav = $grav;
// just for the gpm cli we force the filesystem driver cache
$this->grav['config']->set('system.cache.driver', 'default');
$this->argv = $_SERVER['argv'][0];
parent::__construct();
}
protected function configure() {
$this
->setName("update")
@@ -60,11 +48,10 @@ class UpdateCommand extends Command {
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->input = $input;
$this->output = $output;
$this->setupConsole($input, $output);
$this->destination = realpath($this->input->getOption('destination'));
$this->setColors();
$this->isGravRoot($this->destination);
// fetch remote data and scan for local extensions
@@ -125,17 +112,6 @@ class UpdateCommand extends Command {
}
}
private function setColors()
{
$this->output->getFormatter()->setStyle('normal', new OutputFormatterStyle('white'));
$this->output->getFormatter()->setStyle('red', new OutputFormatterStyle('red', null, array('bold')));
$this->output->getFormatter()->setStyle('yellow', new OutputFormatterStyle('yellow', null, array('bold')));
$this->output->getFormatter()->setStyle('cyan', new OutputFormatterStyle('cyan', null, array('bold')));
$this->output->getFormatter()->setStyle('green', new OutputFormatterStyle('green', null, array('bold')));
$this->output->getFormatter()->setStyle('magenta', new OutputFormatterStyle('magenta', null, array('bold')));
$this->output->getFormatter()->setStyle('white', new OutputFormatterStyle('white', null, array('bold')));
}
private function isGravRoot($path)
{
if (!file_exists($path)){
@@ -163,20 +139,6 @@ class UpdateCommand extends Command {
}
}
private function fetchData()
{
$fetchCommand = $this->getApplication()->find('fetch');
$args = new ArrayInput(array('command' => 'fetch', '-f' => $this->input->getOption('force')));
$commandExec = $fetchCommand->run($args, $this->output);
if ($commandExec != 0){
$this->output->writeln("<red>Error:</red> An error occured while trying to fetch data from <cyan>getgrav.org</cyan>");
exit;
}
return $this->grav['cache']->fetch(md5('cli:gpm'));
}
private function scanForExtensions()
{
$types = $this->types;