* initial commit
* get list of activities * get activity by id
This commit is contained in:
commit
b5c307166e
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
.idea
|
||||
composer.phar
|
||||
|
||||
clover.xml
|
||||
coveralls-upload.json
|
||||
phpunit.xml
|
||||
vendor/
|
||||
45
composer.json
Normal file
45
composer.json
Normal file
@ -0,0 +1,45 @@
|
||||
{
|
||||
"name": "zendframework/zend-expressive-skeleton",
|
||||
"description": "Zend expressive skeleton. Begin developing PSR-7 middleware applications in seconds!",
|
||||
"type": "project",
|
||||
"homepage": "https://github.com/zendframework/zend-expressive-skeleton",
|
||||
"license": "BSD-3-Clause",
|
||||
"require": {
|
||||
"php": "^5.6 || ^7.0",
|
||||
"roave/security-advisories": "dev-master",
|
||||
"zendframework/zend-expressive": "^1.0",
|
||||
"zendframework/zend-expressive-helpers": "^2.0",
|
||||
"zendframework/zend-stdlib": "^2.7 || ^3.0",
|
||||
"zendframework/zend-expressive-fastroute": "^1.0",
|
||||
"zendframework/zend-servicemanager": "^2.7.3 || ^3.0",
|
||||
"zendframework/zend-http": "^2.5",
|
||||
"symfony/css-selector": "^3.2"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^4.8",
|
||||
"squizlabs/php_codesniffer": "^2.3",
|
||||
"filp/whoops": "^1.1 || ^2.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"App\\": "src/App/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"AppTest\\": "test/AppTest/"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"check": [
|
||||
"@cs-check",
|
||||
"@test"
|
||||
],
|
||||
"cs-check": "phpcs",
|
||||
"cs-fix": "phpcbf",
|
||||
"serve": "php -S 0.0.0.0:8080 -t public public/index.php",
|
||||
"test": "phpunit --colors=always",
|
||||
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
|
||||
"upload-coverage": "coveralls -v"
|
||||
}
|
||||
}
|
||||
2423
composer.lock
generated
Normal file
2423
composer.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
2
config/autoload/.gitignore
vendored
Normal file
2
config/autoload/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
local.php
|
||||
*.local.php
|
||||
25
config/autoload/dependencies.global.php
Normal file
25
config/autoload/dependencies.global.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
use Zend\Expressive\Application;
|
||||
use Zend\Expressive\Container\ApplicationFactory;
|
||||
use Zend\Expressive\Helper;
|
||||
|
||||
return [
|
||||
// Provides application-wide services.
|
||||
// We recommend using fully-qualified class names whenever possible as
|
||||
// service names.
|
||||
'dependencies' => [
|
||||
// Use 'invokables' for constructor-less services, or services that do
|
||||
// not require arguments to the constructor. Map a service name to the
|
||||
// class name.
|
||||
'invokables' => [
|
||||
// Fully\Qualified\InterfaceName::class => Fully\Qualified\ClassName::class,
|
||||
Helper\ServerUrlHelper::class => Helper\ServerUrlHelper::class,
|
||||
],
|
||||
// Use 'factories' for services provided by callbacks/factory classes.
|
||||
'factories' => [
|
||||
Application::class => ApplicationFactory::class,
|
||||
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
|
||||
\App\Service\SkiesService::class => \App\Service\SkiesServiceFactory::class,
|
||||
],
|
||||
],
|
||||
];
|
||||
7
config/autoload/local.dist.php
Normal file
7
config/autoload/local.dist.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'authKey' => '',
|
||||
'debug' => true,
|
||||
'config_cache_enabled' => false,
|
||||
];
|
||||
69
config/autoload/middleware-pipeline.global.php
Normal file
69
config/autoload/middleware-pipeline.global.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
use Zend\Expressive\Container\ApplicationFactory;
|
||||
use Zend\Expressive\Helper;
|
||||
|
||||
return [
|
||||
'dependencies' => [
|
||||
'factories' => [
|
||||
Helper\ServerUrlMiddleware::class => Helper\ServerUrlMiddlewareFactory::class,
|
||||
Helper\UrlHelperMiddleware::class => Helper\UrlHelperMiddlewareFactory::class,
|
||||
],
|
||||
],
|
||||
// This can be used to seed pre- and/or post-routing middleware
|
||||
'middleware_pipeline' => [
|
||||
// An array of middleware to register. Each item is of the following
|
||||
// specification:
|
||||
//
|
||||
// [
|
||||
// Required:
|
||||
// 'middleware' => 'Name or array of names of middleware services and/or callables',
|
||||
// Optional:
|
||||
// 'path' => '/path/to/match', // string; literal path prefix to match
|
||||
// // middleware will not execute
|
||||
// // if path does not match!
|
||||
// 'error' => true, // boolean; true for error middleware
|
||||
// 'priority' => 1, // int; higher values == register early;
|
||||
// // lower/negative == register last;
|
||||
// // default is 1, if none is provided.
|
||||
// ],
|
||||
//
|
||||
// While the ApplicationFactory ignores the keys associated with
|
||||
// specifications, they can be used to allow merging related values
|
||||
// defined in multiple configuration files/locations. This file defines
|
||||
// some conventional keys for middleware to execute early, routing
|
||||
// middleware, and error middleware.
|
||||
'always' => [
|
||||
'middleware' => [
|
||||
// Add more middleware here that you want to execute on
|
||||
// every request:
|
||||
// - bootstrapping
|
||||
// - pre-conditions
|
||||
// - modifications to outgoing responses
|
||||
Helper\ServerUrlMiddleware::class,
|
||||
],
|
||||
'priority' => 10000,
|
||||
],
|
||||
|
||||
'routing' => [
|
||||
'middleware' => [
|
||||
ApplicationFactory::ROUTING_MIDDLEWARE,
|
||||
Helper\UrlHelperMiddleware::class,
|
||||
// Add more middleware here that needs to introspect the routing
|
||||
// results; this might include:
|
||||
// - route-based authentication
|
||||
// - route-based validation
|
||||
// - etc.
|
||||
ApplicationFactory::DISPATCH_MIDDLEWARE,
|
||||
],
|
||||
'priority' => 1,
|
||||
],
|
||||
|
||||
'error' => [
|
||||
'middleware' => [
|
||||
// Add error middleware here.
|
||||
],
|
||||
'error' => true,
|
||||
'priority' => -10000,
|
||||
],
|
||||
],
|
||||
];
|
||||
41
config/autoload/routes.global.php
Normal file
41
config/autoload/routes.global.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'dependencies' => [
|
||||
'invokables' => [
|
||||
Zend\Expressive\Router\RouterInterface::class => Zend\Expressive\Router\FastRouteRouter::class,
|
||||
App\Action\PingAction::class => App\Action\PingAction::class,
|
||||
],
|
||||
'factories' => [
|
||||
App\Action\HomePageAction::class => App\Action\HomePageFactory::class,
|
||||
App\Action\ActivityAction::class => App\Action\ActivityFactory::class,
|
||||
],
|
||||
],
|
||||
|
||||
'routes' => [
|
||||
[
|
||||
'name' => 'home',
|
||||
'path' => '/',
|
||||
'middleware' => App\Action\HomePageAction::class,
|
||||
'allowed_methods' => ['GET'],
|
||||
],
|
||||
[
|
||||
'name' => 'api.ping',
|
||||
'path' => '/api/ping',
|
||||
'middleware' => App\Action\PingAction::class,
|
||||
'allowed_methods' => ['GET'],
|
||||
],
|
||||
[
|
||||
'name' => 'api.activity',
|
||||
'path' => '/api/activity',
|
||||
'middleware' => App\Action\ActivityAction::class,
|
||||
'allowed_methods' => ['GET', 'POST', 'DELETE', 'OPTIONS'],
|
||||
],
|
||||
[
|
||||
'name' => 'api.activity.id',
|
||||
'path' => '/api/activity/{id:\d+}',
|
||||
'middleware' => App\Action\ActivityAction::class,
|
||||
'allowed_methods' => ['GET', 'PUT', 'DELETE', 'OPTIONS'],
|
||||
],
|
||||
],
|
||||
];
|
||||
14
config/autoload/zend-expressive.global.php
Normal file
14
config/autoload/zend-expressive.global.php
Normal file
@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
return [
|
||||
'debug' => false,
|
||||
|
||||
'config_cache_enabled' => false,
|
||||
|
||||
'zend-expressive' => [
|
||||
'error_handler' => [
|
||||
'template_404' => 'error::404',
|
||||
'template_error' => 'error::error',
|
||||
],
|
||||
],
|
||||
];
|
||||
35
config/config.php
Normal file
35
config/config.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
use Zend\Stdlib\ArrayUtils;
|
||||
use Zend\Stdlib\Glob;
|
||||
|
||||
/**
|
||||
* Configuration files are loaded in a specific order. First ``global.php``, then ``*.global.php``.
|
||||
* then ``local.php`` and finally ``*.local.php``. This way local settings overwrite global settings.
|
||||
*
|
||||
* The configuration can be cached. This can be done by setting ``config_cache_enabled`` to ``true``.
|
||||
*
|
||||
* Obviously, if you use closures in your config you can't cache it.
|
||||
*/
|
||||
|
||||
$cachedConfigFile = 'data/cache/app_config.php';
|
||||
|
||||
$config = [];
|
||||
if (is_file($cachedConfigFile)) {
|
||||
// Try to load the cached config
|
||||
$config = include $cachedConfigFile;
|
||||
} else {
|
||||
// Load configuration from autoload path
|
||||
foreach (Glob::glob('config/autoload/{{,*.}global,{,*.}local}.php', Glob::GLOB_BRACE) as $file) {
|
||||
$config = ArrayUtils::merge($config, include $file);
|
||||
}
|
||||
|
||||
// Cache config if enabled
|
||||
if (isset($config['config_cache_enabled']) && $config['config_cache_enabled'] === true) {
|
||||
file_put_contents($cachedConfigFile, '<?php return ' . var_export($config, true) . ';');
|
||||
}
|
||||
}
|
||||
|
||||
// Return an ArrayObject so we can inject the config as a service in Aura.Di
|
||||
// and still use array checks like ``is_array``.
|
||||
return new ArrayObject($config, ArrayObject::ARRAY_AS_PROPS);
|
||||
16
config/container.php
Normal file
16
config/container.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
use Zend\ServiceManager\Config;
|
||||
use Zend\ServiceManager\ServiceManager;
|
||||
|
||||
// Load configuration
|
||||
$config = require __DIR__ . '/config.php';
|
||||
|
||||
// Build container
|
||||
$container = new ServiceManager();
|
||||
(new Config($config['dependencies']))->configureServiceManager($container);
|
||||
|
||||
// Inject config
|
||||
$container->setService('config', $config);
|
||||
|
||||
return $container;
|
||||
2
data/.gitignore
vendored
Normal file
2
data/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
20
phpcs.xml
Normal file
20
phpcs.xml
Normal file
@ -0,0 +1,20 @@
|
||||
<?xml version="1.0"?>
|
||||
<ruleset name="Zend Framework coding standard">
|
||||
<description>Zend Framework coding standard</description>
|
||||
|
||||
<!-- display progress -->
|
||||
<arg value="p"/>
|
||||
<arg name="colors"/>
|
||||
|
||||
<!-- inherit rules from: -->
|
||||
<rule ref="PSR2"/>
|
||||
<rule ref="Generic.Arrays.DisallowLongArraySyntax"/>
|
||||
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
|
||||
<properties>
|
||||
<property name="ignoreBlankLines" value="false"/>
|
||||
</properties>
|
||||
</rule>
|
||||
|
||||
<!-- Paths to check -->
|
||||
<file>src</file>
|
||||
</ruleset>
|
||||
17
phpunit.xml.dist
Normal file
17
phpunit.xml.dist
Normal file
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
|
||||
bootstrap="vendor/autoload.php"
|
||||
colors="true">
|
||||
<testsuites>
|
||||
<testsuite name="App\\Tests">
|
||||
<directory>./test</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory suffix=".php">./src</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
||||
17
public/.htaccess
Normal file
17
public/.htaccess
Normal file
@ -0,0 +1,17 @@
|
||||
RewriteEngine On
|
||||
# The following rule tells Apache that if the requested filename
|
||||
# exists, simply serve it.
|
||||
RewriteCond %{REQUEST_FILENAME} -s [OR]
|
||||
RewriteCond %{REQUEST_FILENAME} -l [OR]
|
||||
RewriteCond %{REQUEST_FILENAME} -d
|
||||
RewriteRule ^.*$ - [NC,L]
|
||||
|
||||
# The following rewrites all other queries to index.php. The
|
||||
# condition ensures that if you are using Apache aliases to do
|
||||
# mass virtual hosting, the base path will be prepended to
|
||||
# allow proper resolution of the index.php file; it will work
|
||||
# in non-aliased environments as well, providing a safe, one-size
|
||||
# fits all solution.
|
||||
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
|
||||
RewriteRule ^(.*) - [E=BASE:%1]
|
||||
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]
|
||||
BIN
public/favicon.ico
Normal file
BIN
public/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.4 KiB |
18
public/index.php
Normal file
18
public/index.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
// Delegate static file requests back to the PHP built-in webserver
|
||||
if (php_sapi_name() === 'cli-server'
|
||||
&& is_file(__DIR__ . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
chdir(dirname(__DIR__));
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
/** @var \Interop\Container\ContainerInterface $container */
|
||||
$container = require 'config/container.php';
|
||||
|
||||
/** @var \Zend\Expressive\Application $app */
|
||||
$app = $container->get(\Zend\Expressive\Application::class);
|
||||
$app->run();
|
||||
BIN
public/zf-logo.png
Normal file
BIN
public/zf-logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 600 B |
92
src/App/Action/AbstractAction.php
Normal file
92
src/App/Action/AbstractAction.php
Normal file
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Zend\Stratigility\MiddlewareInterface;
|
||||
|
||||
abstract class AbstractAction implements MiddlewareInterface
|
||||
{
|
||||
const IDENTIFIER_NAME = 'id';
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$requestMethod = strtoupper($request->getMethod());
|
||||
$id = $request->getAttribute(static::IDENTIFIER_NAME);
|
||||
|
||||
switch ($requestMethod) {
|
||||
case 'GET':
|
||||
return isset($id)
|
||||
? $this->get($request, $response, $next)
|
||||
: $this->getList($request, $response, $next);
|
||||
case 'POST':
|
||||
return $this->create($request, $response, $next);
|
||||
case 'PUT':
|
||||
return $this->update($request, $response, $next);
|
||||
case 'DELETE':
|
||||
return isset($id)
|
||||
? $this->delete($request, $response, $next)
|
||||
: $this->deleteList($request, $response, $next);
|
||||
case 'HEAD':
|
||||
return $this->head($request, $response, $next);
|
||||
case 'OPTIONS':
|
||||
return $this->options($request, $response, $next);
|
||||
case 'PATCH':
|
||||
return $this->patch($request, $response, $next);
|
||||
default:
|
||||
return $next($request, $response);
|
||||
}
|
||||
}
|
||||
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function getList(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function create(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function update(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function delete(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function deleteList(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function head(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function options(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
public function patch(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return $this->createResponse(['content' => 'Method not allowed'], 405);
|
||||
}
|
||||
|
||||
final protected function createResponse($data, $status = 200)
|
||||
{
|
||||
return new JsonResponse($data, $status);
|
||||
}
|
||||
}
|
||||
34
src/App/Action/ActivityAction.php
Normal file
34
src/App/Action/ActivityAction.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use App\Service\SkiesService;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
|
||||
class ActivityAction extends AbstractAction
|
||||
{
|
||||
private $skiesService;
|
||||
|
||||
public function __construct(SkiesService $skiesService)
|
||||
{
|
||||
$this->skiesService = $skiesService;
|
||||
}
|
||||
|
||||
public function getList(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return new JsonResponse($this->skiesService->getActivityList());
|
||||
}
|
||||
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$id = $request->getAttribute(self::IDENTIFIER_NAME);
|
||||
return new JsonResponse($this->skiesService->getActivity($id));
|
||||
}
|
||||
|
||||
public function options(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return new JsonResponse(true);
|
||||
}
|
||||
}
|
||||
15
src/App/Action/ActivityFactory.php
Normal file
15
src/App/Action/ActivityFactory.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use App\Service\SkiesService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class ActivityFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$skiesService = $container->get(SkiesService::class);
|
||||
return new ActivityAction($skiesService);
|
||||
}
|
||||
}
|
||||
62
src/App/Action/HomePageAction.php
Normal file
62
src/App/Action/HomePageAction.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Zend\Diactoros\Response\HtmlResponse;
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Zend\Expressive\Router;
|
||||
use Zend\Expressive\Template;
|
||||
use Zend\Expressive\Plates\PlatesRenderer;
|
||||
use Zend\Expressive\Twig\TwigRenderer;
|
||||
use Zend\Expressive\ZendView\ZendViewRenderer;
|
||||
|
||||
class HomePageAction
|
||||
{
|
||||
private $router;
|
||||
|
||||
private $template;
|
||||
|
||||
public function __construct(Router\RouterInterface $router, Template\TemplateRendererInterface $template = null)
|
||||
{
|
||||
$this->router = $router;
|
||||
$this->template = $template;
|
||||
}
|
||||
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
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'] = 'http://framework.zend.com/manual/current/en/modules/zend.mvc.routing.html';
|
||||
}
|
||||
|
||||
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'] = 'http://framework.zend.com/manual/current/en/modules/zend.view.quick-start.html';
|
||||
}
|
||||
|
||||
if (!$this->template) {
|
||||
return new JsonResponse([
|
||||
'welcome' => 'Congratulations! You have installed the zend-expressive skeleton application.',
|
||||
'docsUrl' => 'zend-expressive.readthedocs.org',
|
||||
]);
|
||||
}
|
||||
|
||||
return new HtmlResponse($this->template->render('app::home-page', $data));
|
||||
}
|
||||
}
|
||||
20
src/App/Action/HomePageFactory.php
Normal file
20
src/App/Action/HomePageFactory.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Zend\Expressive\Router\RouterInterface;
|
||||
use Zend\Expressive\Template\TemplateRendererInterface;
|
||||
|
||||
class HomePageFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$router = $container->get(RouterInterface::class);
|
||||
$template = ($container->has(TemplateRendererInterface::class))
|
||||
? $container->get(TemplateRendererInterface::class)
|
||||
: null;
|
||||
|
||||
return new HomePageAction($router, $template);
|
||||
}
|
||||
}
|
||||
17
src/App/Action/PingAction.php
Normal file
17
src/App/Action/PingAction.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
|
||||
use Zend\Diactoros\Response\JsonResponse;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
class PingAction
|
||||
{
|
||||
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null)
|
||||
{
|
||||
return new JsonResponse([
|
||||
'ack' => time(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
187
src/App/Service/SkiesService.php
Normal file
187
src/App/Service/SkiesService.php
Normal file
@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Symfony\Component\CssSelector\CssSelectorConverter;
|
||||
use Zend\Http\Client;
|
||||
|
||||
/**
|
||||
* Class SkiesService
|
||||
*
|
||||
* @package App\Service
|
||||
* @todo handle errors in http response
|
||||
*/
|
||||
class SkiesService
|
||||
{
|
||||
const BASE_URI = 'http://skies.sigmatechnology.se/';
|
||||
|
||||
/**
|
||||
* @var Client
|
||||
*/
|
||||
private $httpClient;
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
private $container;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
$this->httpClient = new Client();
|
||||
$headers = $this->httpClient->getRequest()->getHeaders();
|
||||
$headers->addHeaderLine('Authorization', 'Basic ' . $this->getAuthToken());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of all activities
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getActivityList()
|
||||
{
|
||||
$this->httpClient->setUri(self::BASE_URI . "main.asp?rID=2");
|
||||
$skiesResponse = $this->httpClient->send();
|
||||
$skiesHtmlBody = $skiesResponse->getBody();
|
||||
|
||||
$cssToXpathConverter = new CssSelectorConverter();
|
||||
$xpathQuery = $cssToXpathConverter->toXPath('html > body > div > div.row > div.col-md-10 > div.row > div.col-md-9 > div.box > table.table-striped > tr');
|
||||
|
||||
$doc = new \DOMDocument();
|
||||
$doc->loadHTML($skiesHtmlBody);
|
||||
|
||||
$xpath = new \DOMXPath($doc);
|
||||
$elements = $xpath->query($xpathQuery);
|
||||
|
||||
$parsed = [];
|
||||
/** @var \DOMNode $domElement */
|
||||
foreach ($elements as $domElement) {
|
||||
if($domElement->childNodes->item(0)->nodeName != 'td') {
|
||||
continue;
|
||||
}
|
||||
$url = $domElement->childNodes->item(0)->childNodes->item(0)->attributes->getNamedItem('href')->textContent;
|
||||
preg_match("/aktid=([0-9]+)/msi", $url, $match);
|
||||
$parsed[] = [
|
||||
'id' => (int)$match[1],
|
||||
'label' => $domElement->childNodes->item(0)->childNodes->item(0)->textContent,
|
||||
'date' => $domElement->childNodes->item(2)->textContent,
|
||||
'time' => trim($domElement->childNodes->item(3)->textContent, " \t\n\r\0\x0B\xc2\xa0"),
|
||||
];
|
||||
}
|
||||
|
||||
return $parsed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single activity
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function getActivity(int $id)
|
||||
{
|
||||
$this->httpClient->setUri(self::BASE_URI . "main.asp?rID=2&alt=1&aktID=" . $id);
|
||||
$skiesResponse = $this->httpClient->send();
|
||||
$skiesHtmlBody = $skiesResponse->getBody();
|
||||
|
||||
$cssToXpathConverter = new CssSelectorConverter();
|
||||
$xpathQuery = $cssToXpathConverter->toXPath('div.container > div.row > div.col-md-10 > div.row > div.col-md-9 > div.box');
|
||||
$xpathCommentsQuery = $cssToXpathConverter->toXPath('div.container div.paragraph > div.onecomment');
|
||||
|
||||
$doc = new \DOMDocument();
|
||||
$doc->loadHTML($skiesHtmlBody);
|
||||
|
||||
$xpath = new \DOMXPath($doc);
|
||||
$elements = $xpath->query($xpathQuery);
|
||||
|
||||
$h5Elements = $elements->item(0)->getElementsByTagName('h5');
|
||||
$title = $h5Elements->item(0)->textContent;
|
||||
|
||||
$detailDivElements = $elements->item(0)->getElementsByTagName('div');
|
||||
$commentElements = $xpath->query($xpathCommentsQuery);
|
||||
|
||||
$eventDetails = $this->parseActivityDetails($detailDivElements->item(1)->textContent);
|
||||
$eventDescription = $this->parseActivityDescription($elements->item(0));
|
||||
$eventComments = $this->parseActivityComments($commentElements);
|
||||
|
||||
return [
|
||||
'title' => $title,
|
||||
'details' => $eventDetails,
|
||||
'description' => $eventDescription,
|
||||
'comments' => $eventComments,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|string
|
||||
* @todo real auth token from whatever...
|
||||
*/
|
||||
private function getAuthToken(): ?string
|
||||
{
|
||||
$config = $this->container->get('config');
|
||||
return $config['authKey'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the activity information details returning the date, time finaly entry and the person accountable
|
||||
*
|
||||
* @param string $divText
|
||||
* @return array|null
|
||||
*/
|
||||
private function parseActivityDetails(string $divText): ?array
|
||||
{
|
||||
preg_match("#Date:.*?([0-9]{4}-[0-9]{2}-[0-9]{2}).*?Time: ([0-9]{1,2}:[0-9]{2}).*?Final entry day:.*?([0-9]{4}-[0-9]{2}-[0-9]{2}).*?Accountable: (.*?) / ([0-9 +]*)#msi", $divText, $matches);
|
||||
return [
|
||||
'date' => $matches[1],
|
||||
'time' => $matches[2],
|
||||
'finalEntry' => $matches[3],
|
||||
'accountable' => str_replace(" ", " ", $matches[5] ? sprintf("%s (%s)", $matches[4], $matches[5]) : $matches[4]),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses comment block. Comment section is pretty much unformatted text
|
||||
* between a <hr> tag and a <form> tag in the passed $element
|
||||
*
|
||||
* @param \DOMElement $element
|
||||
* @return null|string
|
||||
*/
|
||||
private function parseActivityDescription(\DOMElement $element): ?string
|
||||
{
|
||||
$description = "";
|
||||
$isContent = false;
|
||||
/** @var \DOMElement $childNode */
|
||||
foreach($element->childNodes as $childNode) {
|
||||
if ($childNode->nodeName == "hr") { $isContent = true; }
|
||||
if ($childNode->nodeName == "form") { break; }
|
||||
if ($isContent) {
|
||||
$description .= $childNode->nodeName == "br"
|
||||
? "\n"
|
||||
: rtrim($childNode->textContent);
|
||||
}
|
||||
}
|
||||
return trim($description);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse $commentElements
|
||||
*
|
||||
* @param \DOMNodeList $commentElements
|
||||
* @return array|null
|
||||
*/
|
||||
private function parseActivityComments(\DOMNodeList $commentElements): ?array
|
||||
{
|
||||
$comments = [];
|
||||
/** @var \DOMElement $commentElement */
|
||||
foreach($commentElements as $commentElement) {
|
||||
preg_match("#(.*)\s/\s([0-9]{4}-[0-9]{2}-[0-9]{2})\s([0-9]{1,2}:[0-9]{1,2})#msi", str_replace(" "," ", $commentElement->getElementsByTagName("div")->item(2)->textContent), $matches);
|
||||
$comments[] = [
|
||||
'comment' => $commentElement->getElementsByTagName("div")->item(1)->textContent,
|
||||
'user' => $matches[1],
|
||||
'date' => $matches[2],
|
||||
'time' => $matches[3],
|
||||
];
|
||||
}
|
||||
return $comments;
|
||||
}
|
||||
}
|
||||
13
src/App/Service/SkiesServiceFactory.php
Normal file
13
src/App/Service/SkiesServiceFactory.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class SkiesServiceFactory
|
||||
{
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
return new SkiesService($container);
|
||||
}
|
||||
}
|
||||
0
templates/.gitkeep
Normal file
0
templates/.gitkeep
Normal file
28
test/AppTest/Action/HomePageActionTest.php
Normal file
28
test/AppTest/Action/HomePageActionTest.php
Normal file
@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace AppTest\Action;
|
||||
|
||||
use App\Action\HomePageAction;
|
||||
use Zend\Diactoros\Response;
|
||||
use Zend\Diactoros\ServerRequest;
|
||||
use Zend\Expressive\Router\RouterInterface;
|
||||
|
||||
class HomePageActionTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/** @var RouterInterface */
|
||||
protected $router;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->router = $this->prophesize(RouterInterface::class);
|
||||
}
|
||||
|
||||
public function testResponse()
|
||||
{
|
||||
$homePage = new HomePageAction($this->router->reveal(), null);
|
||||
$response = $homePage(new ServerRequest(['/']), new Response(), function () {
|
||||
});
|
||||
|
||||
$this->assertTrue($response instanceof Response);
|
||||
}
|
||||
}
|
||||
50
test/AppTest/Action/HomePageFactoryTest.php
Normal file
50
test/AppTest/Action/HomePageFactoryTest.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace AppTest\Action;
|
||||
|
||||
use App\Action\HomePageAction;
|
||||
use App\Action\HomePageFactory;
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Zend\Expressive\Router\RouterInterface;
|
||||
use Zend\Expressive\Template\TemplateRendererInterface;
|
||||
|
||||
class HomePageFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/** @var ContainerInterface */
|
||||
protected $container;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->container = $this->prophesize(ContainerInterface::class);
|
||||
$router = $this->prophesize(RouterInterface::class);
|
||||
|
||||
$this->container->get(RouterInterface::class)->willReturn($router);
|
||||
}
|
||||
|
||||
public function testFactoryWithoutTemplate()
|
||||
{
|
||||
$factory = new HomePageFactory();
|
||||
$this->container->has(TemplateRendererInterface::class)->willReturn(false);
|
||||
|
||||
$this->assertTrue($factory instanceof HomePageFactory);
|
||||
|
||||
$homePage = $factory($this->container->reveal());
|
||||
|
||||
$this->assertTrue($homePage instanceof HomePageAction);
|
||||
}
|
||||
|
||||
public function testFactoryWithTemplate()
|
||||
{
|
||||
$factory = new HomePageFactory();
|
||||
$this->container->has(TemplateRendererInterface::class)->willReturn(true);
|
||||
$this->container
|
||||
->get(TemplateRendererInterface::class)
|
||||
->willReturn($this->prophesize(TemplateRendererInterface::class));
|
||||
|
||||
$this->assertTrue($factory instanceof HomePageFactory);
|
||||
|
||||
$homePage = $factory($this->container->reveal());
|
||||
|
||||
$this->assertTrue($homePage instanceof HomePageAction);
|
||||
}
|
||||
}
|
||||
22
test/AppTest/Action/PingActionTest.php
Normal file
22
test/AppTest/Action/PingActionTest.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace AppTest\Action;
|
||||
|
||||
use App\Action\PingAction;
|
||||
use Zend\Diactoros\Response;
|
||||
use Zend\Diactoros\ServerRequest;
|
||||
|
||||
class PingActionTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testResponse()
|
||||
{
|
||||
$pingAction = new PingAction();
|
||||
$response = $pingAction(new ServerRequest(['/']), new Response(), function () {
|
||||
});
|
||||
$json = json_decode((string) $response->getBody());
|
||||
|
||||
$this->assertTrue($response instanceof Response);
|
||||
$this->assertTrue($response instanceof Response\JsonResponse);
|
||||
$this->assertTrue(isset($json->ack));
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user