-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Handler; | ||
|
||
use Exception; | ||
use MapFile\Parser\Map as MapParser; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Zend\Diactoros\Response\HtmlResponse; | ||
use Zend\Diactoros\Response\RedirectResponse; | ||
use Zend\Diactoros\UploadedFile; | ||
use Zend\Expressive\Router; | ||
use Zend\Expressive\Session\SessionMiddleware; | ||
use Zend\Expressive\Template; | ||
|
||
class OpenHandler 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 | ||
{ | ||
$session = $request->getAttribute(SessionMiddleware::SESSION_ATTRIBUTE); | ||
|
||
$method = $request->getMethod(); | ||
|
||
switch ($method) { | ||
case 'POST': | ||
try { | ||
$files = $request->getUploadedFiles(); | ||
|
||
if (isset($files['mapfile'])) { | ||
if ($files['mapfile']->getError() === UPLOAD_ERR_OK) { | ||
$temp = tempnam(sys_get_temp_dir(), 'mapfile-'); | ||
|
||
$files['mapfile']->moveTo($temp); | ||
|
||
$map = (new MapParser($temp))->parse(); | ||
|
||
$session->set('map', serialize($map)); | ||
|
||
return new RedirectResponse($this->router->generateUri('map')); | ||
} else { | ||
throw new Exception(UploadedFile::ERROR_MESSAGES[$files['mapfile']->getError()]); | ||
} | ||
} else { | ||
throw new Exception(UploadedFile::ERROR_MESSAGES[UPLOAD_ERR_NO_FILE]); | ||
} | ||
} catch (Exception $e) { | ||
$data = [ | ||
'error' => $e->getMessage(), | ||
]; | ||
|
||
return new HtmlResponse($this->template->render('app::open', $data)); | ||
} | ||
break; | ||
|
||
case 'GET': | ||
return new HtmlResponse($this->template->render('app::open', [])); | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?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 OpenHandlerFactory | ||
{ | ||
public function __invoke(ContainerInterface $container): RequestHandlerInterface | ||
{ | ||
$router = $container->get(RouterInterface::class); | ||
$template = $container->get(TemplateRendererInterface::class); | ||
|
||
return new OpenHandler($router, $template, get_class($container)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{% extends '@layout/default.html.twig' %} | ||
|
||
{% block title %}Load your MapFile{% endblock %} | ||
|
||
{% block content %} | ||
|
||
<h1>Load your MapFile</h1> | ||
<hr> | ||
|
||
<p> | ||
The application will parse your MapFile to allow you to modify it.<br> | ||
<br> | ||
The <a href="https://github.com/jbelien/MapFile-PHP-Library" target="_blank"><em>MapFile PHP Library</em></a> used | ||
to parse your file supports <a href="https://mapserver.org/mapfile/" target="_blank">MapServer 7.2 configuration</a>. | ||
If your MapFile is written for a previous version of MapServer, some parameters could be deprecated.<br> | ||
You can comment (with <kbd>#</kbd>) or fix the deprecated parameter.<br> | ||
You can also <a href="https://github.com/jbelien/MapFile-PHP-Library/issues" target="_blank">contact me</a> to add | ||
support for deprecated (or unsupported) parameters. | ||
</p> | ||
<hr> | ||
|
||
<form method="POST" action="{{ path('open') }}" enctype="multipart/form-data"> | ||
<div class="row"> | ||
<div class="col"> | ||
<div class="form-group"> | ||
<input type="file" class="form-control-file" id="open-mapfile" name="mapfile" accept=".map" required> | ||
</div> | ||
</div> | ||
<div class="col-md-3 col-sm-4"> | ||
<button type="submit" class="btn btn-primary btn-block"> | ||
<i class="fas fa-file-upload"></i> | ||
Upload | ||
</button> | ||
</div> | ||
</div> | ||
</form> | ||
<hr> | ||
|
||
{% if error is defined %} | ||
<div class="alert alert-warning"> | ||
{{ error }} | ||
</div> | ||
{% endif %} | ||
|
||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters