Files
contao-leaflet-maps/src/Backend/Action/AboutAction.php

146 lines
3.9 KiB
PHP
Raw Normal View History

2015-01-09 20:57:19 +01:00
<?php
/**
2017-10-05 15:45:43 +02:00
* Leaflet maps for Contao CMS.
*
2016-10-11 10:40:15 +02:00
* @package contao-leaflet-maps
2015-01-09 20:57:19 +01:00
* @author David Molineus <david.molineus@netzmacht.de>
* @copyright 2014-2019 netzmacht David Molineus. All rights reserved.
2017-10-05 15:45:43 +02:00
* @license LGPL-3.0 https://github.com/netzmacht/contao-leaflet-maps/blob/master/LICENSE
2015-01-09 20:57:19 +01:00
* @filesource
*/
namespace Netzmacht\Contao\Leaflet\Backend\Action;
2015-01-09 20:57:19 +01:00
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;
2015-01-12 19:03:29 +01:00
/**
* Credits backend module.
*
* @package Netzmacht\Contao\Leaflet\Backend
*/
final class AboutAction
2015-01-09 20:57:19 +01:00
{
/**
* Twig environment.
*
* @var Environment
*/
private $twig;
/**
* Project directory.
*
* @var string
*/
private $projectDir;
/**
* AboutAction constructor.
*
* @param Environment $twig Twig environment.
* @param string $projectDir Project directory.
*/
public function __construct(Environment $twig, string $projectDir)
{
$this->twig = $twig;
$this->projectDir = $projectDir;
}
2015-01-12 19:03:29 +01:00
/**
* Generate the backend view.
*
* @return string
*/
public function __invoke(): Response
2015-01-09 20:57:19 +01:00
{
$data = [
'headline' => 'Leaftlet maps integration for Contao CMS',
'libraries' => $this->getLibraries(),
];
2015-01-09 20:57:19 +01:00
[$data['version'], $data['dependencies']] = $this->extractFromComposer();
2015-01-09 20:57:19 +01:00
return new Response(
$this->twig->render('@NetzmachtContaoLeaflet/backend/about.html.twig', $data)
);
2015-01-09 20:57:19 +01:00
}
2015-01-12 19:03:29 +01:00
/**
* Get list of all libraries.
*
* @return array
*
* @SuppressWarnings(PHPMD.Superglobals)
*/
private function getLibraries(): array
2015-01-09 20:57:19 +01:00
{
return array_map(
function ($library) {
$library = array_merge(
[
2015-01-09 20:57:19 +01:00
'homepage' => null,
'version' => null,
],
2015-01-09 20:57:19 +01:00
$library
);
if ($library['homepage']) {
$library['homepage'] = sprintf(
'<a href="%s" target="_blank">%s</a>',
2015-01-09 20:57:19 +01:00
$library['homepage'],
preg_replace('#^(https?://(www)?)#i', '', $library['homepage'])
);
}
return $library;
},
array_filter(
$GLOBALS['LEAFLET_LIBRARIES'],
function ($library) {
return isset($library['name'], $library['license']);
2015-01-09 20:57:19 +01:00
}
)
);
}
2015-01-12 19:03:29 +01:00
/**
* Extract version and dependencies from composer.
*
* @return array
*/
private function extractFromComposer(): array
2015-01-09 20:57:19 +01:00
{
$extFile = $this->projectDir . '/vendor/netzmacht/contao-leaflet-maps/composer.json';
$lockFile = $this->projectDir . '/composer.lock';
2015-01-09 20:57:19 +01:00
2015-01-19 11:32:22 +01:00
if (!file_exists($extFile) || !file_exists($lockFile)) {
return [];
2015-01-09 20:57:19 +01:00
}
2015-01-19 11:32:22 +01:00
$extension = json_decode(file_get_contents($extFile), true);
$installed = json_decode(file_get_contents($lockFile), true);
$deps = [];
2015-01-09 20:57:19 +01:00
$version = null;
foreach ($installed['packages'] as $package) {
if ($package['name'] === 'netzmacht/contao-leaflet-maps') {
$version = $package['version'];
2015-01-19 11:32:22 +01:00
} elseif (isset($extension['require'][$package['name']])) {
$deps[] = [
2015-01-09 20:57:19 +01:00
'name' => $package['name'],
'version' => $package['version'],
'license' => !empty($package['license']) ? implode(', ', $package['license']) : '',
'homepage' => sprintf(
'<a href="https://packagist.org/packages/%s" target="_blank">Visit packagist</a>',
$package['name']
),
];
2015-01-09 20:57:19 +01:00
}
}
return [$version, $deps];
2015-01-09 20:57:19 +01:00
}
}