<?php
namespace bdrem;
-class Cli
+class Cli extends UserInterface
{
- public function run()
+ protected function loadParameters($cfg)
{
- $cfg = new Config();
- $cfg->load();
- setlocale(LC_TIME, $cfg->locale);
- $source = $cfg->loadSource();
+ $params = $GLOBALS['argv'];
+ array_shift($params);
+ $storeInto = null;
+ foreach ($params as $param) {
+ if ($storeInto !== null) {
+ $cfg->$storeInto = (int)$param;
+ $storeInto = null;
+ continue;
+ }
- $arEvents = $source->getEvents(
- date('Y-m-d'), $cfg->daysBefore, $cfg->daysAfter
- );
- usort($arEvents, '\\bdrem\\Event::compare');
- $this->render($arEvents);
+ if ($param == '--days-after' || $param == '-a') {
+ $storeInto = 'daysAfter';
+ continue;
+ } else if ($param == '--days-before' || $param == '-b') {
+ $storeInto = 'daysBefore';
+ continue;
+ }
+ $storeInto = null;
+ }
}
- public function render($arEvents)
+ protected function render($arEvents)
{
$r = new Renderer_Console();
echo $r->render($arEvents);
--- /dev/null
+<?php
+namespace bdrem;
+
+abstract class UserInterface
+{
+ public function run()
+ {
+ $cfg = new Config();
+ $cfg->load();
+ setlocale(LC_TIME, $cfg->locale);
+ $source = $cfg->loadSource();
+
+ $this->loadParameters($cfg);
+ $arEvents = $source->getEvents(
+ date('Y-m-d'), $cfg->daysBefore, $cfg->daysAfter
+ );
+ usort($arEvents, '\\bdrem\\Event::compare');
+ $this->render($arEvents);
+ }
+
+ protected function loadParameters($cfg)
+ {
+ }
+
+ abstract protected function render($arEvents);
+}
+?>
\ No newline at end of file
<?php
namespace bdrem;
-class Web
+class Web extends UserInterface
{
- public function run()
+ protected function render($arEvents)
{
- $cfg = new Config();
- $cfg->load();
- setlocale(LC_TIME, $cfg->locale);
- $source = $cfg->loadSource();
-
- $arEvents = $source->getEvents(
- date('Y-m-d'), $cfg->daysBefore, $cfg->daysAfter
- );
- usort($arEvents, '\\bdrem\\Event::compare');
- $this->render($arEvents);
+ $r = new Renderer_Html();
+ echo $r->render($arEvents);
}
- public function render($arEvents)
+ protected function loadParameters($cfg)
{
- $r = new Renderer_Html();
- echo $r->render($arEvents);
+ if (isset($_GET['daysBefore'])) {
+ $cfg->daysBefore = (int) $_GET['daysBefore'];
+ }
+ if (isset($_GET['daysAfter'])) {
+ $cfg->daysAfter = (int) $_GET['daysAfter'];
+ }
}
}
?>
class WebText extends Web
{
- public function render($arEvents)
+ protected function render($arEvents)
{
header('Content-type: text/plain; charset=utf-8');
$r = new Renderer_Console();