Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed console usage

- Created a "BlogRenderer" service, which sets up a PhpRenderer similar to how
  it's setup on the Http\ViewManager currently. This can be removed once ZF2
  allows retrieving the HttpViewManager or ViewPhpRenderer directly as a
  service.
- Translated short options to long options before returning them.
- Inject the view, instead of instantiating directly
- Created a factory for compiler controller
- Remove the old compile script
- Update the README
  • Loading branch information...
commit 41e2dabd57d4c323b6c01289c334bb8231302d52 1 parent 6e512c5
@weierophinney weierophinney authored
View
67 Module.php
@@ -4,8 +4,12 @@
use Traversable;
use Zend\Console\Adapter\AdapterInterface as Console;
+use Zend\Http\PhpEnvironment\Request;
+use Zend\Http\PhpEnvironment\Response;
use Zend\ModuleManager\Feature\ConsoleUsageProviderInterface;
use Zend\Stdlib\ArrayUtils;
+use Zend\View\Renderer\PhpRenderer;
+use Zend\View\View;
class Module implements ConsoleUsageProviderInterface
{
@@ -25,6 +29,64 @@ public function getConfig()
return include __DIR__ . '/config/module.config.php';
}
+ public function getServiceConfig()
+ {
+ return array('factories' => array(
+ 'blogrequest' => function ($services) {
+ return new Request();
+ },
+ 'blogresponse' => function ($services) {
+ return new Response();
+ },
+ 'blogrenderer' => function ($services) {
+ $helpers = $services->get('ViewHelperManager');
+ $resolver = $services->get('ViewResolver');
+
+ $renderer = new PhpRenderer();
+ $renderer->setHelperPluginManager($helpers);
+ $renderer->setResolver($resolver);
+
+ $config = $services->get('Config');
+ if ($services->has('MvcEvent')) {
+ $event = $services->get('MvcEvent');
+ $model = $event->getViewModel();
+ } else {
+ $model = new Model\ViewModel();
+ }
+ $layout = 'layout/layout';
+ if (isset($config['view_manager']['layout'])) {
+ $layout = $config['view_manager']['layout'];
+ }
+ $model->setTemplate($layout);
+ $helpers->get('view_model')->setRoot($model);
+
+ return $renderer;
+ },
+ ));
+ }
+
+ public function getControllerConfig()
+ {
+ return array('factories' => array(
+ 'PhlyBlog\CompileController' => function ($controllers) {
+ $services = $controllers->getServiceLocator();
+ $config = $services->get('Config');
+ $config = isset($config['blog']) ? $config['blog'] : array();
+
+ $request = $services->get('BlogRequest');
+ $response = $services->get('BlogResponse');
+ $view = new View();
+ $view->setRequest($request);
+ $view->setResponse($response);
+
+ $controller = new CompileController();
+ $controller->setConfig($config);
+ $controller->setView($view);
+ return $controller;
+ },
+ ));
+ }
+
public function getConsoleUsage(Console $console)
{
return array(
@@ -46,14 +108,11 @@ public function onBootstrap($e)
$app = $e->getApplication();
$services = $app->getServiceManager();
self::$config = $services->get('config');
- if (self::$config instanceof Traversable) {
- self::$config = ArrayUtils::iteratorToArray(self::$config);
- }
}
public static function prepareCompilerView($view, $config, $services)
{
- $renderer = $services->get('ViewRenderer');
+ $renderer = $services->get('BlogRenderer');
$view->addRenderingStrategy(function($e) use ($renderer) {
return $renderer;
}, 100);
View
27 README.md
@@ -67,8 +67,15 @@ Important things to note:
Usage
=====
-A script, `bin/compile.php`, is shipped for your convenience, and it will
-generate the following artifacts:
+This module is ZF2 Console-aware. Once installed and active in your
+application, you should be able to run:
+
+```bash
+% php public/index.php
+```
+
+and see usage for the module. Currently, it defines a "blog compile" action
+that can generate the following artifacts:
* A file per entry
* Paginated entry files
@@ -80,18 +87,6 @@ generate the following artifacts:
* Atom and/or RSS feeds for recent entries by tag
* Optionally, a tag cloud
-The script, makes the following assumptions:
-
-* They are being called by another script that:
- * sets up one or more autoloaders, including functionality to autoload the
- code in this library
- * compiles and merges all application configuration
- * bootstraps the application
- * retains the Application instance in the current scope
-
-Basically, a script that does normal bootstrapping, but without calling `run()`
-or `send()` on the Application instance.
-
You will want to setup local configuration; I recommend putting it in
`config/autoload/module.blog.config.global.php`. As a sample:
@@ -203,8 +198,8 @@ You will want to setup local configuration; I recommend putting it in
)),
));
-When you run the script, it will generate files in the locations you specify in
-your configuration.
+When you run the command line tool, it will generate files in the locations you
+specify in your configuration.
License
----
View
168 bin/compile.php
@@ -1,168 +0,0 @@
-<?php
-use PhlyBlog\Module;
-use PhlyBlog\Compiler;
-use PhlyBlog\CompilerOptions;
-use PhlyBlog\Compiler\Listener;
-use PhlyBlog\Compiler\ResponseFile;
-use PhlyBlog\Compiler\ResponseStrategy;
-use Zend\Console\Exception as GetoptException;
-use Zend\Console\Getopt;
-
-// Options
-// Assumes that $argv is already in scope
-try {
- $options = new Getopt(array(
- 'help|h' => 'Print this usage message',
- 'all|a' => 'Execute all actions (default)',
- 'entries|e' => 'Compile entries',
- 'archive|c' => 'Compile paginated archive (and feed)',
- 'year|y' => 'Compile paginated entries by year',
- 'month|m' => 'Compile paginated entries by month',
- 'day|d' => 'Compile paginated entries by day',
- 'tag|t' => 'Compile paginated entries by tag (and feeds)',
- 'author|r' => 'Compile paginated entries by author (and feeds)',
- ), $argv);
-} catch (GetoptException $e) {
- file_put_contents('php://stderr', $e->getUsageMessage());
-}
-
-if ($options->getOption('h')) {
- echo $options->getUsageMessage();
- exit(0);
-}
-
-$all = true;
-$entries = false;
-$archive = false;
-$byYear = false;
-$byMonth = false;
-$byDay = false;
-$byTag = false;
-$byAuthor = false;
-
-if (!isset($options->a)
- && (isset($options->e)
- || isset($options->c)
- || isset($options->y)
- || isset($options->m)
- || isset($options->d)
- || isset($options->t)
- || isset($options->r)
- )
-) {
- $all = false;
- if (isset($options->e)) {
- $entries = true;
- }
- if (isset($options->c)) {
- $archive = true;
- }
- if (isset($options->y)) {
- $byYear = true;
- }
- if (isset($options->m)) {
- $byMonth = true;
- }
- if (isset($options->d)) {
- $byDay = true;
- }
- if (isset($options->t)) {
- $byTag = true;
- }
- if (isset($options->r)) {
- $byAuthor = true;
- }
-}
-
-// Get locator, and grab renderer and view from it
-$config = Module::$config;
-$locator = $application->getServiceManager();
-$view = $locator->get('View');
-
-$view->setRequest($application->getRequest());
-$view->setResponse($application->getResponse());
-$view->getEventManager()->clearListeners('renderer');
-$view->getEventManager()->clearListeners('response');
-
-// Setup renderer for layout, and layout view model
-if ($config['blog']['view_callback'] && is_callable($config['blog']['view_callback'])) {
- $callable = $config['blog']['view_callback'];
- call_user_func($callable, $view, $config, $locator);
-}
-
-// Prepare compiler and grab tag cloud
-$writer = new Compiler\FileWriter();
-$responseFile = new ResponseFile();
-$responseStrategy = new ResponseStrategy($writer, $responseFile, $view);
-
-$options = new CompilerOptions($config['blog']['options']);
-
-$postFiles = new Compiler\PhpFileFilter($config['blog']['posts_path']);
-$compiler = new Compiler($postFiles);
-
-$listeners = array();
-$tags = new Listener\Tags($view, $writer, $responseFile, $options);
-$compiler->getEventManager()->attach($tags);
-
-if ($all || $entries) {
- $entries = new Listener\Entries($view, $responseFile, $options);
- $compiler->getEventManager()->attach($entries);
- $listeners['entries'] = $entries;
-}
-
-if ($all || $archive) {
- $archive = new Listener\Archives($view, $writer, $responseFile, $options);
- $compiler->getEventManager()->attach($archive);
- $listeners['archives'] = $archive;
-}
-
-if ($all || $byYear) {
- $byYear = new Listener\ByYear($view, $writer, $responseFile, $options);
- $compiler->getEventManager()->attach($byYear);
- $listeners['entries by year'] = $byYear;
-}
-
-if ($all || $byMonth) {
- $byMonth = new Listener\ByMonth($view, $writer, $responseFile, $options);
- $compiler->getEventManager()->attach($byMonth);
- $listeners['entries by month'] = $byMonth;
-}
-
-if ($all || $byDay) {
- $byDay = new Listener\ByDate($view, $writer, $responseFile, $options);
- $compiler->getEventManager()->attach($byDay);
- $listeners['entries by day'] = $byDay;
-}
-
-if ($all || $byAuthor) {
- $byAuthor = new Listener\Authors($view, $writer, $responseFile, $options);
- $compiler->getEventManager()->attach($byAuthor);
- $listeners['entries by author'] = $byAuthor;
-}
-
-if ($all || $byTag) {
- $listeners['entries by tag'] = $tags;
-}
-
-// Compile
-echo "Compiling and sorting entries...";
-$compiler->compile();
-echo "DONE!\n";
-
-// Create tag cloud
-if ($config['blog']['cloud_callback']
- && is_callable($config['blog']['cloud_callback'])
-) {
- $callable = $config['blog']['cloud_callback'];
- echo "Creating and rendering tag cloud...";
- $cloud = $tags->getTagCloud();
- call_user_func($callable, $cloud, $view, $config, $locator);
- echo "DONE!\n";
-}
-
-// compile various artifacts
-foreach ($listeners as $type => $listener) {
- echo "Compiling " . $type . "...";
- $listener->compile();
- echo "DONE!\n";
-}
View
19 config/module.config.php
@@ -189,19 +189,18 @@
),
),
- 'controllers' => array(
- 'PhlyBlog\CompileController' => 'PhlyBlog\CompileController',
- ),
-
'console' => array(
'router' => array('routes' => array(
- 'phly-blog-compile' => array('options' => array(
- 'route' => 'blog compile [--all|-a] [--entries|-e] [--archive|-c] [--year|-y] [--month|-m] [--day|-d] [--tag|-t] [--author|-r]',
- 'defaults' => array(
- 'controller' => 'PhlyBlog\CompileController',
- 'action' => 'compile',
+ 'phly-blog-compile' => array(
+ 'type' => 'Simple',
+ 'options' => array(
+ 'route' => 'blog compile [--all|-a] [--entries|-e] [--archive|-c] [--year|-y] [--month|-m] [--day|-d] [--tag|-t] [--author|-r]',
+ 'defaults' => array(
+ 'controller' => 'PhlyBlog\CompileController',
+ 'action' => 'compile',
+ ),
),
- )),
+ ),
)),
),
);
View
74 src/PhlyBlog/CompileController.php
@@ -4,16 +4,16 @@
use RuntimeException;
use Zend\EventManager\EventManagerInterface;
use Zend\Mvc\Controller\AbstractActionController;
-use Zend\Stdlib\ArrayUtils;
use Zend\View\View;
class CompileController extends AbstractActionController
{
public $config = array();
+ public $view;
+ protected $compiler;
protected $compilerOptions;
protected $responseFile;
- protected $view;
protected $writer;
protected $defaultOptions = array(
@@ -49,7 +49,7 @@ public function setEventManager(EventManagerInterface $events)
$config = $controller->config;
if ($config['view_callback'] && is_callable($config['view_callback'])) {
$callable = $config['view_callback'];
- $view = $controller->getView();
+ $view = $controller->view;
$locator = $controller->getServiceLocator();
call_user_func($callable, $view, $config, $locator);
}
@@ -58,30 +58,46 @@ public function setEventManager(EventManagerInterface $events)
public function getFlags()
{
- $options = $this->params();
+ $options = $this->params()->fromRoute();
+ $test = array(
+ array('long' => 'all', 'short' => 'a'),
+ array('long' => 'entries', 'short' => 'e'),
+ array('long' => 'archive', 'short' => 'c'),
+ array('long' => 'year', 'short' => 'y'),
+ array('long' => 'month', 'short' => 'm'),
+ array('long' => 'day', 'short' => 'd'),
+ array('long' => 'tag', 'short' => 't'),
+ array('long' => 'author', 'short' => 'r'),
+ );
+ foreach ($test as $spec) {
+ $long = $spec['long'];
+ $short = $spec['short'];
+ if ((!isset($options[$long]) || !$options[$long])
+ && (isset($options[$short]) && $options[$short])
+ ) {
+ $options[$long] = true;
+ unset($options[$short]);
+ }
+ }
+
$options = array_merge($this->defaultOptions, $options);
- if ($options['entries']
- || $options['archive']
- || $options['year']
- || $options['month']
- || $options['day']
- || $options['tag']
+ if ($options['entries']
+ || $options['archive']
+ || $options['year']
+ || $options['month']
+ || $options['day']
+ || $options['tag']
|| $options['author']
) {
$options['all'] = false;
}
+
return $options;
}
- public function getView()
+ public function setView(View $view)
{
- if ($this->view) {
- return $this->view;
- }
- $this->view = $view = new View();
- $view->setRequest($this->getRequest);
- $view->setResponse($this->getResponse);
- return $view;
+ $this->view = $view;
}
public function getWriter()
@@ -104,12 +120,12 @@ public function getResponseFile()
public function getCompilerOptions()
{
- if ($this->options) {
- return $this->options;
+ if ($this->compilerOptions) {
+ return $this->compilerOptions;
}
- $this->options = new CompilerOptions($this->config['options']);
- return $this->options;
+ $this->compilerOptions = new CompilerOptions($this->config['options']);
+ return $this->compilerOptions;
}
public function getCompiler()
@@ -118,11 +134,11 @@ public function getCompiler()
return $this->compiler;
}
- $view = $this->getView();
+ $view = $this->view;
$writer = $this->getWriter();
$responseFile = $this->getResponseFile();
$responseStrategy = new Compiler\ResponseStrategy($writer, $responseFile, $view);
- $postFiles = new Compiler\PhpFileFilter($config['posts_path']);
+ $postFiles = new Compiler\PhpFileFilter($this->config['posts_path']);
$this->compiler = new Compiler($postFiles);
return $this->compiler;
@@ -130,15 +146,15 @@ public function getCompiler()
public function attachTags()
{
- $tags = new Compiler\Listener\Tags($this->getView(), $this->getWriter(), $this->getResponseFile(), $this->getCompilerOptions());
+ $tags = new Compiler\Listener\Tags($this->view, $this->getWriter(), $this->getResponseFile(), $this->getCompilerOptions());
$this->getCompiler()->getEventManager()->attach($tags);
return $tags;
}
- public function attachListeners(array $flags)
+ public function attachListeners(array $flags, $tags)
{
$listeners = array();
- $view = $this->getView();
+ $view = $this->view;
$compiler = $this->getCompiler();
$writer = $this->getWriter();
$responseFile = $this->getResponseFile();
@@ -192,7 +208,7 @@ public function compileAction()
$flags = $this->getFlags();
$compiler = $this->getCompiler();
$tags = $this->attachTags();
- $listeners = $this->attachListeners($flags);
+ $listeners = $this->attachListeners($flags, $tags);
// Compile
echo "Compiling and sorting entries...";
@@ -206,7 +222,7 @@ public function compileAction()
$callable = $this->config['cloud_callback'];
echo "Creating and rendering tag cloud...";
$cloud = $tags->getTagCloud();
- call_user_func($callable, $cloud, $this->getView(), $this->config, $this->getServiceLocator());
+ call_user_func($callable, $cloud, $this->view, $this->config, $this->getServiceLocator());
echo "DONE!\n";
}
Please sign in to comment.
Something went wrong with that request. Please try again.