Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: a97b7e31ec
heartsentwined
file 147 lines (128 sloc) 3.552 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
<?php
namespace Heartsentwined\Cli;

use Heartsentwined\ArgValidator\ArgValidator;
use Zend\Console\Console;
use Zend\Console\Adapter\AdapterInterface;

class Cli
{
    protected $console;
    protected $templates = array();

    /**
* setConsole
*
* @param Console $console
* @return self
*/
    public function setConsole(AdapterInterface $console)
    {
        $this->console = $console;

        return $this;
    }

    /**
* getConsole
*
* @return Console
*/
    public function getConsole()
    {
        return $this->console;
    }

    /**
* register templates
*
* @param array $templates
* (code) => $template
* @see self::addTemplate()
* @return self
*/
    public function setTemplates(array $templates)
    {
        foreach ($templates as $code => $template) {
            $this->addTemplate($code, $template);
        }

        return $this;
    }

    /**
* getTemplates
*
* @return array
*/
    public function getTemplates()
    {
        return $this->templates;
    }

    /**
* register a template
*
* @param string $code the template identifier / code
* @param array $template
* 'template' => string (optional)
* %s as text placeholder,
* not set = '%s' (plain text)
* 'color' => string (optional)
* a \Zend\Console\ColorInterface defined constant name,
* not set = default / normal color
* @return self
*/
    public function addTemplate($code, array $template)
    {
        ArgValidator::assert($code, 'string');
        ArgValidator::arrayAssert($template, array(
            'template' => array('string', 'notSet'),
            'color' => array('string', 'notSet'),
        ));
        $this->templates[$code] = $template;

        return $this;
    }

    /**
* output text with template
*
* @param string $text
* @param string $code the template code
* @param bool $echo = true, if false, will return print string instead
* @return void|string
*/
    public function write($text, $code, $echo = true)
    {
        ArgValidator::assert($text, 'string');
        ArgValidator::assert($code, 'string');

        $templates = $this->getTemplates();
        if (!isset($templates[$code])) {
            throw new Exception\InvalidArgumentException(sprintf(
                'the template "%s" is not defined',
                $code
            ));
        }
        $template = $templates[$code];

        if (!$echo) ob_start();

        if (!$this->getConsole()) $this->initConsole();

        $console = $this->getConsole();
        if ($console && isset($template['color'])
            && defined("Zend\Console\ColorInterface::{$template['color']}")) {
            $console->setColor(
                constant("Zend\Console\ColorInterface::{$template['color']}"));
        }

        if (isset($template['template'])) {
            printf("\n{$template['template']}\n", $text);
        } else {
            echo "\n$text\n";
        }

        if ($console) $console->resetColor();
        if (!$echo) return ob_get_clean();
    }

    /**
* try to initiate console instance if not set
*
* @return self
*/
    public function initConsole()
    {
        try {
            if ($console = Console::getInstance()) {
                $this->setConsole($console);
            }
        } catch (\Exception $e) {
        }

        return $this;
    }
}
Something went wrong with that request. Please try again.