1
0
mirror of synced 2024-12-12 22:36:02 +03:00

[2.0] Implemented base of CLI tasks. Also included drafts of Version and Help tasks. Implemented two Printers: Normal and ANSI Color.

This commit is contained in:
guilhermeblanco 2009-08-23 20:27:02 +00:00
parent b1d34fca1c
commit 111e94ec3e
7 changed files with 533 additions and 0 deletions

View File

@ -0,0 +1,104 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Tools\Cli;
use Doctrine\ORM\Tools\Cli\Style;
abstract class AbstractPrinter
{
protected $_stream;
protected $_maxColumnSize;
protected $_styles;
public function __construct($stream = STDOUT)
{
$this->_stream = $stream;
$this->_maxColumnSize = 80;
$this->_initStyles();
}
protected function _initStyles()
{
// Defines base styles
$this->addStyles(array(
'ERROR' => new Style(),
'INFO' => new Style(),
'COMMENT' => new Style(),
'HEADER' => new Style(),
'NONE' => new Style(),
));
}
public function addStyles($styles)
{
foreach ($styles as $name => $style) {
$this->addStyle($name, $style);
}
}
public function addStyle($name, Style $style)
{
$this->_styles[strtoupper($name)] = $style;
}
public function getStyle($name)
{
$name = strtoupper($name);
return isset($this->_styles[$name]) ? $this->_styles[$name] : null;
}
/**
* Sets the maximum column size (defines the CLI margin).
*
* @param integer $maxColumnSize The maximum column size for a message
*/
public function setMaxColumnSize($maxColumnSize)
{
$this->_maxColumnSize = $maxColumnSize;
}
/**
* Writes to output stream the message, formatting it by applying the defined style.
*
* @param string $message Message to be outputted
* @param mixed $style Optional style to be applied in message
*/
public function write($message, $style = 'ERROR')
{
$style = is_string($style) ? $this->getStyle($style) : $style;
fwrite($this->_stream, $this->format($message, $style));
}
/**
* Formats the given message with the defined style.
*
* @param string $message Message to be formatted
* @param mixed $style Style to be applied in message
* @return string Formatted message
*/
abstract public function format($message, Style $style);
}

View File

@ -0,0 +1,58 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Tools\Cli;
use Doctrine\ORM\Tools\Cli\AbstractPrinter;
abstract class AbstractTask
{
protected $_printer;
protected $_arguments;
public function setPrinter(AbstractPrinter $printer)
{
$this->_printer = $printer;
}
public function getPrinter()
{
return $this->_printer;
}
public function setArguments($arguments)
{
$this->_arguments = $arguments;
}
public function getArguments()
{
return $this->_arguments;
}
abstract public function extendedHelp();
abstract public function basicHelp();
abstract public function validate();
abstract public function run();
}

View File

@ -0,0 +1,167 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Tools\Cli\Printer;
use Doctrine\ORM\Tools\Cli\AbstractPrinter,
Doctrine\ORM\Tools\Cli\Style;
class AnsiColor extends AbstractPrinter
{
protected function _initStyles()
{
$this->addStyles(array(
'ERROR' => new Style('RED', 'WHITE', array('BOLD' => true)),
'INFO' => new Style('GREEN', 'DEFAULT', array('BOLD' => true)),
'COMMENT' => new Style('YELLOW', 'DEFAULT'),
'HEADER' => new Style('DEFAULT', 'DEFAULT', array('BOLD' => true)),
'NONE' => new Style(),
));
}
public function format($message, Style $style)
{
if ( ! $this->_supportsColor()) {
return $message;
}
$str = $this->getForegroundString($style->getForeground())
. $this->getBackgroundString($style->getBackground())
. $this->getOptionsString($style->getOptions());
$styleSet = ($str != '');
return $str . $message . ($styleSet ? chr(27) . '[0m' : '');
}
public function getForegroundString($foreground)
{
if (empty($foreground)) {
return '';
}
$esc = chr(27);
switch ($foreground) {
case 'BLACK':
return $esc . '[40m';
case 'RED':
return $esc . '[41m';
case 'GREEN':
return $esc . '[42m';
case 'YELLOW':
return $esc . '[43m';
case 'BLUE':
return $esc . '[44m';
case 'MAGENTA':
return $esc . '[45m';
case 'CYAN':
return $esc . '[46m';
case 'WHITE':
return $esc . '[47m';
case 'DEFAULT':
default:
return $esc . '[48m';
}
}
public function getBackgroundString($background)
{
if (empty($background)) {
return '';
}
$esc = chr(27);
switch ($background) {
case 'BLACK':
return $esc . '[30m';
case 'RED':
return $esc . '[31m';
case 'GREEN':
return $esc . '[32m';
case 'YELLOW':
return $esc . '[33m';
case 'BLUE':
return $esc . '[34m';
case 'MAGENTA':
return $esc . '[35m';
case 'CYAN':
return $esc . '[36m';
case 'WHITE':
return $esc . '[37m';
case 'DEFAULT_FGU':
return $esc . '[38m';
case 'DEFAULT':
default:
return $esc . '[39m';
}
}
public function getOptionsString($options)
{
if (empty($options)) {
return '';
}
$esc = chr(27);
$str = '';
foreach ($options as $name => $value) {
if ($value) {
$name = strtoupper($name);
switch ($name) {
case 'BOLD':
$str .= $esc . '[1m';
break;
case 'HALF':
$str .= $esc . '[2m';
break;
case 'UNDERLINE':
$str .= $esc . '[4m';
break;
case 'BLINK':
$str .= $esc . '[5m';
break;
case 'REVERSE':
$str .= $esc . '[7m';
break;
case 'CONCEAL':
$str .= $esc . '[8m';
break;
default:
// Ignore unknown option
break;
}
}
}
return $str;
}
private function _supportsColor()
{
return DIRECTORY_SEPARATOR != '\\' &&
function_exists('posix_isatty') &&
@posix_isatty($this->_stream);
}
}

View File

@ -0,0 +1,33 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Tools\Cli\Printer;
use Doctrine\ORM\Tools\Cli\AbstractPrinter,
Doctrine\ORM\Tools\Cli\Style;
class Normal extends AbstractPrinter
{
public function format($message, Style $style)
{
return $message;
}
}

View File

@ -0,0 +1,53 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Tools\Cli;
class Style
{
private $_background;
private $_foreground;
private $_options = array();
public function __construct($foreground = null, $background = null, $options = array())
{
$this->_foreground = strtoupper($foreground);
$this->_background = strtoupper($background);
$this->_options = $options;
}
public function getForeground()
{
return $this->_foreground;
}
public function getBackground()
{
return $this->_background;
}
public function getOptions()
{
return $this->_options;
}
}

View File

@ -0,0 +1,59 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Tools\Cli\Task;
use Doctrine\ORM\Tools\Cli\AbstractTask;
class Help extends AbstractTask
{
public function extendedHelp()
{
$this->getPrinter()->write('help extended help' . PHP_EOL, 'HEADER');
$this->getPrinter()->write('help extended help' . PHP_EOL, 'ERROR');
$this->getPrinter()->write('help extended help' . PHP_EOL, 'INFO');
$this->getPrinter()->write('help extended help' . PHP_EOL, 'COMMENT');
$this->getPrinter()->write('help extended help' . PHP_EOL, 'NONE');
}
public function basicHelp()
{
$this->getPrinter()->write('help basic help' . PHP_EOL, 'HEADER');
$this->getPrinter()->write('help basic help' . PHP_EOL, 'ERROR');
$this->getPrinter()->write('help basic help' . PHP_EOL, 'INFO');
$this->getPrinter()->write('help basic help' . PHP_EOL, 'COMMENT');
$this->getPrinter()->write('help basic help' . PHP_EOL, 'NONE');
}
public function validate()
{
return true;
}
public function run()
{
$this->getPrinter()->write('help run' . PHP_EOL, 'HEADER');
$this->getPrinter()->write('help run' . PHP_EOL, 'ERROR');
$this->getPrinter()->write('help run' . PHP_EOL, 'INFO');
$this->getPrinter()->write('help run' . PHP_EOL, 'COMMENT');
$this->getPrinter()->write('help run' . PHP_EOL, 'NONE');
}
}

View File

@ -0,0 +1,59 @@
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\ORM\Tools\Cli\Task;
use Doctrine\ORM\Tools\Cli\AbstractTask;
class Version extends AbstractTask
{
public function extendedHelp()
{
$this->getPrinter()->write('version extended help' . PHP_EOL, 'HEADER');
$this->getPrinter()->write('version extended help' . PHP_EOL, 'ERROR');
$this->getPrinter()->write('version extended help' . PHP_EOL, 'INFO');
$this->getPrinter()->write('version extended help' . PHP_EOL, 'COMMENT');
$this->getPrinter()->write('version extended help' . PHP_EOL, 'NONE');
}
public function basicHelp()
{
$this->getPrinter()->write('version basic help' . PHP_EOL, 'HEADER');
$this->getPrinter()->write('version basic help' . PHP_EOL, 'ERROR');
$this->getPrinter()->write('version basic help' . PHP_EOL, 'INFO');
$this->getPrinter()->write('version basic help' . PHP_EOL, 'COMMENT');
$this->getPrinter()->write('version basic help' . PHP_EOL, 'NONE');
}
public function validate()
{
return true;
}
public function run()
{
$this->getPrinter()->write('version run' . PHP_EOL, 'HEADER');
$this->getPrinter()->write('version run' . PHP_EOL, 'ERROR');
$this->getPrinter()->write('version run' . PHP_EOL, 'INFO');
$this->getPrinter()->write('version run' . PHP_EOL, 'COMMENT');
$this->getPrinter()->write('version run' . PHP_EOL, 'NONE');
}
}