Next major version 2 with PHP 8.1, Symfony 6, Tabler UI, 2FA ... (#2902)

This commit is contained in:
Kevin Papst
2022-12-31 21:19:55 +01:00
committed by GitHub
parent 95e06746bd
commit 90a0fd8a22
2164 changed files with 83700 additions and 86426 deletions

View File

@@ -14,16 +14,16 @@ use Symfony\Component\HttpKernel\DependencyInjection\Extension;
abstract class AbstractPluginExtension extends Extension
{
protected function registerBundleConfiguration(ContainerBuilder $container, array $configs)
protected function registerBundleConfiguration(ContainerBuilder $container, array $configs): void
{
$bundleConfig = [$this->getAlias() => $configs];
/* @phpstan-ignore-next-line */
if ($container->hasParameter('kimai.bundles.config')) {
$bundleConfig = array_merge(
$container->getParameter('kimai.bundles.config'),
$bundleConfig
);
if ($container->hasParameter('kimai.bundles.config')) { // @phpstan-ignore-line
$config = $container->getParameter('kimai.bundles.config');
if (!\is_array($config)) {
throw new \Exception('Invalid bundle configuration registered for ' . $this->getAlias());
}
$bundleConfig = array_merge($config, $bundleConfig);
}
$container->setParameter('kimai.bundles.config', $bundleConfig);

View File

@@ -9,70 +9,44 @@
namespace App\Plugin;
class Plugin
final class Plugin
{
/**
* @var string
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $path;
/**
* @var PluginMetadata
*/
private $metadata;
private string $id;
private string $path;
private ?PluginMetadata $metadata = null;
public function __construct(PluginInterface $bundle)
{
$this->id = $bundle->getName();
$this->path = $bundle->getPath();
}
public function getMetadata(): ?PluginMetadata
{
return $this->metadata;
}
public function setMetadata(PluginMetadata $metadata): Plugin
public function setMetadata(PluginMetadata $metadata): void
{
$this->metadata = $metadata;
return $this;
}
public function getPath(): ?string
public function getPath(): string
{
return $this->path;
}
public function setPath(string $path): Plugin
public function getName(): string
{
$this->path = $path;
if ($this->metadata !== null && $this->metadata->getName() !== null) {
return $this->metadata->getName();
}
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): Plugin
{
$this->name = $name;
return $this;
}
public function getId(): ?string
{
return $this->id;
}
public function setId(string $id): Plugin
public function getId(): string
{
$this->id = $id;
return $this;
return $this->id;
}
}

View File

@@ -11,13 +11,7 @@ namespace App\Plugin;
interface PluginInterface
{
/**
* @return string
*/
public function getName();
public function getName(): string;
/**
* @return string
*/
public function getPath();
public function getPath(): string;
}

View File

@@ -9,73 +9,54 @@
namespace App\Plugin;
use App\Constants;
class PluginManager
final class PluginManager
{
/**
* @var Plugin[]
* @var array<Plugin>|null
*/
private $plugins = [];
private ?array $plugins = null;
/**
* @var iterable<PluginInterface>
*/
private iterable $bundles;
/**
* @param PluginInterface[] $plugins
* @param iterable<PluginInterface> $plugins
*/
public function __construct(iterable $plugins)
{
foreach ($plugins as $plugin) {
$this->addPlugin($plugin);
}
}
/**
* @param PluginInterface $plugin
*/
public function addPlugin(PluginInterface $plugin)
{
if (isset($this->plugins[$plugin->getName()])) {
return;
}
$this->plugins[$plugin->getName()] = $this->createPlugin($plugin);
$this->bundles = $plugins;
}
/**
* @return Plugin[]
*/
public function getPlugins()
public function getPlugins(): array
{
if ($this->plugins === null) {
$plugins = [];
foreach ($this->bundles as $bundle) {
$plugins[$bundle->getName()] = new Plugin($bundle);
}
$this->plugins = array_values($plugins);
}
return $this->plugins;
}
/**
* @param string $name
* @return Plugin|null
*/
public function getPlugin(string $name): ?Plugin
{
if (!isset($this->plugins[$name])) {
return null;
$plugins = $this->getPlugins();
foreach ($plugins as $plugin) {
if ($plugin->getName() === $name) {
return $plugin;
}
}
return $this->plugins[$name];
}
/**
* @param PluginInterface $bundle
* @return Plugin
*/
protected function createPlugin(PluginInterface $bundle)
{
$plugin = new Plugin();
$plugin
->setId($bundle->getName())
->setName($bundle->getName())
->setPath($bundle->getPath())
->setMetadata(new PluginMetadata())
;
return $plugin;
return null;
}
/**
@@ -83,33 +64,11 @@ class PluginManager
* This is not pre-filled by default, as it would mean to parse several composer.json on each request.
*
* @param Plugin $plugin
* @throws \Exception
*/
public function loadMetadata(Plugin $plugin)
public function loadMetadata(Plugin $plugin): void
{
$composer = $plugin->getPath() . '/composer.json';
if (!file_exists($composer) || !is_readable($composer)) {
return;
}
$json = json_decode(file_get_contents($composer), true);
$reqVersion = $json['extra']['kimai']['require'] ?? 'unknown';
// the version field is required if we use composer to install a plugin via var/packages/
$version = $json['extra']['kimai']['version'] ?? ($json['version'] ?? 'unknown');
$description = $json['description'] ?? '';
$homepage = $json['homepage'] ?? Constants::HOMEPAGE . '/store/';
if (\array_key_exists('name', $json['extra']['kimai'])) {
$plugin->setName($json['extra']['kimai']['name']);
}
$plugin
->getMetadata()
->setHomepage($homepage)
->setKimaiVersion($reqVersion)
->setVersion($version)
->setDescription($description)
;
$meta = PluginMetadata::loadFromComposer($plugin->getPath());
$plugin->setMetadata($meta);
}
}

View File

@@ -9,98 +9,90 @@
namespace App\Plugin;
use App\Constants;
class PluginMetadata
{
/**
* @var string
*/
private $version;
/**
* @var string
*/
private $kimaiVersion;
/**
* @var string
*/
private $homepage;
/**
* @var string
*/
private $description;
private ?string $version = null;
private ?int $kimaiVersion = null;
private ?string $homepage = null;
private ?string $description = null;
private ?string $name = null;
/**
* @return string
* @param string $path
* @return PluginMetadata
* @throws \Exception
*/
public static function loadFromComposer(string $path): PluginMetadata
{
if (!is_dir($path) || !is_readable($path)) {
throw new \Exception(sprintf('Bundle directory "%s" cannot be accessed.', $path));
}
$pluginName = basename($path);
$composer = $path . '/composer.json';
if (!file_exists($composer) || !is_readable($composer)) {
throw new \Exception(sprintf('Bundle "%s" does not ship composer.json, which is required since 2.0.', $pluginName));
}
$json = json_decode(file_get_contents($composer), true);
if (!\array_key_exists('extra', $json)) {
throw new \Exception(sprintf('Bundle "%s" does not define an "extra" node in composer.json, which is required since 2.0.', $pluginName));
}
if (!\array_key_exists('kimai', $json['extra'])) {
throw new \Exception(sprintf('Bundle "%s" does not define the "extra.kimai" node in composer.json, which is required since 2.0.', $pluginName));
}
if (!\array_key_exists('require', $json['extra']['kimai'])) {
throw new \Exception(sprintf('Bundle "%s" does not define the minimum Kimai version in "extra.kimai.required" in composer.json, which is required since 2.0.', $pluginName));
}
if (!\array_key_exists('name', $json['extra']['kimai'])) {
throw new \Exception(sprintf('Bundle "%s" does not define its name in "extra.kimai.name" in composer.json, which is required since 2.0.', $pluginName));
}
if (!\is_int($json['extra']['kimai']['require'])) {
throw new \Exception(sprintf('Bundle "%s" defines an invalid Kimai minimum version in extra.kimai.require. Please provide an integer as in Constants::VERSION_ID.', $pluginName));
}
$meta = new self();
$meta->description = $json['description'] ?? '';
$meta->homepage = $json['homepage'] ?? Constants::HOMEPAGE . '/store/';
$meta->name = $json['extra']['kimai']['name'];
$meta->kimaiVersion = $json['extra']['kimai']['require'];
// the version field is required if we use composer to install a plugin via var/packages/
$meta->version = $json['extra']['kimai']['version'] ?? ($json['version'] ?? 'unknown');
return $meta;
}
public function getDescription(): ?string
{
return $this->description;
}
/**
* @param string $description
* @return PluginMetadata
*/
public function setDescription(string $description)
{
$this->description = $description;
return $this;
}
/**
* @return string
*/
public function getVersion(): ?string
{
return $this->version;
}
/**
* @param string $version
* @return PluginMetadata
*/
public function setVersion(string $version)
{
$this->version = $version;
return $this;
}
/**
* @return string
*/
public function getKimaiVersion(): ?string
public function getKimaiVersion(): ?int
{
return $this->kimaiVersion;
}
/**
* @param string $kimaiVersion
* @return PluginMetadata
*/
public function setKimaiVersion(string $kimaiVersion)
{
$this->kimaiVersion = $kimaiVersion;
return $this;
}
/**
* @return string
*/
public function getHomepage(): ?string
{
return $this->homepage;
}
/**
* @param string $homepage
* @return PluginMetadata
*/
public function setHomepage(string $homepage)
public function getName(): ?string
{
$this->homepage = $homepage;
return $this;
return $this->name;
}
}