initial commit

This commit is contained in:
2025-07-17 12:59:14 +02:00
commit e55b359e44
11 changed files with 3864 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
vendor/
phpunit
.phpunit.result.cache
.phpunit.cache/

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
The MIT license
Copyright (c) 2025 Marco Grätsch <magdev3.0@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

11
README.md Normal file
View File

@@ -0,0 +1,11 @@
# Listmonk-API Bundle for Symfony
Symfony bundle to integrate [Listmonk](https://listmonk.app) in your application. Uses [magdev/listmonk-api](https://src.bundespruefstelle.ch/magdev/listmonk-api) to connect to Listmonk.
## Install
To install this bundle execute the following command in your Symfony project
```shell
composer require magdev/listmonk-api-bundle
```

39
composer.json Normal file
View File

@@ -0,0 +1,39 @@
{
"name": "magdev/listmonk-api-bundle",
"description": "Symfony bundle to integrate Listmonk in your applications",
"type": "symfony-bundle",
"license": "MIT",
"minimum-stability": "stable",
"authors": [
{
"name": "magdev",
"email": "magdev3.0@gmail.com"
}
],
"require": {
"magdev/listmonk-api": "dev-main"
},
"require-dev": {
"phpunit/phpunit": "^12.2",
"symfony/config": "^7.3",
"symfony/dependency-injection": "^7.3",
"symfony/http-kernel": "^7.3",
"symfony/phpunit-bridge": "^7.3"
},
"autoload": {
"psr-4": {
"Magdev\\ListmonkApiBundle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Magdev\\ListmonkApiBundleTest\\": "tests/"
}
},
"repositories": [
{
"type": "path",
"url": "../listmonk-api"
}
]
}

3539
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

29
phpunit.xml.dist Normal file
View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
>
<php>
<ini name="display_errors" value="1"/>
<ini name="error_reporting" value="-1"/>
<server name="APP_ENV" value="test" force="true"/>
<server name="SHELL_VERBOSITY" value="-1"/>
<server name="SYMFONY_PHPUNIT_REMOVE" value=""/>
</php>
<testsuites>
<testsuite name="Listmonk-API-Bundle Testsuite">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>

View File

@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace Magdev\ListmonkApiBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* @author Marco Grätsch <magdev3.0@gmail.com>
*/
final class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder('magdev_listmonk');
$rootNode = $treeBuilder->getRootNode();
$rootNode->fixXmlConfig('connection')
->children()
->arrayNode('connections')
->useAttributeAsKey('name')
->normalizeKeys(false)
->arrayPrototype()
->children()
->scalarNode('hostname')
->isRequired()
->info('The base URL of the Listmonk instance')
->end()
->scalarNode('username')
->isRequired()
->info('The Listmonk username')
->end()
->scalarNode('token')
->isRequired()
->info('The Listmonk token')
->end()
->booleanNode('verify_https')
->defaultTrue()
->info('Enable/disable HTTPS certificate verification')
->end()
->arrayNode('options')
->children()
->arrayNode('logger')
->children()
->scalarNode('filename')
->defaultValue('php://stdout')
->info('Logfile name')
->end()
->integerNode('level')
->defaultValue(100)
->info('Log level')
->end()
->end()
->end()
->end()
->end()
->end()
->end()
->end()
->scalarNode('default_connection')->end()
;
return $treeBuilder;
}
}

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace Magdev\ListmonkApiBundle\DependencyInjection;
use Magdev\ListmonkApi\ListmonkClient;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\Extension;
/**
* @author Marco Grätsch <magdev3.0@gmail.com>
*/
final class MagdevListmonkApiExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container)
{
$config = $this->processConfiguration(new Configuration(), $configs);
if (!isset($config['default_connection'])) {
$config['default_connection'] = array_key_first($config['connections']);
}
foreach ($config['connections'] as $name => $connection) {
$id = sprintf('magdev.listmonk.%s', $name);
$container->register($id, ListmonkClient::class)
->addMethodCall('connect', [
$connection['hostname'],
$connection['username'],
$connection['token'],
$connection['verify_https'],
])
->addMethodCall('setOptions', [$connection['options']]);
$container->registerAliasForArgument($id, ListmonkClient::class, "{$name}Client");
if ($name === $config['default_connection']) {
$container->setAlias(ListmonkClient::class, $id);
}
}
}
}

View File

@@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Magdev\ListmonkApiBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
/**
* @author Marco Grätsch <magdev3.0@gmail.com>
*/
final class MagdevListmonkApiBundle extends Bundle
{
}

View File

@@ -0,0 +1,52 @@
<?php declare(strict_types=1);
namespace Magdev\ListmonkApiBundle\Tests\DependencyInjection;
use Magdev\ListmonkApiBundle\DependencyInjection\Configuration;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Processor;
/**
* @author Marco Grätsch <magdev3.0@gmail.com>
*/
class ConfigurationTest extends TestCase
{
#[DataProvider('configsProvider')]
public function testConfig(array $configs): void
{
$config = (new Processor())->processConfiguration(new Configuration(), $configs);
$this->assertSame('https://localhost:8443', $config['connections']['default']['hostname']);
$this->assertSame('listmonk', $config['connections']['default']['username']);
$this->assertSame('listmonk', $config['connections']['default']['token']);
$this->assertTrue($config['connections']['default']['verify_https']);
$this->assertIsArray($config['connections']['default']['options']);
$this->assertIsArray($config['connections']['default']['options']['logger']);
$this->assertSame('php://stdout', $config['connections']['default']['options']['logger']['filename']);
$this->assertSame(100, $config['connections']['default']['options']['logger']['level']);
}
public static function configsProvider(): iterable
{
yield [[
'magdev_listmonk' => [
'connections' => [
'default' => [
'hostname' => 'https://localhost:8443',
'username' => 'listmonk',
'token' => 'listmonk',
'verify_https' => true,
'options' => [
'logger' => [
'filename' => 'php://stdout',
'level' => 100,
],
],
],
],
],
]];
}
}

View File

@@ -0,0 +1,49 @@
<?php declare(strict_types=1);
namespace Magdev\RedmineBundle\Tests\DependencyInjection;
use Magdev\ListmonkApi\ListmonkClient;
use Magdev\ListmonkApiBundle\DependencyInjection\MagdevListmonkApiExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpClient\ScopingHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* @author Marco Grätsch <magdev3.0@gmail.com>
*/
class MagdevListmonkApiExtensionTest extends TestCase
{
public function testExtension(): void
{
$container = new ContainerBuilder();
(new MagdevListmonkApiExtension())->load([
'magdev_listmonk' => [
'connections' => [
'default' => [
'hostname' => 'https://localhost:8443',
'username' => 'listmonk',
'token' => 'listmonk',
'verify_https' => false,
'options' => [
'logger' => [
'filename' => 'php://stdout',
'level' => 100,
],
],
],
],
],
], $container);
$client = $container->get('magdev.listmonk.default');
/** @var \Magdev\ListmonkApi\ListmonkClient $client */
$this->assertInstanceOf(ListmonkClient::class, $client);
$httpClient = $client->getClient();
/** @var \Symfony\Component\HttpClient\ScopingHttpClient $httpClient */
$this->assertInstanceOf(HttpClientInterface::class, $httpClient);
$this->assertInstanceOf(ScopingHttpClient::class, $httpClient);
}
}