Skip to content

Commit

Permalink
Fixed console usage
Browse files Browse the repository at this point in the history
- 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
weierophinney committed Sep 26, 2012
1 parent 6e512c5 commit 41e2dab
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 227 deletions.
67 changes: 63 additions & 4 deletions Module.php
Expand Up @@ -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
{
Expand All @@ -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(
Expand All @@ -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);
Expand Down
27 changes: 11 additions & 16 deletions README.md
Expand Up @@ -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
Expand All @@ -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:

Expand Down Expand Up @@ -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
----
Expand Down
168 changes: 0 additions & 168 deletions bin/compile.php

This file was deleted.

19 changes: 9 additions & 10 deletions config/module.config.php
Expand Up @@ -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',
),
),
)),
),
)),
),
);

0 comments on commit 41e2dab

Please sign in to comment.