From 320c58be50a6bca82e29f7f7e8a91e36501aa93f Mon Sep 17 00:00:00 2001 From: Markus Birth Date: Thu, 4 Aug 2016 15:35:50 +0200 Subject: [PATCH] Split Output into Output(Generator) and Transport. Needed for later switching between different mail transports. --- index.php | 6 ++++-- lib/Frs/Output/GenericOutput.php | 13 +++++++++++-- lib/Frs/Output/HtmlOutput.php | 4 ---- lib/Frs/Output/OutputInterface.php | 8 ++++++++ lib/Frs/Output/Transport/StdoutTransport.php | 18 ++++++++++++++++++ .../Output/Transport/TransportInterface.php | 10 ++++++++++ 6 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 lib/Frs/Output/OutputInterface.php create mode 100644 lib/Frs/Output/Transport/StdoutTransport.php create mode 100644 lib/Frs/Output/Transport/TransportInterface.php diff --git a/index.php b/index.php index 511f047..9149447 100644 --- a/index.php +++ b/index.php @@ -6,8 +6,10 @@ use \Frs\FieldDefinition; use \Frs\SessionManager; use \Frs\Output\HtmlOutput; use \Frs\Output\MailOutput; +use \Frs\Output\Transport\StdoutTransport; -$ho = new HtmlOutput(dirname(__FILE__) . '/templates'); +$stdout = new StdoutTransport(); +$ho = new HtmlOutput($stdout, dirname(__FILE__) . '/templates'); $ho->setTemplateVar('session_time_left', 0); $sm = new SessionManager(); @@ -125,4 +127,4 @@ if (!$tpl_done && $sm->hasSessionToken()) { $ho->setTemplate('index_html'); } -$ho->sendOutputToStdout(); +$ho->send(); diff --git a/lib/Frs/Output/GenericOutput.php b/lib/Frs/Output/GenericOutput.php index d60c0dd..a1a5206 100644 --- a/lib/Frs/Output/GenericOutput.php +++ b/lib/Frs/Output/GenericOutput.php @@ -2,8 +2,9 @@ namespace Frs\Output; -class GenericOutput +class GenericOutput implements OutputInterface { + private $transport; private $templatesPath; private $partialsPath; private $templateEngine; @@ -13,11 +14,13 @@ class GenericOutput /** * Creates new output object for generic output. * + * @param \Frs\Output\Transport\TransportInterface Transport to send output to * @param string $templatesPath Path to templates. Must be a folder, no slash at end! * @param string $partialsPath Path to partials (relative to $templatesPath). Must be a folder, no slash at end! */ - public function __construct($templatesPath = 'templates', $partialsPath = 'partials') + public function __construct(\Frs\Output\Transport\TransportInterface $transport, $templatesPath = 'templates', $partialsPath = 'partials') { + $this->transport = $transport; $this->templatesPath = $templatesPath; $this->partialsPath = $templatesPath . DIRECTORY_SEPARATOR . $partialsPath; $this->templateEngine = new \Mustache_Engine(array( @@ -53,4 +56,10 @@ class GenericOutput { return $this->template->render($this->templateVars); } + + public function send() + { + $this->transport->setContent($this->getRenderedOutput()); + $this->transport->transmit(); + } } diff --git a/lib/Frs/Output/HtmlOutput.php b/lib/Frs/Output/HtmlOutput.php index d1ef4b7..fa5bdad 100644 --- a/lib/Frs/Output/HtmlOutput.php +++ b/lib/Frs/Output/HtmlOutput.php @@ -6,8 +6,4 @@ use \Frs\Output\GenericOutput; class HtmlOutput extends GenericOutput { - public function sendOutputToStdout() - { - echo $this->getRenderedOutput(); - } } diff --git a/lib/Frs/Output/OutputInterface.php b/lib/Frs/Output/OutputInterface.php new file mode 100644 index 0000000..689033e --- /dev/null +++ b/lib/Frs/Output/OutputInterface.php @@ -0,0 +1,8 @@ +content = $content; + } + + public function transmit() + { + echo $this->content; + } +} diff --git a/lib/Frs/Output/Transport/TransportInterface.php b/lib/Frs/Output/Transport/TransportInterface.php new file mode 100644 index 0000000..b210593 --- /dev/null +++ b/lib/Frs/Output/Transport/TransportInterface.php @@ -0,0 +1,10 @@ +