* initial commit

This commit is contained in:
Danyi Dávid
2018-05-02 11:01:38 +02:00
commit ff2721f67c
39 changed files with 5308 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace App;
/**
* The configuration provider for the App module
*
* @see https://docs.zendframework.com/zend-component-installer/
*/
class ConfigProvider
{
/**
* Returns the configuration array
*
* To add a bit of a structure, each section is defined in a separate
* method which returns an array with its configuration.
*
*/
public function __invoke() : array
{
return [
'dependencies' => $this->getDependencies(),
'templates' => $this->getTemplates(),
];
}
/**
* Returns the container dependencies
*/
public function getDependencies() : array
{
return [
'invokables' => [
Handler\PingHandler::class => Handler\PingHandler::class,
],
'factories' => [
Handler\HomePageHandler::class => Handler\HomePageHandlerFactory::class,
Plates\NavigationExtension::class => Plates\NavigationExtensionFactory::class,
],
];
}
/**
* Returns the templates configuration
*/
public function getTemplates() : array
{
return [
'paths' => [
'app' => ['templates/app'],
'error' => ['templates/error'],
'layout' => ['templates/layout'],
],
];
}
}

View File

@@ -0,0 +1,94 @@
<?php
declare(strict_types=1);
namespace App\Handler;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Expressive\Plates\PlatesRenderer;
use Zend\Expressive\Router;
use Zend\Expressive\Template;
use Zend\Expressive\Twig\TwigRenderer;
use Zend\Expressive\ZendView\ZendViewRenderer;
class HomePageHandler implements RequestHandlerInterface
{
private $containerName;
private $router;
private $template;
public function __construct(
Router\RouterInterface $router,
Template\TemplateRendererInterface $template = null,
string $containerName
) {
$this->router = $router;
$this->template = $template;
$this->containerName = $containerName;
}
public function handle(ServerRequestInterface $request) : ResponseInterface
{
if (! $this->template) {
return new JsonResponse([
'welcome' => 'Congratulations! You have installed the zend-expressive skeleton application.',
'docsUrl' => 'https://docs.zendframework.com/zend-expressive/',
]);
}
$data = [];
switch ($this->containerName) {
case 'Aura\Di\Container':
$data['containerName'] = 'Aura.Di';
$data['containerDocs'] = 'http://auraphp.com/packages/2.x/Di.html';
break;
case 'Pimple\Container':
$data['containerName'] = 'Pimple';
$data['containerDocs'] = 'https://pimple.symfony.com/';
break;
case 'Zend\ServiceManager\ServiceManager':
$data['containerName'] = 'Zend Servicemanager';
$data['containerDocs'] = 'https://docs.zendframework.com/zend-servicemanager/';
break;
case 'Auryn\Injector':
$data['containerName'] = 'Auryn';
$data['containerDocs'] = 'https://github.com/rdlowrey/Auryn';
break;
case 'Symfony\Component\DependencyInjection\ContainerBuilder':
$data['containerName'] = 'Symfony DI Container';
$data['containerDocs'] = 'https://symfony.com/doc/current/service_container.html';
break;
}
if ($this->router instanceof Router\AuraRouter) {
$data['routerName'] = 'Aura.Router';
$data['routerDocs'] = 'http://auraphp.com/packages/2.x/Router.html';
} elseif ($this->router instanceof Router\FastRouteRouter) {
$data['routerName'] = 'FastRoute';
$data['routerDocs'] = 'https://github.com/nikic/FastRoute';
} elseif ($this->router instanceof Router\ZendRouter) {
$data['routerName'] = 'Zend Router';
$data['routerDocs'] = 'https://docs.zendframework.com/zend-router/';
}
if ($this->template instanceof PlatesRenderer) {
$data['templateName'] = 'Plates';
$data['templateDocs'] = 'http://platesphp.com/';
} elseif ($this->template instanceof TwigRenderer) {
$data['templateName'] = 'Twig';
$data['templateDocs'] = 'http://twig.sensiolabs.org/documentation';
} elseif ($this->template instanceof ZendViewRenderer) {
$data['templateName'] = 'Zend View';
$data['templateDocs'] = 'https://docs.zendframework.com/zend-view/';
}
return new HtmlResponse($this->template->render('app::home-page', $data));
}
}

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace App\Handler;
use Psr\Container\ContainerInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Expressive\Router\RouterInterface;
use Zend\Expressive\Template\TemplateRendererInterface;
class HomePageHandlerFactory
{
public function __invoke(ContainerInterface $container) : RequestHandlerInterface
{
$router = $container->get(RouterInterface::class);
$template = $container->has(TemplateRendererInterface::class)
? $container->get(TemplateRendererInterface::class)
: null;
return new HomePageHandler($router, $template, get_class($container));
}
}

View File

@@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
namespace App\Handler;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\JsonResponse;
class PingHandler implements RequestHandlerInterface
{
public function handle(ServerRequestInterface $request) : ResponseInterface
{
return new JsonResponse(['ack' => time()]);
}
}

View File

@@ -0,0 +1,102 @@
<?php
declare(strict_types=1);
namespace App\Plates;
use Knp\Menu\ItemInterface;
use Knp\Menu\Matcher\Matcher;
use Knp\Menu\Matcher\Voter\UriVoter;
use Knp\Menu\Matcher\Voter\VoterInterface;
use Knp\Menu\MenuFactory;
use Knp\Menu\MenuItem;
use Knp\Menu\Renderer\ListRenderer;
use League\Plates\Engine;
use League\Plates\Extension\ExtensionInterface;
use Zend\Expressive\Router\RouterInterface;
class NavigationExtension implements ExtensionInterface
{
/** @var RouterInterface */
private $router;
/** @var MenuItem */
private $menu;
public function __construct(RouterInterface $router)
{
$this->router = $router;
}
/**
* Register functions with the Plates engine.
*
* Registers:
*
* - url($route = null, array $params = []) : string
* - serverurl($path = null) : string
*
* @param Engine $engine
*/
public function register(Engine $engine): void
{
$engine->registerFunction('navigation', [$this, 'generateNavigation']);
}
public function generateNavigation(): string
{
$this->initMainMenu();
$matcher = new Matcher([
new UriVoter($_SERVER['REQUEST_URI'])
]);
$renderer = new ListRenderer($matcher);
return $renderer->render($this->menu);
}
private function initMainMenu()
{
$factory = new MenuFactory();
$this->menu = $factory->createItem("Main-menu");
$prizeMenu = $this->menu->addChild("The prize", [
'uri' => $this->getUriFromRouter('the-prize')
]);
$prizeMenu->addChild("Background and Purpose", [
'uri' => $this->getUriFromRouter('the-prize.bg')
]);
$prizeMenu->addChild("Description and Values", [
'uri' => $this->getUriFromRouter('the-prize.desc')
]);
$prizeMenu->addChild("Aspect for Selection", [
'uri' => $this->getUriFromRouter('the-prize.aspect')
]);
$prizeMenu->addChild("Gran Prize Award and Events", [
'uri' => $this->getUriFromRouter('the-prize.events')
]);
$this->menu->addChild("Judges", [
'uri' => $this->getUriFromRouter('judges')
]);
$awardeesMenu = $this->menu->addChild("Awardees", [
'uri' => $this->getUriFromRouter('awardees')
]);
$this->populateAwardeesSubmenu($awardeesMenu);
}
private function getUriFromRouter(string $name, $param = []): string
{
return $this->router->generateUri($name, $param);
}
private function populateAwardeesSubmenu(ItemInterface $awardeesMenu)
{
$year = (int)date("Y");
for ($i = $year; $i > 2012; $i--) {
$awardeesMenu->addChild($i, [
'uri' => $this->getUriFromRouter('awardees-by-year', ['year' => $i])
]);
}
}
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace App\Plates;
use Psr\Container\ContainerInterface;
use Zend\Expressive\Plates\Exception\MissingHelperException;
use Zend\Expressive\Router\RouterInterface;
class NavigationExtensionFactory
{
public function __invoke(ContainerInterface $container) : NavigationExtension
{
if (! $container->has(RouterInterface::class)) {
throw new MissingHelperException(sprintf(
'%s requires that the %s service be present; not found',
NavigationExtension::class,
RouterInterface::class
));
}
$router = $container->get(RouterInterface::class);
return new NavigationExtension($router);
}
}