Skip to content

Commit

Permalink
Documentation and command line tools
Browse files Browse the repository at this point in the history
- Documented all functions and classes - Added php-cli-tools to interact
with the cli
  • Loading branch information
Andreas Creten committed Sep 10, 2011
1 parent f6ec437 commit 1e2b9ee
Show file tree
Hide file tree
Showing 10 changed files with 656 additions and 181 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
.DS_Store
3 changes: 3 additions & 0 deletions .gitmodules
@@ -0,0 +1,3 @@
[submodule "php-cli-tools"]
path = php-cli-tools
url = git://github.com/jlogsdon/php-cli-tools.git
77 changes: 71 additions & 6 deletions class-wp-cli-command.php
@@ -1,22 +1,87 @@
<?php

class WP_CLI_Command {
function __construct($args) {
/**
* Base class for WP-CLI commands
*
* @package wp-cli
* @author Andreas Creten
*/
abstract class WP_CLI_Command {
/**
* Construct for this class, transfers the cli arguments to the right class
*
* @param Array $args
* @author Andreas Creten
*/
function __construct($args = array()) {
// The first command is the sub command
$sub_command = array_shift($args);

// If the method exists, try to load it
if(method_exists($this, $sub_command)) {
$this->$sub_command($args);
}
// Otherwise, show the help for this command
else {
$this->help($args);
}
}

public function help() {
print_r(get_class_methods($this));
/**
* General help function for this command
*
* @param Array $args
* @return void
* @author Andreas Creten
*/
public function help($args = array()) {
// Get the cli arguments
$arguments = $GLOBALS['argv'];

// Remove the first entry
array_shift($arguments);

// Get the command
$used_command = array_shift($arguments);

// Show the list of sub-commands for this command
WP_CLI::line('Example usage:');
WP_CLI::out(' wp '.$used_command);
$methods = WP_CLI_Command::getMethods($this);
if(!empty($methods)) {
WP_CLI::out(' ['.implode('|', $methods).']');
}
WP_CLI::line(' ...');
WP_CLI::line();

// Send a warning to the user because there is no custom help function defined in the command
// Make usure there always is a help method in your command class
WP_CLI::warning('The command has no dedicated help function, ask the creator to fix it.');
}

protected function _echo($string) {
echo $string."\n";
/**
* Get the filtered list of methods for a class
*
* @param string $class
* @return Array The list of methods
* @author Andreas Creten
*/
static function getMethods($class) {
// Methods that don't need to be included in the method list
$blacklist = array('__construct', 'getMethods');

// Get all the methods of the class
$methods = get_class_methods($class);

// Remove the blacklisted methods
foreach($blacklist as $method) {
$in_array = array_search($method, $methods);
if($in_array !== false) {
unset($methods[$in_array]);
}
}

// Only return the values, to fill up the gaps
return array_values($methods);
}
}
183 changes: 182 additions & 1 deletion class-wp-cli.php
@@ -1,9 +1,190 @@
<?php

/**
* Wrapper class for WP-CLI
*
* @package wp-cli
* @author Andreas Creten
*/
class WP_CLI {
static $commands = array();


/**
* Add a command to the wp-cli list of commands
*
* @param string $name The name of the command that will be used in the cli
* @param string $class The class to manage the command
* @return void
* @author Andreas Creten
*/
public function addCommand($name, $class) {
self::$commands[$name] = $class;
}

/**
* Display a message in the cli
*
* @param string $message
* @return void
* @author Andreas Creten
*/
static function out($message) {
\cli\out($message);
}

/**
* Display a message in the CLI and end with a newline
*
* @param string $message
* @return void
* @author Andreas Creten
*/
static function line($message = '') {
\cli\line($message);
}

/**
* Display an error in the CLI and end with a newline
*
* @param string $message
* @param string $label
* @return void
* @author Andreas Creten
*/
static function error($message, $label = 'Error') {
\cli\line('%R'.$label.': %n'.self::errorToString($message));
}

/**
* Display a success in the CLI and end with a newline
*
* @param string $message
* @param string $label
* @return void
* @author Andreas Creten
*/
static function success($message, $label = 'Success') {
\cli\line('%G'.$label.': %n'.$message);
}

/**
* Display a warning in the CLI and end with a newline
*
* @param string $message
* @param string $label
* @return void
* @author Andreas Creten
*/
static function warning($message, $label = 'Warning') {
\cli\line('%C'.$label.': %n'.$message);
}

/**
* Convert a wp_error into a String
*
* @param mixed $errors
* @return string
* @author Andreas Creten
*/
static function errorToString($errors) {
if(is_string($errors)){
return $errors;
}
elseif(is_wp_error($errors) && $errors->get_error_code()){
foreach($errors->get_error_messages() as $message){
if($errors->get_error_data() )
return $message . ' ' . $errors->get_error_data();
else
return $message;
}
}
}

/**
* Display the help function for the wp-cli
*
* @return void
* @author Andreas Creten
*/
static function generalHelp() {
self::line('Example usage:');
foreach(self::$commands as $name => $command) {
self::out(' wp '.$name);
$methods = WP_CLI_Command::getMethods($command);
if(!empty($methods)) {
self::out(' ['.implode('|', $methods).']');
}
self::line(' ...');
}
}
}

/**
* A Upgrader Skin for Wordpress that only generates plain-text
*
* @package wp-cli
* @author Andreas Creten
*/
class CLI_Upgrader_Skin {
var $upgrader;
var $done_header = false;
var $result = false;

function __construct($args = array()) {
$defaults = array('url' => '', 'nonce' => '', 'title' => '', 'context' => false);
$this->options = wp_parse_args($args, $defaults);
}

function set_upgrader(&$upgrader) {
if(is_object($upgrader)) {
$this->upgrader =& $upgrader;
}

$this->add_strings();
}

function add_strings() {}

function set_result($result) {
$this->result = $result;
}

function request_filesystem_credentials($error = false) {
$url = $this->options['url'];
$context = $this->options['context'];
if(!empty($this->options['nonce'])) {
$url = wp_nonce_url($url, $this->options['nonce']);
}

return request_filesystem_credentials($url, '', $error, $context); //Possible to bring inline, Leaving as is for now.
}

function header() {}
function footer() {}

function error($errors) {
$this->feedback(WP_CLI::errorToString($errors));
}

function feedback($string) {
if(isset( $this->upgrader->strings[$string]))
$string = $this->upgrader->strings[$string];

if(strpos($string, '%') !== false) {
$args = func_get_args();
$args = array_splice($args, 1);
if(!empty($args)) {
$string = vsprintf($string, $args);
}

}
if(empty($string)) {
return;
}

echo $string;
}

function before() {}
function after() {}
}
27 changes: 22 additions & 5 deletions commands/core.php → commands/internals/core.php
@@ -1,28 +1,45 @@
<?php

// Add the command to the wp-cli
WP_CLI::addCommand('core', 'CoreCommand');

/**
* Implement core command
*
* @package wp-cli
* @subpackage commands/internals
* @author Andreas Creten
*/
class CoreCommand extends WP_CLI_Command {
/**
* Update the Wordpress core
*
* @param string $args
* @return void
* @author Andreas Creten
*/
function update($args) {
echo 'Updating the core: ';
WP_CLI::line('Updating the Wordpress core.');

if(!class_exists('Core_Upgrader')) {
require_once(ABSPATH.'wp-admin/includes/class-wp-upgrader.php');
}
ob_start();
$upgrader = new Core_Upgrader(new CLI_Upgrader_Skin);
$result = $upgrader->upgrade($current);
$feedback = ob_get_clean();
$this->_echo($feedback);

// Borrowed verbatim from wp-admin/update-core.php
if(is_wp_error($result) ) {
$this->_echo(error_to_string($result));
if('up_to_date' != $result->get_error_code()) {
$this->_echo('Installation Failed');
WP_CLI::error('Installation failed ('.WP_CLI::errorToString($result).').');
}
else {
WP_CLI::success(WP_CLI::errorToString($result));
}
}
else {
$this->_echo('WordPress upgraded successfully');
WP_CLI::success('WordPress upgraded successfully.');
}
}
}
47 changes: 47 additions & 0 deletions commands/internals/home.php
@@ -0,0 +1,47 @@
<?php

// Add the command to the wp-cli
WP_CLI::addCommand('home', 'HomeCommand');

/**
* Implement home command
*
* @package wp-cli
* @subpackage commands/internals
* @author Andreas Creten
*/
class HomeCommand extends WP_CLI_Command {
/**
* Overwrite the construct to have a command without subcommand
*
* @param string $args
* @author Andreas Creten
*/
function __construct($args) {
if(empty($args)) {
// Open the wp-cli page in the browser
exec('open https://github.com/andreascreten/wp-cli');

WP_CLI::success('The wp-cli homepage should be opening in your browser.');
}
else {
// Call the parent constructor
parent::__construct($args);
}
}

/**
* Help function for this command
*
* @param string $args
* @return void
* @author Andreas Creten
**/

public function help($args = array()) {
WP_CLI::line('This command has no arguments, when called it will open the wp-cli homepage in your browser.');
WP_CLI::line();
WP_CLI::line('Example usage:');
WP_CLI::line(' wp home');
}
}

0 comments on commit 1e2b9ee

Please sign in to comment.