Skip to content
This repository was archived by the owner on May 1, 2019. It is now read-only.

[WIP] [RFC] Implement Packagist-style module add process #352

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions config/application.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
'modules' => [
'ZF\DevelopmentMode',
'AssetManager',
'EdpModuleLayouts',
'ZfcBase',
'ZfcUser',
'ScnSocialAuth',
'EdpGithub',
'Application',
'User',
'EdpModuleLayouts',
'ZfModule',
'User',
],
'module_listener_options' => [
'config_glob_paths' => [
Expand Down
3 changes: 2 additions & 1 deletion config/autoload/global.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php
return [
'module_layouts' => [
'ZfcUser' => 'layout/layout-small-header.phtml',
'ZfcUser' => 'layout/layout-small-header.phtml',
'User' => 'layout/layout-small-header.phtml',
'ZfModule' => 'layout/layout-small-header.phtml',
],
'asset_manager' => [
Expand Down
2 changes: 1 addition & 1 deletion config/autoload/zfcuser.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
* Accepted values: A valid route name within your application
*
*/
//'login_redirect_route' => 'zfcuser',
'login_redirect_route' => 'user',

/**
* Logout Redirect Route
Expand Down
4 changes: 2 additions & 2 deletions module/Application/view/layout/layout-small-header.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
<li class="sep">|</li>
<li class="user-info">
<div class="gravatar-container"><a class="login" href="<?php
echo $this->url('zfcuser');
echo $this->url('user');
?>" title="My profile">
<img src="<?php
echo $this->escapeHtmlAttr($this->zfcUserIdentity()->getPhotoUrl());
?>" alt="<?php echo $this->escapeHtmlAttr($this->zfcUserDisplayName());
?>" style="width:23px;height:23px;" />
</a></div><a class="login" href="<?php echo $this->url('zfcuser') ?>" title="My profile"> Hello <?php echo $this->escapeHtml(ucfirst($userDisplayName)); ?></a>
</a></div><a class="login" href="<?php echo $this->url('user') ?>" title="My profile"> Hello <?php echo $this->escapeHtml(ucfirst($userDisplayName)); ?></a>
</li>
<li class="sep">|</li>
<li ><a href="<?php echo $this->url('zfcuser/logout'); ?>">Logout</a></li>
Expand Down
4 changes: 2 additions & 2 deletions module/Application/view/layout/layout.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
?>
<li class="sep">|</li>
<li class="user-info">
<div class="gravatar-container"><a class="login" href="<?php echo $this->url('zfcuser') ?>" title="My profile">
<div class="gravatar-container"><a class="login" href="<?php echo $this->url('user') ?>" title="My profile">
<img src="<?php
echo $this->escapeHtmlAttr($this->zfcUserIdentity()->getPhotoUrl());
?>" alt="<?php echo $this->escapeHtmlAttr($this->zfcUserDisplayName());
?>" style="width:23px;height:23px;" />
</a></div><a class="login" href="<?php echo $this->url('zfcuser') ?>" title="My profile"> Hello <?php echo $this->escapeHtml(ucfirst($userDisplayName)) ?></a>
</a></div><a class="login" href="<?php echo $this->url('user') ?>" title="My profile"> Hello <?php echo $this->escapeHtml(ucfirst($userDisplayName)) ?></a>
</li>
<li class="sep">|</li>
<li ><a href="<?php echo $this->url('zfcuser/logout') ?>">Logout</a></li>
Expand Down
73 changes: 71 additions & 2 deletions module/User/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,78 @@
__DIR__ . '/../view',
],
],
'router' => [
'routes' => [
'zfcuser' => [
'options' => [
'route' => '/auth',
],
],
'scn-social-auth-user' => [
'options' => [
'route' => '/auth',
'defaults' => [
'controller' => 'User\Controller\Index',
],
],
],
'user' => [
'type' => 'Literal',
'options' => [
'route' => '/user',
'defaults' => [
'controller' => 'User\Controller\Index',
'action' => 'index',
],
],
'may_terminate' => true,
'child_routes' => [
'module' => [
'type' => 'Literal',
'options' => [
'route' => '/module',
],
'may_terminate' => false,
'child_routes' => [
'list' => [
'type' => 'Literal',
'options' => [
'route' => '/render-list',
'defaults' => [
'action' => 'render-module-list',
],
],
],
'add' => [
'type' => 'Literal',
'options' => [
'route' => '/add',
'defaults' => [
'action' => 'add',
],
],
],
'remove' => [
'type' => 'Segment',
'options' => [
'route' => '/:module_id/remove',
'constraints' => [
'module_id' => '[0-9]+',
],
'defaults' => [
'action' => 'remove',
],
],
],
],
],
],
],
],
],
'controllers' => [
'invokables' => [
'User\Controller\Module' => 'User\Controller\ModuleController',
'factories' => [
'User\Controller\Index' => 'User\Controller\IndexControllerFactory',
],
],
'view_helpers' => [
Expand Down
46 changes: 46 additions & 0 deletions module/User/src/User/Controller/IndexController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace User\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use ZfModule\Mapper\Module as ModuleMapper;

class IndexController extends AbstractActionController
{
/**
* @var ModuleMapper
*/
protected $moduleMapper;

/**
* @param ModuleMapper $mapper
*/
public function __construct(ModuleMapper $mapper)
{
$this->moduleMapper = $mapper;
}

public function indexAction()
{
$registeredModules = $this->renderModuleListAction();
$registeredModules->setTerminal(false);

$vm = new ViewModel();
$vm->addChild($registeredModules, 'registered_modules');

return $vm;
}

public function renderModuleListAction()
{
$modules = $this->moduleMapper->findByOwner($this->zfcUserAuthentication()->getIdentity()->getId());

$vm = new ViewModel();
$vm->setTemplate('user/index/render-module-list');
$vm->setVariable('modules', $modules);
$vm->setTerminal(true);

return $vm;
}
}
27 changes: 27 additions & 0 deletions module/User/src/User/Controller/IndexControllerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace User\Controller;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use ZfModule\Mapper\Module as ModuleMapper;

class IndexControllerFactory implements FactoryInterface
{
/**
* @param ServiceLocatorInterface $controllerManager
* @return IndexController
*/
public function createService(ServiceLocatorInterface $controllerManager)
{
/* @var ServiceLocatorInterface $controllerManager */
$serviceManager = $controllerManager->getServiceLocator();

/* @var ModuleMapper $moduleMapper */
$moduleMapper = $serviceManager->get('zfmodule_mapper_module');

return new IndexController(
$moduleMapper
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace UserTest\Integration\Controller;

use ApplicationTest\Integration\Util\Bootstrap;
use User\Entity\User as UserEntity;
use Zend\Authentication\AuthenticationService;
use Zend\Db\ResultSet\HydratingResultSet;
use Zend\Http\Response as HttpResponse;
use Zend\Stdlib\Hydrator\ClassMethods;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
use ZfModule\Entity\Module as ModuleEntity;
use ZfModule\Mapper\Module as ModuleMapper;
use ZfModule\View\Helper\TotalModules;

class IndexControllerTest extends AbstractHttpControllerTestCase
{
protected $viewHelperManager;

protected function setUp()
{
parent::setUp();
$this->setApplicationConfig(Bootstrap::getConfig());
$this->getApplicationServiceLocator()->setAllowOverride(true);

$mockTotalModules = $this->getMockBuilder(TotalModules::class)
->disableOriginalConstructor()
->getMock();

$this->viewHelperManager = $this->getApplicationServiceLocator()->get('ViewHelperManager');
$this->viewHelperManager->setService('totalModules', $mockTotalModules);

$mockAuthService = $this->getMockBuilder(AuthenticationService::class)
->disableOriginalConstructor()
->getMock();
$mockAuthService->expects($this->any())
->method('hasIdentity')
->will($this->returnValue(true));
$mockAuthService->expects($this->any())
->method('getIdentity')
->will($this->returnValue(new UserEntity()));
$this->getApplicationServiceLocator()->setService('zfcuser_auth_service', $mockAuthService);
$this->viewHelperManager->get('zfcUserIdentity')->setAuthService($mockAuthService);

$mockMapper = $this->getMockBuilder(ModuleMapper::class)
->disableOriginalConstructor()
->getMock();
$this->getApplicationServiceLocator()->setService('zfmodule_mapper_module', $mockMapper);
}

/**
* @group integration
*/
public function testIndexActionRendersUserModuleList()
{
$mockResultSet = new HydratingResultSet(new ClassMethods(false), new ModuleEntity());
$mockResultSet->initialize([[
'id' => 123,
'name' => 'FooModule',
'description' => 'some random module',
'url' => 'https://github.com/zendframework/modules.zendframework.com',
]]);

$mockMapper = $this->getApplicationServiceLocator()->get('zfmodule_mapper_module');
$mockMapper->expects($this->once())
->method('findByOwner')
->willReturn($mockResultSet);

$this->dispatch('/user');

$this->assertControllerName('User\Controller\Index');
$this->assertActionName('index');
$this->assertTemplateName('user/index/index');
$this->assertResponseStatusCode(HttpResponse::STATUS_CODE_200);
$this->assertContains('FooModule', $this->getResponse()->getContent());
$this->assertContains('/user/module/123/remove', $this->getResponse()->getContent());
}
}
26 changes: 26 additions & 0 deletions module/User/view/user/index/index.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

<div style="margin-bottom: 20px;">
<img style="float:left; height: 80px; padding-right: 10px;" src="<?php echo $this->zfcUserIdentity()->getPhotoUrl() ?>" alt="<?php echo $this->zfcUserDisplayName() ?>"/>
<h3 style="margin-bottom: 0px;">Hello, <?php echo $this->zfcUserDisplayName() ?>!</h3>
<p>We should fill this space with some useful information...</p>
<div class="clearfix"></div>
</div>

<?php foreach ($this->flashMessenger()->getMessages() as $message): ?>
<h3 class="zf-green"><?php echo $message ?></h3>
<?php endforeach; ?>

<ul class="nav nav-tabs">
<li class="active"><a href="#modules" data-toggle="tab"><?php echo $this->translate('Your Registered Modules'); ?></a></li>
<li><a href="#addrepo" data-toggle="tab"><?php echo $this->translate('Add a New Module'); ?></a></li>
</ul>

<div class="tab-content">
<div class="tab-pane active" id="modules">
<?php echo $this->registered_modules; ?>
</div>
<div class="tab-pane" id="addrepo">

</div>
</div>

17 changes: 17 additions & 0 deletions module/User/view/user/index/render-module-list.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
if ($this->modules->count() === 0) {
?>
<div class="alert alert-info alert-block"><?php echo $this->translate('You have not added any modules'); ?></div>
<?php
}
foreach ($this->modules as $module) {
echo $this->moduleView([
'id' => $module->getId(),
'owner' => $module->getOwner(),
'name' => $module->getName(),
'created_at' => $module->getCreatedAt(),
'url' => $module->getUrl(),
'photo_url' => $module->getPhotoUrl(),
'description' => $module->getDescription(),
], 'remove');
}
Loading