Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Merge pr #74 from branch 'samsonasik/contributor-page'
Browse files Browse the repository at this point in the history
* contributor-page:
  #74 : add note to CHANGELOG.md
  add assertion
  added test for consolecontroller
  patch on dispatch for layout choose
  inject the controller with Http Client
  fixes cs
  update .phpcs
  fixes travis build
  make $contributors pass to ContributorsController
  fix forgotten 2columns layout
  fix page
  update
  fixes cs
  patch cs
  remove left bar for contribution page using "skipWelcome" layout variable
  using Zend navigation to build nav menu
  added navigation
  added test for contributors page
  fixes cs
  contributor page
  • Loading branch information
samsonasik committed Dec 15, 2014
2 parents f2376c2 + 327f4ba commit 3ca63bf
Show file tree
Hide file tree
Showing 31 changed files with 548 additions and 76 deletions.
2 changes: 2 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ $config->fixers(
'elseif',
'eof_ending',
'unused_use',
'phpdoc_indent',
'multiline_array_trailing_comma'
)
);
$config->finder($finder);
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- [67: Doctrine configuration support](https://github.com/sitrunlab/LearnZF2/pull/67)
- [69: Db usage for module list](https://github.com/sitrunlab/LearnZF2/pull/69)
- [69: Pagination module](https://github.com/sitrunlab/LearnZF2/pull/70)

- [74: Contributors Page](https://github.com/sitrunlab/LearnZF2/pull/74)

## 0.0.2 - 2014-12-03

Expand Down
16 changes: 16 additions & 0 deletions config/autoload/navigation.global.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

return [
'navigation' => [
'default' => [
[
'label' => 'Home',
'route' => 'home',
],
[
'label' => 'Contributors',
'route' => 'contributors',
],
],
],
];
1 change: 1 addition & 0 deletions data/contributors/contributors.pson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a:3:{i:0;a:19:{s:5:"login";s:10:"samsonasik";s:2:"id";i:459648;s:10:"avatar_url";s:50:"https://avatars.githubusercontent.com/u/459648?v=3";s:11:"gravatar_id";s:0:"";s:3:"url";s:39:"https://api.github.com/users/samsonasik";s:8:"html_url";s:29:"https://github.com/samsonasik";s:13:"followers_url";s:49:"https://api.github.com/users/samsonasik/followers";s:13:"following_url";s:62:"https://api.github.com/users/samsonasik/following{/other_user}";s:9:"gists_url";s:55:"https://api.github.com/users/samsonasik/gists{/gist_id}";s:11:"starred_url";s:62:"https://api.github.com/users/samsonasik/starred{/owner}{/repo}";s:17:"subscriptions_url";s:53:"https://api.github.com/users/samsonasik/subscriptions";s:17:"organizations_url";s:44:"https://api.github.com/users/samsonasik/orgs";s:9:"repos_url";s:45:"https://api.github.com/users/samsonasik/repos";s:10:"events_url";s:56:"https://api.github.com/users/samsonasik/events{/privacy}";s:19:"received_events_url";s:55:"https://api.github.com/users/samsonasik/received_events";s:4:"type";s:4:"User";s:10:"site_admin";b:0;s:13:"contributions";i:168;s:9:"user_info";a:30:{s:5:"login";s:10:"samsonasik";s:2:"id";i:459648;s:10:"avatar_url";s:50:"https://avatars.githubusercontent.com/u/459648?v=3";s:11:"gravatar_id";s:0:"";s:3:"url";s:39:"https://api.github.com/users/samsonasik";s:8:"html_url";s:29:"https://github.com/samsonasik";s:13:"followers_url";s:49:"https://api.github.com/users/samsonasik/followers";s:13:"following_url";s:62:"https://api.github.com/users/samsonasik/following{/other_user}";s:9:"gists_url";s:55:"https://api.github.com/users/samsonasik/gists{/gist_id}";s:11:"starred_url";s:62:"https://api.github.com/users/samsonasik/starred{/owner}{/repo}";s:17:"subscriptions_url";s:53:"https://api.github.com/users/samsonasik/subscriptions";s:17:"organizations_url";s:44:"https://api.github.com/users/samsonasik/orgs";s:9:"repos_url";s:45:"https://api.github.com/users/samsonasik/repos";s:10:"events_url";s:56:"https://api.github.com/users/samsonasik/events{/privacy}";s:19:"received_events_url";s:55:"https://api.github.com/users/samsonasik/received_events";s:4:"type";s:4:"User";s:10:"site_admin";b:0;s:4:"name";s:18:"Abdul Malik Ikhsan";s:7:"company";s:19:"Sitrun Technologies";s:4:"blog";s:31:"http://samsonasik.wordpress.com";s:8:"location";s:7:"Bandung";s:5:"email";s:20:"samsonasik@gmail.com";s:8:"hireable";b:1;s:3:"bio";N;s:12:"public_repos";i:117;s:12:"public_gists";i:42;s:9:"followers";i:143;s:9:"following";i:55;s:10:"created_at";s:20:"2010-10-29T15:22:08Z";s:10:"updated_at";s:20:"2014-12-14T16:30:46Z";}}i:1;a:19:{s:5:"login";s:7:"acelaya";s:2:"id";i:2719332;s:10:"avatar_url";s:51:"https://avatars.githubusercontent.com/u/2719332?v=3";s:11:"gravatar_id";s:0:"";s:3:"url";s:36:"https://api.github.com/users/acelaya";s:8:"html_url";s:26:"https://github.com/acelaya";s:13:"followers_url";s:46:"https://api.github.com/users/acelaya/followers";s:13:"following_url";s:59:"https://api.github.com/users/acelaya/following{/other_user}";s:9:"gists_url";s:52:"https://api.github.com/users/acelaya/gists{/gist_id}";s:11:"starred_url";s:59:"https://api.github.com/users/acelaya/starred{/owner}{/repo}";s:17:"subscriptions_url";s:50:"https://api.github.com/users/acelaya/subscriptions";s:17:"organizations_url";s:41:"https://api.github.com/users/acelaya/orgs";s:9:"repos_url";s:42:"https://api.github.com/users/acelaya/repos";s:10:"events_url";s:53:"https://api.github.com/users/acelaya/events{/privacy}";s:19:"received_events_url";s:52:"https://api.github.com/users/acelaya/received_events";s:4:"type";s:4:"User";s:10:"site_admin";b:0;s:13:"contributions";i:35;s:9:"user_info";a:30:{s:5:"login";s:7:"acelaya";s:2:"id";i:2719332;s:10:"avatar_url";s:51:"https://avatars.githubusercontent.com/u/2719332?v=3";s:11:"gravatar_id";s:0:"";s:3:"url";s:36:"https://api.github.com/users/acelaya";s:8:"html_url";s:26:"https://github.com/acelaya";s:13:"followers_url";s:46:"https://api.github.com/users/acelaya/followers";s:13:"following_url";s:59:"https://api.github.com/users/acelaya/following{/other_user}";s:9:"gists_url";s:52:"https://api.github.com/users/acelaya/gists{/gist_id}";s:11:"starred_url";s:59:"https://api.github.com/users/acelaya/starred{/owner}{/repo}";s:17:"subscriptions_url";s:50:"https://api.github.com/users/acelaya/subscriptions";s:17:"organizations_url";s:41:"https://api.github.com/users/acelaya/orgs";s:9:"repos_url";s:42:"https://api.github.com/users/acelaya/repos";s:10:"events_url";s:53:"https://api.github.com/users/acelaya/events{/privacy}";s:19:"received_events_url";s:52:"https://api.github.com/users/acelaya/received_events";s:4:"type";s:4:"User";s:10:"site_admin";b:0;s:4:"name";s:16:"Alejandro Celaya";s:7:"company";s:20:"Wonnova gamification";s:4:"blog";s:30:"http://www.alejandrocelaya.com";s:8:"location";s:8:"Zaragoza";s:5:"email";s:0:"";s:8:"hireable";b:1;s:3:"bio";N;s:12:"public_repos";i:19;s:12:"public_gists";i:1;s:9:"followers";i:21;s:9:"following";i:36;s:10:"created_at";s:20:"2012-11-04T09:37:53Z";s:10:"updated_at";s:20:"2014-12-14T14:29:53Z";}}i:2;a:19:{s:5:"login";s:12:"mockiemockiz";s:2:"id";i:1708946;s:10:"avatar_url";s:51:"https://avatars.githubusercontent.com/u/1708946?v=3";s:11:"gravatar_id";s:0:"";s:3:"url";s:41:"https://api.github.com/users/mockiemockiz";s:8:"html_url";s:31:"https://github.com/mockiemockiz";s:13:"followers_url";s:51:"https://api.github.com/users/mockiemockiz/followers";s:13:"following_url";s:64:"https://api.github.com/users/mockiemockiz/following{/other_user}";s:9:"gists_url";s:57:"https://api.github.com/users/mockiemockiz/gists{/gist_id}";s:11:"starred_url";s:64:"https://api.github.com/users/mockiemockiz/starred{/owner}{/repo}";s:17:"subscriptions_url";s:55:"https://api.github.com/users/mockiemockiz/subscriptions";s:17:"organizations_url";s:46:"https://api.github.com/users/mockiemockiz/orgs";s:9:"repos_url";s:47:"https://api.github.com/users/mockiemockiz/repos";s:10:"events_url";s:58:"https://api.github.com/users/mockiemockiz/events{/privacy}";s:19:"received_events_url";s:57:"https://api.github.com/users/mockiemockiz/received_events";s:4:"type";s:4:"User";s:10:"site_admin";b:0;s:13:"contributions";i:33;s:9:"user_info";a:30:{s:5:"login";s:12:"mockiemockiz";s:2:"id";i:1708946;s:10:"avatar_url";s:51:"https://avatars.githubusercontent.com/u/1708946?v=3";s:11:"gravatar_id";s:0:"";s:3:"url";s:41:"https://api.github.com/users/mockiemockiz";s:8:"html_url";s:31:"https://github.com/mockiemockiz";s:13:"followers_url";s:51:"https://api.github.com/users/mockiemockiz/followers";s:13:"following_url";s:64:"https://api.github.com/users/mockiemockiz/following{/other_user}";s:9:"gists_url";s:57:"https://api.github.com/users/mockiemockiz/gists{/gist_id}";s:11:"starred_url";s:64:"https://api.github.com/users/mockiemockiz/starred{/owner}{/repo}";s:17:"subscriptions_url";s:55:"https://api.github.com/users/mockiemockiz/subscriptions";s:17:"organizations_url";s:46:"https://api.github.com/users/mockiemockiz/orgs";s:9:"repos_url";s:47:"https://api.github.com/users/mockiemockiz/repos";s:10:"events_url";s:58:"https://api.github.com/users/mockiemockiz/events{/privacy}";s:19:"received_events_url";s:57:"https://api.github.com/users/mockiemockiz/received_events";s:4:"type";s:4:"User";s:10:"site_admin";b:0;s:4:"name";s:13:"muhamad rifki";s:7:"company";s:0:"";s:4:"blog";s:10:"mockie.net";s:8:"location";s:9:"Indonesia";s:5:"email";s:25:"rifkimuhammad89@gmail.com";s:8:"hireable";b:1;s:3:"bio";N;s:12:"public_repos";i:6;s:12:"public_gists";i:19;s:9:"followers";i:3;s:9:"following";i:3;s:10:"created_at";s:20:"2012-05-05T16:33:57Z";s:10:"updated_at";s:20:"2014-12-08T16:18:53Z";}}}
27 changes: 20 additions & 7 deletions module/Application/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Application;

use Zend\Console\Adapter\AdapterInterface as Console;
use Zend\EventManager\EventInterface;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\BootstrapListenerInterface;
Expand Down Expand Up @@ -45,16 +46,18 @@ public function onDispatch(MvcEvent $e)
$routeMatch = $e->getRouteMatch();
$activeController = $routeMatch->getParam('controller');

if ($activeController != 'Application\Controller\Index') {
$controller = $e->getTarget();
$controller = $e->getTarget();
$controller->layout('layout/2columns');
$listController1Columns = [
'Application\Controller\Index',
'Application\Controller\Contributors',
];

if (!in_array($activeController, $listController1Columns) && !$e->getViewModel() instanceof JsonModel) {
$controller = $e->getTarget();
$controllerClass = get_class($controller);
$moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
if (!$e->getViewModel() instanceof JsonModel) {
$e->getViewModel()->setVariable('modulenamespace', $moduleNamespace);
}

$e->getViewModel()->setVariable('modulenamespace', $moduleNamespace);
$controller->layout('layout/2columns');
}
}

Expand All @@ -73,6 +76,16 @@ public function onRender(MvcEvent $e)
}
}

/**
* Get console usage description
*/
public function getConsoleUsage(Console $console)
{
return [
'get contributors' => 'get contributors list',
];
}

/**
* @return array|mixed|\Traversable
*/
Expand Down
29 changes: 28 additions & 1 deletion module/Application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,26 @@
],
],
],
'contributors' => [
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => [
'route' => '/contributors',
'defaults' => [
'controller' => 'Application\Controller\Contributors',
'action' => 'index',
],
],
],
],
],
'service_manager' => [
'abstract_factories' => [
'Zend\Cache\Service\StorageCacheAbstractServiceFactory',
'Zend\Log\LoggerAbstractServiceFactory',
],
'factories' => [
'navigation' => 'Zend\Navigation\Service\DefaultNavigationFactory',
],
'aliases' => [
'translator' => 'MvcTranslator',
],
Expand All @@ -85,6 +98,8 @@
'factories' => [
'Application\Controller\Download' => 'Application\Factory\Controller\DownloadControllerFactory',
'Application\Controller\Index' => 'Application\Factory\Controller\IndexControllerFactory',
'Application\Controller\Contributors' => 'Application\Factory\Controller\ContributorsControllerFactory',
'Application\Controller\Console' => 'Application\Factory\Controller\ConsoleControllerFactory',
],
],
'view_helpers' => [
Expand All @@ -108,10 +123,22 @@
__DIR__.'/../view',
],
],
// Placeholder for console routes
'console' => [
'contributors' => [
'output' => 'data/contributors/contributors.pson',
],
'router' => [
'routes' => [
'contributors' => [
'type' => 'Simple',
'options' => [
'route' => 'get contributors',
'defaults' => [
'controller' => 'Application\Controller\Console',
'action' => 'getcontributors',
],
],
],
],
],
],
Expand Down
111 changes: 111 additions & 0 deletions module/Application/src/Application/Controller/ConsoleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractConsoleController;
use Zend\Console\Adapter\AdapterInterface as Console;
use Zend\Console\ColorInterface as Color;
use Zend\Http\Client as HttpClient;

/**
* Console controller to get contributor list
* inspired and almost all code copied from https://github.com/zendframework/zf-web/blob/master/module/Application/src/Application/Controller/ConsoleController.php
*/
class ConsoleController extends AbstractConsoleController
{
/**
* @var Console
*/
protected $console;

/**
* @var array
*/
protected $config;

/**
* @var HttpClient
*/
protected $httpClient;

/**
* Construct console and config property
*/
public function __construct(Console $console, array $config, HttpClient $httpClient)
{
$this->console = $console;
$this->config = $config;

$this->httpClient = $httpClient;
}

protected function reportError($width, $length, $message, $e = null)
{
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($message);
if ($e) {
$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);
}

/**
* route : get contributors
*/
public function getcontributorsAction()
{
$width = $this->console->getWidth();
$this->console->writeLine('Fetching GitHub Contributors', Color::GREEN);

$response = $this->httpClient->send();
if (!$response->isSuccess()) {
// report failure
$message = $response->getStatusCode().': '.$response->getReasonPhrase();
$this->reportError($width, 0, $message);

return;
}

$body = $response->getBody();
$contributors = json_decode($body, true);
$total = count($contributors);

foreach ($contributors as $i => $contributor) {
$message = sprintf(' Processing %d/%d', $i, $total);
$this->console->write($message);
$this->httpClient->setUri("https://api.github.com/users/{$contributor['login']}");
$response = $this->httpClient->send();
if (!$response->isSuccess()) {
// report failure
$error = $response->getStatusCode().': '.$response->getReasonPhrase();
$this->reportError($width, strlen($message), $error);
}
$body = $response->getBody();
$userInfo = json_decode($body, 1);
$contributors[$i]['user_info'] = $userInfo;
$this->reportSuccess($width, strlen($message));
}

$this->console->writeLine(str_repeat('-', $width));
$message = 'Writing file';
$this->console->write($message, Color::BLUE);
$path = $this->config['console']['contributors']['output'];
file_put_contents($path, serialize($contributors));
$this->reportSuccess($width, strlen($message));
$this->console->writeLine(sprintf('File written to %s', $path));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

class ContributorsController extends AbstractActionController
{
/**
* @var array
*/
protected $contributors;

/**
* Construct contributors property
*/
public function __construct(array $contributors)
{
$this->contributors = $contributors;
}

public function indexAction()
{
$this->layout('layout/1columns.phtml');
$this->layout()->setVariable('skipWelcome', true);

return new ViewModel([
'contributors' => $this->contributors,
]);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Application\Controller;

Expand Down Expand Up @@ -33,7 +26,8 @@ public function __construct(array $modulesList)

public function indexAction()
{
$this->layout('layout/home.phtml');
$this->layout('layout/1columns.phtml');
$this->layout()->setVariable('skipWelcome', false);

$paginator = new Paginator(new ArrayAdapter($this->modulesList));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Application\Factory\Controller;

use Application\Controller\ConsoleController;
use Zend\Http\Client as HttpClient;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class ConsoleControllerFactory implements FactoryInterface
{
/**
* {@inheritDoc}
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$services = $serviceLocator->getServiceLocator();
$client = new HttpClient();
$client->setAdapter('Zend\Http\Client\Adapter\Curl');
$client->setUri('https://api.github.com/repos/sitrunlab/LearnZF2/contributors');

return new ConsoleController(
$services->get('Console'),
$services->get('Config'),
$client
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Application\Factory\Controller;

use Application\Controller\ContributorsController;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class ContributorsControllerFactory implements FactoryInterface
{
/**
* {@inheritDoc}
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
return new ContributorsController(unserialize(file_get_contents('./data/contributors/contributors.pson')));
}
}
Loading

0 comments on commit 3ca63bf

Please sign in to comment.