{
protected function loadParameters()
{
- $params = $GLOBALS['argv'];
- array_shift($params);
- $storeInto = null;
- foreach ($params as $param) {
- if ($storeInto !== null) {
- $this->config->$storeInto = (int)$param;
- $storeInto = null;
- continue;
- }
+ $parser = parent::loadParameters();
+ //set default renderer to console
+ $parser->options['renderer']->default = 'console';
- if ($param == '--days-after' || $param == '-a') {
- $storeInto = 'daysAfter';
- continue;
- } else if ($param == '--days-before' || $param == '-b') {
- $storeInto = 'daysBefore';
- continue;
- }
- $storeInto = null;
- }
- }
-
- protected function render($arEvents)
- {
- $r = new Renderer_Mail();
- $r->config = $this->config;
- $r->ansi = true;
- echo $r->render($arEvents);
+ return $parser;
}
}
?>
--- /dev/null
+<?php
+namespace bdrem;
+
+abstract class Renderer
+{
+ protected $httpContentType = null;
+
+ public function renderAndOutput($arEvents)
+ {
+ if (PHP_SAPI != 'cli' && $this->httpContentType !== null) {
+ header('Content-type: ' . $this->httpContentType);
+ }
+ echo $this->render($arEvents);
+ }
+
+ abstract public function render($arEvents);
+}
+?>
<?php
namespace bdrem;
-class Renderer_Console
+class Renderer_Console extends Renderer
{
+ protected $httpContentType = 'text/plain; charset=utf-8';
+
/**
* Use ANSI color codes for output coloring
*
public function render($arEvents)
{
+ $this->loadConfig();
if ($this->ansi) {
$this->cc = new \Console_Color2();
}
}
return $data;
}
+
+ protected function loadConfig()
+ {
+ if (isset($this->config->ansi)) {
+ $this->ansi = $this->config->ansi;
+ }
+ }
}
?>
<?php
namespace bdrem;
-class Renderer_Html
+class Renderer_Html extends Renderer
{
+ protected $httpContentType = 'application/xhtml+xml; charset=utf-8';
+
public function render($arEvents)
{
$tr = new Renderer_HtmlTable();
<?php
namespace bdrem;
-class Renderer_HtmlTable
+class Renderer_HtmlTable extends Renderer
{
+ protected $httpContentType = 'text/html; charset=utf-8';
+
public function render($arEvents)
{
$s = <<<HTM
require_once 'Mail/mime.php';
-class Renderer_Mail
+class Renderer_Mail extends Renderer
{
public function render($arEvents)
{
setlocale(LC_TIME, $this->config->locale);
$source = $this->config->loadSource();
- $this->loadParameters($this->config);
+ $parser = $this->loadParameters();
+ $this->parseParameters($parser);
+
$arEvents = $source->getEvents(
$this->config->date,
$this->config->daysBefore, $this->config->daysAfter
protected function loadParameters()
{
+ $parser = new \Console_CommandLine();
+ $parser->description = 'Birthday reminder';
+ $parser->version = '0.1.0';
+
+ $parser->addOption(
+ 'daysAfter',
+ array(
+ 'short_name' => '-a',
+ 'long_name' => '--days-after',
+ 'description' => 'Show NUM days after date',
+ 'help_name' => 'NUM',
+ 'action' => 'StoreInt',
+ 'default' => $this->config->daysAfter,
+ )
+ );
+ $parser->addOption(
+ 'daysBefore',
+ array(
+ 'short_name' => '-b',
+ 'long_name' => '--days-before',
+ 'description' => 'Show NUM days before date',
+ 'help_name' => 'NUM',
+ 'action' => 'StoreInt',
+ 'default' => $this->config->daysBefore,
+ )
+ );
+ $parser->addOption(
+ 'renderer',
+ array(
+ 'short_name' => '-r',
+ 'long_name' => '--renderer',
+ 'description' => 'Output mode',
+ 'action' => 'StoreString',
+ 'choices' => array(
+ 'console',
+ 'html',
+ 'htmltable',
+ 'mail',
+ ),
+ 'default' => 'console',
+ 'add_list_option' => true,
+ )
+ );
+ $parser->addOption(
+ 'quiet',
+ array(
+ 'short_name' => '-q',
+ 'long_name' => '--quiet',
+ 'description' => "Don't print status messages to stdout",
+ 'action' => 'StoreTrue'
+ )
+ );
+ return $parser;
+ }
+
+ protected function parseParameters($parser)
+ {
+ try {
+ $result = $parser->parse();
+ // do something with the result object
+ $this->config->daysAfter = $result->options['daysAfter'];
+ $this->config->daysBefore = $result->options['daysBefore'];
+ $this->config->renderer = $result->options['renderer'];
+ $this->config->quiet = $result->options['quiet'];
+ } catch (\Exception $exc) {
+ $this->preRenderParameterError();
+ $parser->displayError($exc->getMessage());
+ }
}
- abstract protected function render($arEvents);
+ protected function render($arEvents)
+ {
+ $r = $this->getRenderer();
+ $r->config = $this->config;
+ $r->renderAndOutput($arEvents);
+ }
+
+ protected function getRenderer()
+ {
+ $renderer = ucfirst($this->config->renderer);
+ if ($renderer == 'Htmltable') {
+ $renderer = 'HtmlTable';
+ }
+ $class = '\\bdrem\\Renderer_' . $renderer;
+ return new $class();
+ }
+
+ protected function preRenderParameterError()
+ {
+ }
}
?>
\ No newline at end of file
class Web extends UserInterface
{
- protected function render($arEvents)
+ protected function loadParameters()
{
- $r = new Renderer_Html();
- echo $r->render($arEvents);
+ $parser = parent::loadParameters();
+ //set default renderer to html
+ $parser->options['renderer']->default = 'html';
+
+ return $parser;
}
- protected function loadParameters()
+ protected function preRenderParameterError()
{
- if (isset($_GET['daysBefore'])) {
- $this->config->daysBefore = (int) $_GET['daysBefore'];
- }
- if (isset($_GET['daysAfter'])) {
- $this->config->daysAfter = (int) $_GET['daysAfter'];
- }
+ header('Content-type: text/plain; charset=utf-8');
}
}
?>
class WebText extends Web
{
- protected function render($arEvents)
+ protected function loadParameters()
{
- header('Content-type: text/plain; charset=utf-8');
- $r = new Renderer_Console();
- echo $r->render($arEvents);
+ $parser = parent::loadParameters();
+ //set default renderer to console
+ $parser->options['renderer']->default = 'console';
+
+ return $parser;
}
}
?>