added plugin screen (#671)

This commit is contained in:
Kevin Papst
2019-04-08 18:38:56 +02:00
committed by GitHub
parent 5bea9915f5
commit 3a4aa5a001
68 changed files with 1392 additions and 490 deletions

83
src/Plugin/Plugin.php Normal file
View File

@@ -0,0 +1,83 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Plugin;
class Plugin
{
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $path;
/**
* @var PluginMetadata
*/
private $metadata;
/**
* @return PluginMetadata
*/
public function getMetadata(): ?PluginMetadata
{
return $this->metadata;
}
/**
* @param PluginMetadata $metadata
* @return Plugin
*/
public function setMetadata(PluginMetadata $metadata)
{
$this->metadata = $metadata;
return $this;
}
/**
* @return string
*/
public function getPath(): ?string
{
return $this->path;
}
/**
* @param string $path
* @return Plugin
*/
public function setPath(string $path)
{
$this->path = $path;
return $this;
}
/**
* @return string
*/
public function getName(): ?string
{
return $this->name;
}
/**
* @param string $name
* @return Plugin
*/
public function setName(string $name)
{
$this->name = $name;
return $this;
}
}

View File

@@ -0,0 +1,23 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Plugin;
interface PluginInterface
{
/**
* @return string
*/
public function getName();
/**
* @return string
*/
public function getPath();
}

View File

@@ -0,0 +1,111 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Plugin;
use App\Constants;
class PluginManager
{
/**
* @var Plugin[]
*/
private $plugins = [];
/**
* @param PluginInterface[] $plugins
* @throws \Exception
*/
public function __construct(iterable $plugins)
{
foreach ($plugins as $plugin) {
$this->addPlugin($plugin);
}
}
/**
* @param PluginInterface $plugin
* @throws \Exception
*/
public function addPlugin(PluginInterface $plugin)
{
if (isset($this->plugins[$plugin->getName()])) {
return;
}
$this->plugins[$plugin->getName()] = $this->createPlugin($plugin);
}
/**
* @return Plugin[]
*/
public function getPlugins()
{
return $this->plugins;
}
/**
* @param string $name
* @return Plugin|null
*/
public function getPlugin(string $name): ?Plugin
{
if (!isset($this->plugins[$name])) {
return null;
}
return $this->plugins[$name];
}
/**
* @param PluginInterface $bundle
* @return Plugin
*/
protected function createPlugin(PluginInterface $bundle)
{
$plugin = new Plugin();
$plugin
->setName($bundle->getName())
->setPath($bundle->getPath())
->setMetadata(new PluginMetadata())
;
return $plugin;
}
/**
* Call this method and pass a plugin, to set its metdata.
* This is not pre-filled by default, as it would mean to parse several composer.json on each request.
*
* @param Plugin $plugin
*/
public function loadMetadata(Plugin $plugin)
{
$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';
$version = $json['extra']['kimai']['version'] ?? 'unknown';
$description = $json['description'] ?? '';
$homepage = $json['homepage'] ?? Constants::HOMEPAGE . '/store/';
$plugin
->getMetadata()
->setHomepage($homepage)
->setKimaiVersion($reqVersion)
->setVersion($version)
->setDescription($description)
;
}
}

View File

@@ -0,0 +1,106 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Plugin;
class PluginMetadata
{
/**
* @var string
*/
private $version;
/**
* @var string
*/
private $kimaiVersion;
/**
* @var string
*/
private $homepage;
/**
* @var string
*/
private $description;
/**
* @return string
*/
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
{
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)
{
$this->homepage = $homepage;
return $this;
}
}