| 1 |
<?php |
| 2 |
|
| 3 |
class SM_CLI_Scaffold { |
| 4 |
|
| 5 |
/** |
| 6 |
* Storage for dynamic properties |
| 7 |
* Used by magic __set, __get |
| 8 |
* |
| 9 |
* @protected |
| 10 |
* @type array |
| 11 |
*/ |
| 12 |
protected $_properties = array(); |
| 13 |
|
| 14 |
/** |
| 15 |
* @param $args |
| 16 |
* @param $assoc_args |
| 17 |
*/ |
| 18 |
public function __construct( $args, $assoc_args ) { |
| 19 |
if ( php_sapi_name() != 'cli' ) { |
| 20 |
die('Must run from command line'); |
| 21 |
} |
| 22 |
|
| 23 |
$this->args = $args; |
| 24 |
$this->assoc_args = $assoc_args; |
| 25 |
foreach( $assoc_args as $k => $v ) { |
| 26 |
$this->{$k} = $v; |
| 27 |
} |
| 28 |
|
| 29 |
/* Set default Limit */ |
| 30 |
$this->limit = is_numeric( $this->limit ) && $this->limit > 0 ? $this->limit : 100; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Forces data printing to command line ignoring buffer. |
| 35 |
* |
| 36 |
* @param string $msg |
| 37 |
* @return null |
| 38 |
*/ |
| 39 |
public function output( $msg = '' ) { |
| 40 |
$args = $this->assoc_args; |
| 41 |
if( !isset( $args['log'] ) ) return null; |
| 42 |
esc_html_e(date( 'H:i:s', time() ) . ': ' . $msg . ' ' . $this->memory_usage() . PHP_EOL); |
| 43 |
@ob_flush(); |
| 44 |
flush(); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Returns Memory Usage information. |
| 49 |
*/ |
| 50 |
public function memory_usage() { |
| 51 |
$args = $this->assoc_args; |
| 52 |
if( !isset( $args['memory-usage'] ) ) return null; |
| 53 |
static $last_usage = 0; |
| 54 |
$differences = $last_usage ? number_format( ( memory_get_usage() / 1024 / 1024 ) - $last_usage, 3 ) . 'Mb' : 'none'; |
| 55 |
$current_usage = number_format( $last_usage = memory_get_usage() / 1024 / 1024, 3 ) . 'Mb'; |
| 56 |
return sprintf( "Memory Usage: %s. Diff: %s.", $current_usage, $differences ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Returns domain of current blog. |
| 61 |
* |
| 62 |
*/ |
| 63 |
public function get_current_blog_domain() { |
| 64 |
$url = get_home_url(); |
| 65 |
$pieces = parse_url( $url ); |
| 66 |
$domain = isset( $pieces[ 'host' ] ) ? $pieces['host'] : false; |
| 67 |
return $domain; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @param $key |
| 72 |
* |
| 73 |
* @return null |
| 74 |
*/ |
| 75 |
public function __get( $key ) { |
| 76 |
return isset( $this->_properties[ $key ] ) ? $this->_properties[ $key ] : NULL; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @param $key |
| 81 |
* @param $value |
| 82 |
*/ |
| 83 |
public function __set( $key, $value ) { |
| 84 |
$this->_properties[ $key ] = $value; |
| 85 |
} |
| 86 |
|
| 87 |
} |