From 6b844ef12f65dd9966f728a00bf3924b94a56e2b Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Wed, 26 Sep 2012 10:57:19 -0500 Subject: [PATCH] Modify GithubFeed module to use ZF2 console - Removed bin/fetch.php - Added FetchController, which recreates logic of bin/fetch.php - Add factories for FetchController, GithubFeed\Renderer --- .vimproject | 4 +- module/GithubFeed/Module.php | 41 ++++++++- module/GithubFeed/autoload_classmap.php | 3 +- module/GithubFeed/bin/fetch.php | 25 ----- module/GithubFeed/config/module.config.php | 19 +++- .../src/GithubFeed/FetchController.php | 92 +++++++++++++++++++ 6 files changed, 151 insertions(+), 33 deletions(-) delete mode 100644 module/GithubFeed/bin/fetch.php create mode 100644 module/GithubFeed/src/GithubFeed/FetchController.php diff --git a/.vimproject b/.vimproject index 7d4e006f0a..76ff36ee81 100644 --- a/.vimproject +++ b/.vimproject @@ -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" { diff --git a/module/GithubFeed/Module.php b/module/GithubFeed/Module.php index a2c67458ad..212d98af75 100644 --- a/module/GithubFeed/Module.php +++ b/module/GithubFeed/Module.php @@ -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() { @@ -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', + ); + } } diff --git a/module/GithubFeed/autoload_classmap.php b/module/GithubFeed/autoload_classmap.php index defd12175a..3d587a886f 100644 --- a/module/GithubFeed/autoload_classmap.php +++ b/module/GithubFeed/autoload_classmap.php @@ -2,4 +2,5 @@ // Generated by ZF2's ./bin/classmap_generator.php return array( 'GithubFeed\AtomReader' => __DIR__ . '/src/GithubFeed/AtomReader.php', -); \ No newline at end of file + 'GithubFeed\FetchController' => __DIR__ . '/src/GithubFeed/FetchController.php', +); diff --git a/module/GithubFeed/bin/fetch.php b/module/GithubFeed/bin/fetch.php deleted file mode 100644 index c7fcef9040..0000000000 --- a/module/GithubFeed/bin/fetch.php +++ /dev/null @@ -1,25 +0,0 @@ -getServiceManager(); -$reader = $locator->get('GithubFeed\AtomReader'); - -try { - $data = $reader->read(); -} catch (Exception $e) { - echo "Error retrieving Github atom feed:\n"; - echo $e->getMessage(), "\n"; - exit(1); -} - -$model = new ViewModel(array( - 'github' => $data, -)); -$model->setTemplate('github-feed/links'); - -$renderer = $locator->get('Zend\View\Renderer\PhpRenderer'); -$content = $renderer->render($model); -file_put_contents('data/github-feed-links.phtml', $content); -echo "[DONE]"; diff --git a/module/GithubFeed/config/module.config.php b/module/GithubFeed/config/module.config.php index fc88e72fcf..2ccb684fe2 100644 --- a/module/GithubFeed/config/module.config.php +++ b/module/GithubFeed/config/module.config.php @@ -1,9 +1,10 @@ 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( @@ -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', + ), + ), + ), + ))), ); diff --git a/module/GithubFeed/src/GithubFeed/FetchController.php b/module/GithubFeed/src/GithubFeed/FetchController.php new file mode 100644 index 0000000000..6cefbf20e4 --- /dev/null +++ b/module/GithubFeed/src/GithubFeed/FetchController.php @@ -0,0 +1,92 @@ +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); + } +}