Skip to content

Commit

Permalink
Merge branch 'feature/github-console' into develop
Browse files Browse the repository at this point in the history
Creates ZF2 console-awareness in GithubFeed module
  • Loading branch information
weierophinney committed Sep 26, 2012
2 parents 28e4a05 + 6b844ef commit 94593a0
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 33 deletions.
4 changes: 1 addition & 3 deletions .vimproject
Expand Up @@ -115,15 +115,13 @@ mwop=/home/matthew/git/mwop CD=. filter="README TODO.md console *.php *.xml *.tx
autoload_function.php
autoload_register.php
Module.php
bin=bin{
fetch.php
}
config=config filter="*.ini *.xml *.php *.php.dist" {
module.config.php
}
src=src {
GithubFeed=GithubFeed {
AtomReader.php
FetchController.php
}
}
view=view filter="*.mustache *.phtml" {
Expand Down
41 changes: 40 additions & 1 deletion module/GithubFeed/Module.php
Expand Up @@ -2,10 +2,13 @@

namespace GithubFeed;

use Zend\Console\Adapter\AdapterInterface as Console;
use Zend\Feed\Reader\Reader as FeedReader;
use Zend\Http\Client as HttpClient;
use Zend\ModuleManager\Feature\ConsoleUsageProviderInterface;
use Zend\View\Renderer\PhpRenderer;

class Module
class Module implements ConsoleUsageProviderInterface
{
public function getAutoloaderConfig()
{
Expand Down Expand Up @@ -44,6 +47,42 @@ public function getServiceConfig()
}
return $reader;
},
'GithubFeed\Renderer' => function ($services) {
$helpers = $services->get('ViewHelperManager');
$resolver = $services->get('ViewResolver');

$renderer = new PhpRenderer();
$renderer->setHelperPluginManager($helpers);
$renderer->setResolver($resolver);

return $renderer;
},
));
}

public function getControllerConfig()
{
return array('factories' => array(
'GithubFeed\Fetch' => function ($controllers) {
$services = $controllers->getServiceLocator();
$config = $services->get('Config');
$config = $config['github_feed'];

$controller = new FetchController();
$controller->setConsole($services->get('Console'));
$controller->setFeedFile($config['content_path']);
$controller->setReader($services->get('GithubFeed\AtomReader'));
$controller->setRenderer($services->get('GithubFeed\Renderer'));

return $controller;
},
));
}

public function getConsoleUsage(Console $console)
{
return array(
'githubfeed fetch' => 'Fetch and cache Github activity',
);
}
}
3 changes: 2 additions & 1 deletion module/GithubFeed/autoload_classmap.php
Expand Up @@ -2,4 +2,5 @@
// Generated by ZF2's ./bin/classmap_generator.php
return array(
'GithubFeed\AtomReader' => __DIR__ . '/src/GithubFeed/AtomReader.php',
);
'GithubFeed\FetchController' => __DIR__ . '/src/GithubFeed/FetchController.php',
);
25 changes: 0 additions & 25 deletions module/GithubFeed/bin/fetch.php

This file was deleted.

19 changes: 16 additions & 3 deletions module/GithubFeed/config/module.config.php
@@ -1,9 +1,10 @@
<?php
return array(
'github_feed' => array(
'user' => 'github username here',
'token' => 'github API token here',
'limit' => 5,
'user' => 'github username here',
'token' => 'github API token here',
'limit' => 5,
'content_path' => 'data/github-feed-links.phtml',
),
'view_manager' => array(
'template_map' => array(
Expand All @@ -13,4 +14,16 @@
'github-feed' => __DIR__ . '/../view',
),
),
'console' => array('router' => array('routes' => array(
'github-feed-fetch' => array(
'type' => 'Simple',
'options' => array(
'route' => 'githubfeed fetch',
'defaults' => array(
'controller' => 'GithubFeed\Fetch',
'action' => 'feed',
),
),
),
))),
);
92 changes: 92 additions & 0 deletions module/GithubFeed/src/GithubFeed/FetchController.php
@@ -0,0 +1,92 @@
<?php
namespace GithubFeed;

use Zend\Console\Adapter\AdapterInterface as Console;
use Zend\Console\ColorInterface as Color;
use Zend\Console\Request as ConsoleRequest;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Renderer\RendererInterface as Renderer;

class FetchController extends AbstractActionController
{
protected $console;
protected $feedFile;
protected $reader;
protected $renderer;

public function setConsole(Console $console)
{
$this->console = $console;
}

public function setFeedFile($path)
{
$this->feedFile = $path;
}

public function setReader(AtomReader $reader)
{
$this->reader = $reader;
}

public function setRenderer(Renderer $renderer)
{
$this->renderer = $renderer;
}

public function feedAction()
{
$request = $this->getRequest();
if (!$request instanceof ConsoleRequest) {
throw RuntimeException(sprintf(
'%s can only be run via the Console',
__METHOD__
));
}

$message = 'Retrieving Github activity links';
$length = strlen($message);
$width = $this->console->getWidth();

$this->console->write($message, Color::BLUE);

try {
$data = $this->reader->read();
} catch (\Exception $e) {
$this->reportError($width, $length, $e);
return;
}

$model = new ViewModel(array(
'github' => $data,
));
$model->setTemplate('github-feed/links');

$content = $this->renderer->render($model);
file_put_contents($this->feedFile, $content);
// file_put_contents('data/github-feed-links.phtml', $content);
$this->reportSuccess($width, $length);
}

protected function reportError($width, $length, $e)
{
if (($length + 9) > $width) {
$this->console->writeLine('');
$length = 0;
}
$spaces = $width - $length - 9;
$this->console->writeLine(str_repeat('.', $spaces) . '[ ERROR ]', Color::RED);
$this->console->writeLine($e->getTraceAsString());
}

protected function reportSuccess($width, $length)
{
if (($length + 8) > $width) {
$this->console->writeLine('');
$length = 0;
}
$spaces = $width - $length - 8;
$this->console->writeLine(str_repeat('.', $spaces) . '[ DONE ]', Color::GREEN);
}
}

0 comments on commit 94593a0

Please sign in to comment.