use Mezzio for application.

This commit is contained in:
Dave Smith-Hayes 2024-02-21 22:14:42 -05:00
parent fff341d3cb
commit af23541644
59 changed files with 8599 additions and 267 deletions

7
app/.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
/.phpcs-cache
/.psalm-cache
/.phpunit.cache
/clover.xml
/coveralls-upload.json
/phpunit.xml
/vendor/

5
app/.laminas-ci/pre-run.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
# Due to the fact that we are disabling plugins when installing/updating/downgrading composer dependencies
# we have to manually enable the coding standard here.
composer enable-codestandard

1
app/COPYRIGHT.md Normal file
View File

@ -0,0 +1 @@
Copyright (c) 2020 Laminas Project a Series of LF Projects, LLC. (https://getlaminas.org/)

26
app/LICENSE.md Normal file
View File

@ -0,0 +1,26 @@
Copyright (c) 2020 Laminas Project a Series of LF Projects, LLC.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
- Neither the name of Laminas Foundation nor the names of its contributors may
be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

159
app/README.md Normal file
View File

@ -0,0 +1,159 @@
# Mezzio Skeleton and Installer
[![Build Status](https://github.com/mezzio/mezzio-skeleton/actions/workflows/continuous-integration.yml/badge.svg)](https://github.com/mezzio/mezzio-skeleton/actions/workflows/continuous-integration.yml)
> ## 🇷🇺 Русским гражданам
>
> Мы, участники Laminas, родились и живем в разных странах. У многих из нас есть друзья, родственники и коллеги как в России, так и в Украине. Некоторые из нас родились в России. Некоторые из нас живут в России. У некоторых бабушки и дедушки сражались с фашистами во Второй мировой войне. Здесь никто не поддерживает фашизм.
>
> У одного из нас есть украинская родственница, которая спаслась из дома вместе с сыном. Поезд задержался из-за бомбежки на дороге впереди. У нас есть друзья, которые прячутся в бомбоубежищах. Мы с тревогой ждем весточки от них после воздушных налетов, которые беспорядочно наносят удары и попадают по больницам, школам, детским садам и домам. Мы не берем это из каких-либо СМИ. Мы наблюдаем это напрямую.
>
> Вы доверяете нам достаточно, чтоб использовать наши программы, и мы просим вас довериться нам вновь. Мы нуждаемся в помощи. Выходите и протестуйте против этой бесполезной войны. Остановите кровопролитие. Скажите "Нет войне!"
>
> ## 🇺🇸 To Citizens of Russia
>
> We at Laminas come from all over the world. Many of us have friends, family and colleagues in both Russia and Ukraine. Some of us were born in Russia. Some of us currently live in Russia. Some have grandparents who fought Nazis in World War II. Nobody here supports fascism.
>
> One team member has a Ukrainian relative who fled her home with her son. The train was delayed due to bombing on the road ahead. We have friends who are hiding in bomb shelters. We anxiously follow up on them after the air raids, which indiscriminately fire at hospitals, schools, kindergartens and houses. We're not taking this from any media. These are our actual experiences.
>
> You trust us enough to use our software. We ask that you trust us to say the truth on this. We need your help. Go out and protest this unnecessary war. Stop the bloodshed. Say "stop the war!"
*Begin developing PSR-15 middleware applications in seconds!*
[mezzio](https://github.com/mezzio/mezzio) builds on
[laminas-stratigility](https://github.com/laminas/laminas-stratigility) to
provide a minimalist PSR-15 middleware framework for PHP with routing, DI
container, optional templating, and optional error handling capabilities.
This installer will setup a skeleton application based on mezzio by
choosing optional packages based on user input as demonstrated in the following
screenshot:
![screenshot-installer](https://user-images.githubusercontent.com/1011217/90332191-55d32200-dfbb-11ea-80c0-27a07ef5691a.png)
The user selected packages are saved into `composer.json` so that everyone else
working on the project have the same packages installed. Configuration files and
templates are prepared for first use. The installer command is removed from
`composer.json` after setup succeeded, and all installer related files are
removed.
## Getting Started
Start your new Mezzio project with composer:
```bash
$ composer create-project mezzio/mezzio-skeleton <project-path>
```
After choosing and installing the packages you want, go to the
`<project-path>` and start PHP's built-in web server to verify installation:
```bash
$ composer serve
```
You can then browse to http://localhost:8080.
## Installing alternative packages
There is a feature to install alternative packages: Instead of entering one of
the selection **you can actually type the package name and version**.
> ```text
> Which template engine do you want to use?
> [1] Plates
> [2] Twig
> [3] zend-view installs zend-servicemanager
> [n] None of the above
> Make your selection or type a composer package name and version (n): infw/pug:0.1
> - Searching for infw/pug:0.1
> - Adding package infw/pug (0.1)
> ```
That feature allows you to install any alternative package you want. It has its limitations though:
* The alternative package must follow this format `namespace/package:1.0`. It needs the correct version.
* Templates are not copied, but the ConfigProvider can be configured in such way that it uses the
default templates directly from the package itself.
* This doesn't work for containers as the container.php file needs to be copied.
## Troubleshooting
If the installer fails during the ``composer create-project`` phase, please go
through the following list before opening a new issue. Most issues we have seen
so far can be solved by `self-update` and `clear-cache`.
1. Be sure to work with the latest version of composer by running `composer self-update`.
2. Try clearing Composer's cache by running `composer clear-cache`.
If neither of the above help, you might face more serious issues:
* Info about the [zlib_decode error](https://github.com/composer/composer/issues/4121).
* Info and solutions for [composer degraded mode](https://getcomposer.org/doc/articles/troubleshooting.md#degraded-mode).
## Application Development Mode Tool
This skeleton comes with [laminas-development-mode](https://github.com/laminas/laminas-development-mode).
It provides a composer script to allow you to enable and disable development mode.
### To enable development mode
**Note:** Do NOT run development mode on your production server!
```bash
$ composer development-enable
```
**Note:** Enabling development mode will also clear your configuration cache, to
allow safely updating dependencies and ensuring any new configuration is picked
up by your application.
### To disable development mode
```bash
$ composer development-disable
```
### Development mode status
```bash
$ composer development-status
```
## Configuration caching
By default, the skeleton will create a configuration cache in
`data/config-cache.php`. When in development mode, the configuration cache is
disabled, and switching in and out of development mode will remove the
configuration cache.
You may need to clear the configuration cache in production when deploying if
you deploy to the same directory. You may do so using the following:
```bash
$ composer clear-config-cache
```
You may also change the location of the configuration cache itself by editing
the `config/config.php` file and changing the `config_cache_path` entry of the
local `$cacheConfig` variable.
## Skeleton Development
This section applies only if you cloned this repo with `git clone`, not when you
installed mezzio with `composer create-project ...`.
If you want to run tests against the installer, you need to clone this repo and
setup all dependencies with composer. Make sure you **prevent composer running
scripts** with `--no-scripts`, otherwise it will remove the installer and all
tests.
```bash
$ composer update --no-scripts
$ composer test
```
Please note that the installer tests remove installed config files and templates
before and after running the tests.
Before contributing read [the contributing guide](https://github.com/mezzio/.github/blob/master/CONTRIBUTING.md).

View File

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
chdir(__DIR__ . '/../');
require 'vendor/autoload.php';
$config = include 'config/config.php';
if (! isset($config['config_cache_path'])) {
echo "No configuration cache path found" . PHP_EOL;
exit(0);
}
if (! file_exists($config['config_cache_path'])) {
printf(
"Configured config cache file '%s' not found%s",
$config['config_cache_path'],
PHP_EOL
);
exit(0);
}
if (false === unlink($config['config_cache_path'])) {
printf(
"Error removing config cache file '%s'%s",
$config['config_cache_path'],
PHP_EOL
);
exit(1);
}
printf(
"Removed configured config cache file '%s'%s",
$config['config_cache_path'],
PHP_EOL
);
exit(0);

113
app/composer.json Normal file
View File

@ -0,0 +1,113 @@
{
"name": "mezzio/mezzio-skeleton",
"description": "Laminas mezzio skeleton. Begin developing PSR-15 middleware applications in seconds!",
"type": "project",
"license": "BSD-3-Clause",
"keywords": [
"laminas",
"mezzio",
"skeleton",
"middleware",
"psr",
"psr-7",
"psr-11",
"psr-15"
],
"homepage": "https://mezzio.dev",
"support": {
"docs": "https://docs.mezzio.dev/mezzio/",
"issues": "https://github.com/mezzio/mezzio-skeleton/issues",
"source": "https://github.com/mezzio/mezzio-skeleton",
"rss": "https://github.com/mezzio/mezzio-skeleton/releases.atom",
"chat": "https://laminas.dev/chat",
"forum": "https://discourse.laminas.dev"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"composer/package-versions-deprecated": true,
"laminas/laminas-component-installer": true
},
"platform": {
"php": "8.1.99"
}
},
"extra": {
"laminas": {
"component-whitelist": [
"mezzio/mezzio",
"mezzio/mezzio-helpers",
"mezzio/mezzio-router",
"laminas/laminas-httphandlerrunner",
"mezzio/mezzio-fastroute",
"mezzio/mezzio-platesrenderer"
]
}
},
"require": {
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"composer/package-versions-deprecated": "^1.10.99",
"laminas/laminas-component-installer": "^2.6 || ^3.0",
"laminas/laminas-config-aggregator": "^1.6",
"laminas/laminas-diactoros": "^3.0.0",
"laminas/laminas-stdlib": "^3.6",
"mezzio/mezzio": "^3.7",
"mezzio/mezzio-helpers": "^5.7",
"elie29/zend-phpdi-config": "^9.0",
"mezzio/mezzio-fastroute": "^3.11.0",
"mezzio/mezzio-platesrenderer": "^2.10"
},
"require-dev": {
"laminas/laminas-coding-standard": "~2.5.0",
"laminas/laminas-development-mode": "^3.12.0",
"mezzio/mezzio-tooling": "^2.9",
"phpunit/phpunit": "^10.5.5",
"psalm/plugin-phpunit": "^0.18.4",
"roave/security-advisories": "dev-master",
"vimeo/psalm": "^5.18",
"filp/whoops": "^2.15.4"
},
"autoload": {
"psr-4": {
"App\\": "src/App/src/"
}
},
"autoload-dev": {
"psr-4": {
"AppTest\\": "test/AppTest/"
}
},
"scripts": {
"post-create-project-cmd": [
"@development-enable"
],
"post-install-cmd": "@clear-config-cache",
"post-update-cmd": "@clear-config-cache",
"development-disable": "laminas-development-mode disable",
"development-enable": "laminas-development-mode enable",
"development-status": "laminas-development-mode status",
"mezzio": "laminas --ansi",
"check": [
"@cs-check",
"@test"
],
"clear-config-cache": "php bin/clear-config-cache.php",
"enable-codestandard": "Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin::run",
"cs-check": "phpcs",
"cs-fix": "phpcbf",
"serve": [
"Composer\\Config::disableProcessTimeout",
"php -S 0.0.0.0:8080 -t public/"
],
"static-analysis": "psalm --stats",
"static-analysis-update-baseline": "psalm --stats --update-baseline",
"test": "phpunit --colors=always",
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml"
},
"scripts-descriptions": {
"clear-config-cache": "Clears merged config cache. Required for config changes to be applied.",
"static-analysis": "Run static analysis tool Psalm.",
"static-analysis-update-baseline": "Run static analysis tool Psalm and update baseline."
}
}

7136
app/composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

1
app/config/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
development.config.php

2
app/config/autoload/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
local.php
*.local.php

View File

@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
return [
// Provides application-wide services.
// We recommend using fully-qualified class names whenever possible as
// service names.
'dependencies' => [
// Use 'aliases' to alias a service name to another service. The
// key is the alias name, the value is the service to which it points.
'aliases' => [
// Fully\Qualified\ClassOrInterfaceName::class => Fully\Qualified\ClassName::class,
],
// Use 'invokables' for constructor-less services, or services that do
// not require arguments to the constructor. Map a service name to the
// class name.
'invokables' => [
// Fully\Qualified\InterfaceName::class => Fully\Qualified\ClassName::class,
],
// Use 'factories' for services provided by callbacks/factory classes.
'factories' => [
// Fully\Qualified\ClassName::class => Fully\Qualified\FactoryName::class,
],
],
];

View File

@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
// phpcs:disable PSR12.Files.FileHeader.IncorrectOrder
/**
* Development-only configuration.
*
* Put settings you want enabled when under development mode in this file, and
* check it into your repository.
*
* Developers on your team will then automatically enable them by calling on
* `composer development-enable`.
*/
use Mezzio\Container;
use Mezzio\Middleware\ErrorResponseGenerator;
return [
'dependencies' => [
'factories' => [
ErrorResponseGenerator::class => Container\WhoopsErrorResponseGeneratorFactory::class,
'Mezzio\Whoops' => Container\WhoopsFactory::class,
'Mezzio\WhoopsPageHandler' => Container\WhoopsPageHandlerFactory::class,
],
],
'whoops' => [
'json_exceptions' => [
'display' => true,
'show_trace' => true,
'ajax_only' => true,
],
],
];

View File

@ -0,0 +1,13 @@
<?php
/**
* Local configuration.
*
* Copy this file to `local.php` and change its settings as required.
* `local.php` is ignored by git and safe to use for local and sensitive data like usernames and passwords.
*/
declare(strict_types=1);
return [
];

View File

@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
use Laminas\ConfigAggregator\ConfigAggregator;
return [
// Toggle the configuration cache. Set this to boolean false, or remove the
// directive, to disable configuration caching. Toggling development mode
// will also disable it by default; clear the configuration cache using
// `composer clear-config-cache`.
ConfigAggregator::ENABLE_CACHE => true,
// Enable debugging; typically used to provide debugging information within templates.
'debug' => false,
'mezzio' => [
// Provide templates for the error handling middleware to use when
// generating responses.
'error_handler' => [
'template_404' => 'error::404',
'template_error' => 'error::error',
],
],
];

51
app/config/config.php Normal file
View File

@ -0,0 +1,51 @@
<?php
declare(strict_types=1);
use Laminas\ConfigAggregator\ArrayProvider;
use Laminas\ConfigAggregator\ConfigAggregator;
use Laminas\ConfigAggregator\PhpFileProvider;
use Mezzio\Helper\ConfigProvider;
// To enable or disable caching, set the `ConfigAggregator::ENABLE_CACHE` boolean in
// `config/autoload/local.php`.
$cacheConfig = [
'config_cache_path' => 'data/cache/config-cache.php',
];
$aggregator = new ConfigAggregator([
\Mezzio\Tooling\ConfigProvider::class,
\Mezzio\Plates\ConfigProvider::class,
\Mezzio\Helper\ConfigProvider::class,
\Mezzio\Router\FastRouteRouter\ConfigProvider::class,
\Laminas\HttpHandlerRunner\ConfigProvider::class,
// Include cache configuration
new ArrayProvider($cacheConfig),
ConfigProvider::class,
\Mezzio\ConfigProvider::class,
\Mezzio\Router\ConfigProvider::class,
\Laminas\Diactoros\ConfigProvider::class,
// Swoole config to overwrite some services (if installed)
class_exists(\Mezzio\Swoole\ConfigProvider::class)
? \Mezzio\Swoole\ConfigProvider::class
: function (): array {
return [];
},
// Default App module config
App\ConfigProvider::class,
// Load application config in a pre-defined order in such a way that local settings
// overwrite global settings. (Loaded as first to last):
// - `global.php`
// - `*.global.php`
// - `local.php`
// - `*.local.php`
new PhpFileProvider(realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php'),
// Load development config if it exists
new PhpFileProvider(realpath(__DIR__) . '/development.config.php'),
], $cacheConfig['config_cache_path']);
return $aggregator->getMergedConfig();

15
app/config/container.php Normal file
View File

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
use Elie\PHPDI\Config\Config;
use Elie\PHPDI\Config\ContainerFactory;
use Psr\Container\ContainerInterface;
// Protect variables from global scope
return (static function (): ContainerInterface {
$config = require __DIR__ . '/config.php';
$factory = new ContainerFactory();
return $factory(new Config($config));
})();

View File

@ -0,0 +1,31 @@
<?php
/**
* File required to allow enablement of development mode.
*
* For use with the laminas-development-mode tool.
*
* Usage:
* $ composer development-disable
* $ composer development-enable
* $ composer development-status
*
* DO NOT MODIFY THIS FILE.
*
* Provide your own development-mode settings by editing the file
* `config/autoload/development.local.php.dist`.
*
* Because this file is aggregated last, it simply ensures:
*
* - The `debug` flag is _enabled_.
* - Configuration caching is _disabled_.
*/
declare(strict_types=1);
use Laminas\ConfigAggregator\ConfigAggregator;
return [
'debug' => true,
ConfigAggregator::ENABLE_CACHE => false,
];

77
app/config/pipeline.php Normal file
View File

@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
use Laminas\Stratigility\Middleware\ErrorHandler;
use Mezzio\Application;
use Mezzio\Handler\NotFoundHandler;
use Mezzio\Helper\ServerUrlMiddleware;
use Mezzio\Helper\UrlHelperMiddleware;
use Mezzio\MiddlewareFactory;
use Mezzio\Router\Middleware\DispatchMiddleware;
use Mezzio\Router\Middleware\ImplicitHeadMiddleware;
use Mezzio\Router\Middleware\ImplicitOptionsMiddleware;
use Mezzio\Router\Middleware\MethodNotAllowedMiddleware;
use Mezzio\Router\Middleware\RouteMiddleware;
use Psr\Container\ContainerInterface;
/**
* Setup middleware pipeline:
*/
return function (Application $app, MiddlewareFactory $factory, ContainerInterface $container): void {
// The error handler should be the first (most outer) middleware to catch
// all Exceptions.
$app->pipe(ErrorHandler::class);
$app->pipe(ServerUrlMiddleware::class);
// Pipe more middleware here that you want to execute on every request:
// - bootstrapping
// - pre-conditions
// - modifications to outgoing responses
//
// Piped Middleware may be either callables or service names. Middleware may
// also be passed as an array; each item in the array must resolve to
// middleware eventually (i.e., callable or service name).
//
// Middleware can be attached to specific paths, allowing you to mix and match
// applications under a common domain. The handlers in each middleware
// attached this way will see a URI with the matched path segment removed.
//
// i.e., path of "/api/member/profile" only passes "/member/profile" to $apiMiddleware
// - $app->pipe('/api', $apiMiddleware);
// - $app->pipe('/docs', $apiDocMiddleware);
// - $app->pipe('/files', $filesMiddleware);
// Register the routing middleware in the middleware pipeline.
// This middleware registers the Mezzio\Router\RouteResult request attribute.
$app->pipe(RouteMiddleware::class);
// The following handle routing failures for common conditions:
// - HEAD request but no routes answer that method
// - OPTIONS request but no routes answer that method
// - method not allowed
// Order here matters; the MethodNotAllowedMiddleware should be placed
// after the Implicit*Middleware.
$app->pipe(ImplicitHeadMiddleware::class);
$app->pipe(ImplicitOptionsMiddleware::class);
$app->pipe(MethodNotAllowedMiddleware::class);
// Seed the UrlHelper with the routing results:
$app->pipe(UrlHelperMiddleware::class);
// Add more middleware here that needs to introspect the routing results; this
// might include:
//
// - route-based authentication
// - route-based validation
// - etc.
// Register the dispatch middleware in the middleware pipeline
$app->pipe(DispatchMiddleware::class);
// At this point, if no Response is returned by any middleware, the
// NotFoundHandler kicks in; alternately, you can provide other fallback
// middleware to execute.
$app->pipe(NotFoundHandler::class);
};

43
app/config/routes.php Normal file
View File

@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
use Mezzio\Application;
use Mezzio\MiddlewareFactory;
use Psr\Container\ContainerInterface;
/**
* FastRoute route configuration
*
* @see https://github.com/nikic/FastRoute
*
* Setup routes with a single request method:
*
* $app->get('/', App\Handler\HomePageHandler::class, 'home');
* $app->post('/album', App\Handler\AlbumCreateHandler::class, 'album.create');
* $app->put('/album/{id:\d+}', App\Handler\AlbumUpdateHandler::class, 'album.put');
* $app->patch('/album/{id:\d+}', App\Handler\AlbumUpdateHandler::class, 'album.patch');
* $app->delete('/album/{id:\d+}', App\Handler\AlbumDeleteHandler::class, 'album.delete');
*
* Or with multiple request methods:
*
* $app->route('/contact', App\Handler\ContactHandler::class, ['GET', 'POST', ...], 'contact');
*
* Or handling all request methods:
*
* $app->route('/contact', App\Handler\ContactHandler::class)->setName('contact');
*
* or:
*
* $app->route(
* '/contact',
* App\Handler\ContactHandler::class,
* Mezzio\Router\Route::HTTP_METHOD_ANY,
* 'contact'
* );
*/
return static function (Application $app, MiddlewareFactory $factory, ContainerInterface $container): void {
$app->get('/', App\Handler\HomePageHandler::class, 'home');
$app->get('/api/ping', App\Handler\PingHandler::class, 'api.ping');
};

4
app/data/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*
!cache
!cache/.gitkeep
!.gitignore

32
app/phpcs.xml.dist Normal file
View File

@ -0,0 +1,32 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
<arg name="basepath" value="."/>
<arg name="cache" value=".phpcs-cache"/>
<arg name="colors"/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="80"/>
<!-- Show progress -->
<arg value="p"/>
<!-- Paths to check -->
<file>config</file>
<file>src</file>
<file>test</file>
<exclude-pattern>config/config.php</exclude-pattern>
<exclude-pattern>config/routes.php</exclude-pattern>
<!-- Include all rules from the Laminas Coding Standard -->
<rule ref="LaminasCodingStandard"/>
<rule ref="Squiz.Classes.ClassFileName.NoMatch">
<exclude-pattern>src/ConfigProvider.*.php</exclude-pattern>
</rule>
<rule ref="PSR12.Files.FileHeader.IncorrectOrder">
<exclude-pattern>config/pipeline.php</exclude-pattern>
<exclude-pattern>src/MezzioInstaller/Resources/config/routes-*.php</exclude-pattern>
</rule>
</ruleset>

26
app/phpunit.xml.dist Normal file
View File

@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
displayDetailsOnIncompleteTests="true"
displayDetailsOnSkippedTests="true"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerErrors="true"
displayDetailsOnTestsThatTriggerNotices="true"
displayDetailsOnTestsThatTriggerWarnings="true"
colors="true"
>
<testsuites>
<testsuite name="default">
<directory>./test</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src</directory>
</include>
</source>
</phpunit>

295
app/psalm-baseline.xml Normal file
View File

@ -0,0 +1,295 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.18.0@b113f3ed0259fd6e212d87c3df80eec95a6abf19">
<file src="src/MezzioInstaller/OptionalPackages.php">
<ArgumentTypeCoercion>
<code><![CDATA[$this->stabilityFlags]]></code>
</ArgumentTypeCoercion>
<MixedArgument>
<code>$answer</code>
<code>$constraint</code>
<code>$filename</code>
<code>$filename</code>
<code>$package</code>
<code>$target</code>
<code><![CDATA[$this->composerDefinition['autoload']]]></code>
<code><![CDATA[$this->composerDefinition['autoload-dev']]]></code>
<code>$word</code>
</MixedArgument>
<MixedArgumentTypeCoercion>
<code>$package</code>
</MixedArgumentTypeCoercion>
<MixedArrayAccess>
<code><![CDATA[$this->composerDefinition['autoload']['psr-4']]]></code>
<code><![CDATA[$this->composerDefinition['autoload']['psr-4']['MezzioInstaller\\']]]></code>
<code><![CDATA[$this->composerDefinition['autoload-dev']['psr-4']]]></code>
<code><![CDATA[$this->composerDefinition['autoload-dev']['psr-4']['MezzioInstallerTest\\']]]></code>
<code><![CDATA[$this->composerDefinition['scripts']['pre-install-cmd']]]></code>
<code><![CDATA[$this->composerDefinition['scripts']['pre-update-cmd']]]></code>
</MixedArrayAccess>
<MixedArrayAssignment>
<code><![CDATA[$this->composerDefinition['autoload']['psr-4']]]></code>
<code><![CDATA[$this->composerDefinition['autoload']['psr-4']['App\\']]]></code>
<code><![CDATA[$this->composerDefinition['require'][$packageName]]]></code>
<code><![CDATA[$this->composerDefinition['require-dev'][$packageName]]]></code>
<code><![CDATA[$this->composerDefinition['scripts']['cs-check']]]></code>
<code><![CDATA[$this->composerDefinition['scripts']['cs-fix']]]></code>
</MixedArrayAssignment>
<MixedAssignment>
<code>$answer</code>
<code>$answer</code>
<code>$constraint</code>
<code>$fileInfo</code>
<code>$filename</code>
<code>$package</code>
<code>$target</code>
<code><![CDATA[$this->composerDefinition]]></code>
</MixedAssignment>
<MixedMethodCall>
<code>isDir</code>
</MixedMethodCall>
<MixedPropertyTypeCoercion>
<code><![CDATA[$this->composerDefinition]]></code>
<code><![CDATA[$this->composerDefinition]]></code>
<code><![CDATA[$this->composerDefinition]]></code>
</MixedPropertyTypeCoercion>
<NoValue>
<code><![CDATA[$this->installType]]></code>
</NoValue>
<PossiblyInvalidArgument>
<code>$resource</code>
</PossiblyInvalidArgument>
<PossiblyInvalidIterator>
<code><![CDATA[$question['options'][$answer][$this->installType]]]></code>
</PossiblyInvalidIterator>
<PossiblyNullArgument>
<code>$content</code>
</PossiblyNullArgument>
<PossiblyUnusedMethod>
<code>install</code>
</PossiblyUnusedMethod>
<PropertyTypeCoercion>
<code><![CDATA[require __DIR__ . '/config.php']]></code>
</PropertyTypeCoercion>
</file>
<file src="test/AppTest/Handler/PingHandlerTest.php">
<MixedArgument>
<code>$json</code>
</MixedArgument>
<MixedAssignment>
<code>$json</code>
</MixedAssignment>
<MixedPropertyFetch>
<code><![CDATA[$json->ack]]></code>
</MixedPropertyFetch>
</file>
<file src="test/MezzioInstallerTest/AddPackageTest.php">
<MixedArgument>
<code>$stabilityFlags</code>
</MixedArgument>
<MixedAssignment>
<code>$stabilityFlags</code>
</MixedAssignment>
<MixedInferredReturnType>
<code>array</code>
</MixedInferredReturnType>
</file>
<file src="test/MezzioInstallerTest/ContainersTest.php">
<MixedArgument>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['router']]]></code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['router']]]></code>
</MixedArrayAccess>
</file>
<file src="test/MezzioInstallerTest/ErrorHandlerTest.php">
<MixedArgument>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['error-handler']]]></code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$config['dependencies']]]></code>
<code><![CDATA[$config['dependencies']['factories']]]></code>
<code><![CDATA[$config['dependencies']['factories'][ErrorResponseGenerator::class]]]></code>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['error-handler']]]></code>
</MixedArrayAccess>
<MixedAssignment>
<code>$config</code>
</MixedAssignment>
<MixedInferredReturnType>
<code>array</code>
</MixedInferredReturnType>
</file>
<file src="test/MezzioInstallerTest/HomePageResponseTest.php">
<MixedArgument>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['router']]]></code>
<code><![CDATA[$config['questions']['router']]]></code>
<code><![CDATA[$config['questions']['template-engine']]]></code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['router']]]></code>
<code><![CDATA[$config['questions']['router']]]></code>
<code><![CDATA[$config['questions']['template-engine']]]></code>
</MixedArrayAccess>
<MoreSpecificReturnType>
<code><![CDATA[Generator<string, array{
* 0: OptionalPackages::INSTALL_*,
* 1: int,
* 2: int,
* 3: class-string<TemplateRendererInterface>,
* 4: string,
* 5: string
* }>]]></code>
<code><![CDATA[Generator<string, array{
* 0: OptionalPackages::INSTALL_*,
* 1: int,
* 2: string,
* 3: string,
* 4: int,
* 5: class-string<RouterInterface>,
* 6: string,
* 7: string
* }>]]></code>
</MoreSpecificReturnType>
<PossiblyInvalidArrayOffset>
<code>self::$expectedContainerAttributes[$containerClass]</code>
<code>self::$expectedContainerAttributes[$containerClass]</code>
<code>self::$expectedRouterAttributes[$routerClass]</code>
<code>self::$rendererConfigProviders[$rendererClass]</code>
<code>self::$routerConfigProviders[$routerClass]</code>
</PossiblyInvalidArrayOffset>
</file>
<file src="test/MezzioInstallerTest/OptionalPackagesTestCase.php">
<MixedArgument>
<code><![CDATA[$properties['devDependencies']]]></code>
<code><![CDATA[$r->getValue($installer)]]></code>
<code><![CDATA[$r->getValue($installer)]]></code>
<code>$whitelist</code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$composerJson['require']]]></code>
<code><![CDATA[$composerJson['require-dev']]]></code>
<code><![CDATA[$r->getValue($installer)['extra']]]></code>
</MixedArrayAccess>
<MixedAssignment>
<code>$composerJson</code>
<code>$whitelist</code>
</MixedAssignment>
<MixedInferredReturnType>
<code>array</code>
<code>array</code>
</MixedInferredReturnType>
<MixedReturnStatement>
<code><![CDATA[$this->getInstallerProperty($installer, 'composerDefinition')]]></code>
<code><![CDATA[$this->getInstallerProperty($installer, 'config')]]></code>
</MixedReturnStatement>
<PossiblyUnusedProperty>
<code>$composerData</code>
</PossiblyUnusedProperty>
</file>
<file src="test/MezzioInstallerTest/ProcessAnswersTest.php">
<MixedArgument>
<code>$question</code>
<code>$question</code>
<code>$question</code>
<code>$question</code>
<code>$question</code>
<code>$question</code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['template-engine']]]></code>
</MixedArrayAccess>
<MixedAssignment>
<code>$question</code>
<code>$question</code>
<code>$question</code>
<code>$question</code>
<code>$question</code>
<code>$question</code>
</MixedAssignment>
</file>
<file src="test/MezzioInstallerTest/ProjectSandboxTrait.php">
<MixedArgumentTypeCoercion>
<code><![CDATA[$this->autoloader]]></code>
</MixedArgumentTypeCoercion>
<MixedAssignment>
<code><![CDATA[$this->container]]></code>
</MixedAssignment>
<MixedInferredReturnType>
<code>ContainerInterface</code>
</MixedInferredReturnType>
<UnresolvableInclude>
<code>require $path</code>
</UnresolvableInclude>
</file>
<file src="test/MezzioInstallerTest/RemoveDevDependenciesTest.php">
<MixedAssignment>
<code><![CDATA[$this->devDependencies]]></code>
</MixedAssignment>
</file>
<file src="test/MezzioInstallerTest/RequestInstallTypeTest.php">
<MixedInferredReturnType>
<code>array</code>
</MixedInferredReturnType>
</file>
<file src="test/MezzioInstallerTest/RoutersTest.php">
<MixedArgument>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['router']]]></code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$config['dependencies']]]></code>
<code><![CDATA[$config['dependencies'][$dependencyKey]]]></code>
<code><![CDATA[$config['dependencies'][$dependencyKey][RouterInterface::class]]]></code>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['router']]]></code>
</MixedArrayAccess>
<MixedAssignment>
<code>$config</code>
</MixedAssignment>
<MixedInferredReturnType>
<code>array</code>
</MixedInferredReturnType>
</file>
<file src="test/MezzioInstallerTest/SetupDataAndCacheDirTest.php">
<UnusedProperty>
<code>$project</code>
</UnusedProperty>
</file>
<file src="test/MezzioInstallerTest/TemplateRenderersTest.php">
<MixedArgument>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['router']]]></code>
<code><![CDATA[$config['questions']['template-engine']]]></code>
</MixedArgument>
<MixedArrayAccess>
<code><![CDATA[$config['dependencies']]]></code>
<code><![CDATA[$config['dependencies']['factories']]]></code>
<code><![CDATA[$config['dependencies']['factories'][ErrorHandler::class]]]></code>
<code><![CDATA[$config['questions']['container']]]></code>
<code><![CDATA[$config['questions']['router']]]></code>
<code><![CDATA[$config['questions']['template-engine']]]></code>
</MixedArrayAccess>
<MixedAssignment>
<code>$config</code>
</MixedAssignment>
</file>
<file src="test/MezzioInstallerTest/UpdateRootPackageTest.php">
<MixedArgumentTypeCoercion>
<code>$property</code>
</MixedArgumentTypeCoercion>
</file>
</files>

23
app/psalm.xml.dist Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0"?>
<psalm
cacheDirectory="./.psalm-cache"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
errorLevel="1"
findUnusedPsalmSuppress="true"
findUnusedCode="true"
findUnusedBaselineEntry="true"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
<directory name="src"/>
<directory name="test"/>
<ignoreFiles>
<directory name="vendor"/>
</ignoreFiles>
</projectFiles>
<plugins>
<pluginClass class="Psalm\PhpUnitPlugin\Plugin"/>
</plugins>
</psalm>

19
app/public/.htaccess Normal file
View File

@ -0,0 +1,19 @@
RewriteEngine On
# The following rule allows authentication to work with fast-cgi
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [NC,L]
# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.
RewriteCond $0::%{REQUEST_URI} ^([^:]*+(?::[^:]*+)*?)::(/.+?)\1$
RewriteRule .+ - [E=BASE:%2]
RewriteRule .* %{ENV:BASE}index.php [NC,L]

30
app/public/index.php Normal file
View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
// Delegate static file requests back to the PHP built-in webserver
if (PHP_SAPI === 'cli-server' && $_SERVER['SCRIPT_FILENAME'] !== __FILE__) {
return false;
}
chdir(dirname(__DIR__));
require 'vendor/autoload.php';
/**
* Self-called anonymous function that creates its own scope and keeps the global namespace clean.
*/
(function () {
/** @var \Psr\Container\ContainerInterface $container */
$container = require 'config/container.php';
/** @var \Mezzio\Application $app */
$app = $container->get(\Mezzio\Application::class);
$factory = $container->get(\Mezzio\MiddlewareFactory::class);
// Execute programmatic/declarative middleware pipeline and routing
// configuration statements
(require 'config/pipeline.php')($app, $factory, $container);
(require 'config/routes.php')($app, $factory, $container);
$app->run();
})();

View File

@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace App;
/**
* The configuration provider for the App module
*
* @see https://docs.laminas.dev/laminas-component-installer/
*/
class ConfigProvider
{
/**
* Returns the configuration array
*
* To add a bit of a structure, each section is defined in a separate
* method which returns an array with its configuration.
*/
public function __invoke(): array
{
return [
'dependencies' => $this->getDependencies(),
'templates' => $this->getTemplates(),
];
}
/**
* Returns the container dependencies
*/
public function getDependencies(): array
{
return [
'invokables' => [
Handler\PingHandler::class => Handler\PingHandler::class,
],
'factories' => [
Handler\HomePageHandler::class => Handler\HomePageHandlerFactory::class,
],
];
}
/**
* Returns the templates configuration
*/
public function getTemplates(): array
{
return [
'paths' => [
'app' => [__DIR__ . '/../templates/app'],
'error' => [__DIR__ . '/../templates/error'],
'layout' => [__DIR__ . '/../templates/layout'],
],
];
}
}

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\Handler;
use Chubbyphp\Container\MinimalContainer;
use DI\Container as PHPDIContainer;
use Laminas\Diactoros\Response\HtmlResponse;
use Laminas\Diactoros\Response\JsonResponse;
use Laminas\ServiceManager\ServiceManager;
use Mezzio\LaminasView\LaminasViewRenderer;
use Mezzio\Plates\PlatesRenderer;
use Mezzio\Router\FastRouteRouter;
use Mezzio\Router\LaminasRouter;
use Mezzio\Router\RouterInterface;
use Mezzio\Template\TemplateRendererInterface;
use Mezzio\Twig\TwigRenderer;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class HomePageHandler implements RequestHandlerInterface
{
public function __construct(
private RouterInterface $router,
private ?TemplateRendererInterface $template = null
) {
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$data = [];
return new HtmlResponse($this->template->render('app::home-page', $data));
}
}

View File

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Handler;
use Mezzio\Router\RouterInterface;
use Mezzio\Template\TemplateRendererInterface;
use Psr\Container\ContainerInterface;
use Psr\Http\Server\RequestHandlerInterface;
use function assert;
class HomePageHandlerFactory
{
public function __invoke(ContainerInterface $container): RequestHandlerInterface
{
$router = $container->get(RouterInterface::class);
assert($router instanceof RouterInterface);
$template = $container->has(TemplateRendererInterface::class)
? $container->get(TemplateRendererInterface::class)
: null;
assert($template instanceof TemplateRendererInterface || null === $template);
return new HomePageHandler($router, $template);
}
}

View File

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Handler;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use function time;
class PingHandler implements RequestHandlerInterface
{
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new JsonResponse(['ack' => time()]);
}
}

View File

@ -0,0 +1,4 @@
<?php $this->layout('layout::default', ['title' => 'Home']) ?>
<div>
<h1>Slovocast</h1>
</div>

View File

@ -0,0 +1,9 @@
<?php $this->layout('layout::default', ['title' => '404 Not Found']) ?>
<h1>Oops!</h1>
<h2>This is awkward.</h2>
<p>We encountered a 404 Not Found error.</p>
<p>
You are looking for something that doesn't exist or may have moved. Check out one of the links on this page
or head back to <a href="/">Home</a>.
</p>

View File

@ -0,0 +1,11 @@
<?php $this->layout('layout::default', ['title' => sprintf('%d %s', $status, $reason)]) ?>
<h1>Oops!</h1>
<h2>This is awkward.</h2>
<p>We encountered a <?=$this->e($status)?> <?=$this->e($reason)?> error.</p>
<?php if ($status == 404) : ?>
<p>
You are looking for something that doesn't exist or may have moved. Check out one of the links on this page
or head back to <a href="/">Home</a>.
</p>
<?php endif; ?>

View File

@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<title><?=$this->e($title)?> - Slovocast</title>
<?=$this->section('stylesheets')?>
</head>
<body>
<header>
<div>Slovocast</div>
</header>
<main>
<?= $this->section('content'); ?>
</main>
<?=$this->section('javascript')?>
</body>
</html>

View File

@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace AppTest\Handler;
use App\Handler\HomePageHandler;
use App\Handler\HomePageHandlerFactory;
use AppTest\InMemoryContainer;
use Mezzio\Router\RouterInterface;
use Mezzio\Template\TemplateRendererInterface;
use PHPUnit\Framework\TestCase;
class HomePageHandlerFactoryTest extends TestCase
{
public function testFactoryWithoutTemplate(): void
{
$container = new InMemoryContainer();
$container->setService(RouterInterface::class, $this->createMock(RouterInterface::class));
$factory = new HomePageHandlerFactory();
$homePage = $factory($container);
self::assertInstanceOf(HomePageHandler::class, $homePage);
}
public function testFactoryWithTemplate(): void
{
$container = new InMemoryContainer();
$container->setService(RouterInterface::class, $this->createMock(RouterInterface::class));
$container->setService(TemplateRendererInterface::class, $this->createMock(TemplateRendererInterface::class));
$factory = new HomePageHandlerFactory();
$homePage = $factory($container);
self::assertInstanceOf(HomePageHandler::class, $homePage);
}
}

View File

@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace AppTest\Handler;
use App\Handler\HomePageHandler;
use Laminas\Diactoros\Response\HtmlResponse;
use Laminas\Diactoros\Response\JsonResponse;
use Mezzio\Router\RouterInterface;
use Mezzio\Template\TemplateRendererInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;
class HomePageHandlerTest extends TestCase
{
/** @var ContainerInterface&MockObject */
protected $container;
/** @var RouterInterface&MockObject */
protected $router;
protected function setUp(): void
{
$this->container = $this->createMock(ContainerInterface::class);
$this->router = $this->createMock(RouterInterface::class);
}
public function testReturnsJsonResponseWhenNoTemplateRendererProvided(): void
{
$homePage = new HomePageHandler(
$this->container::class,
$this->router,
null
);
$response = $homePage->handle(
$this->createMock(ServerRequestInterface::class)
);
self::assertInstanceOf(JsonResponse::class, $response);
}
public function testReturnsHtmlResponseWhenTemplateRendererProvided(): void
{
$renderer = $this->createMock(TemplateRendererInterface::class);
$renderer
->expects($this->once())
->method('render')
->with('app::home-page', $this->isType('array'))
->willReturn('');
$homePage = new HomePageHandler(
$this->container::class,
$this->router,
$renderer
);
$response = $homePage->handle(
$this->createMock(ServerRequestInterface::class)
);
self::assertInstanceOf(HtmlResponse::class, $response);
}
}

View File

@ -0,0 +1,31 @@
<?php
declare(strict_types=1);
namespace AppTest\Handler;
use App\Handler\PingHandler;
use Laminas\Diactoros\Response\JsonResponse;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use function json_decode;
use function property_exists;
use const JSON_THROW_ON_ERROR;
class PingHandlerTest extends TestCase
{
public function testResponse(): void
{
$pingHandler = new PingHandler();
$response = $pingHandler->handle(
$this->createMock(ServerRequestInterface::class)
);
$json = json_decode((string) $response->getBody(), null, 512, JSON_THROW_ON_ERROR);
self::assertInstanceOf(JsonResponse::class, $response);
self::assertTrue(property_exists($json, 'ack') && $json->ack !== null);
}
}

View File

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace AppTest;
use Psr\Container\ContainerInterface;
use RuntimeException;
use function array_key_exists;
use function sprintf;
/**
* A PSR Container stub. Useful for testing factories without excessive mocking
*/
class InMemoryContainer implements ContainerInterface
{
/** @var array<string, mixed> */
public array $services = [];
public function setService(string $name, mixed $service): void
{
$this->services[$name] = $service;
}
/**
* @param string $id
* @return mixed
*/
public function get($id)
{
if (! $this->has($id)) {
throw new RuntimeException(sprintf('Service not found "%s"', $id));
}
return $this->services[$id];
}
/**
* @param string $id
* @return bool
*/
public function has($id)
{
return array_key_exists($id, $this->services);
}
}

View File

@ -1,4 +0,0 @@
DATABASE_HOST=localhost
DATABASE_USER=user
DATABASE_PASSWORD=password
DATABASE_SCHEMA=slovocast

1
server/.gitignore vendored
View File

@ -1 +0,0 @@
node_modules

View File

@ -1,11 +0,0 @@
To install dependencies:
```sh
bun install
```
To run:
```sh
bun run dev
```
open http://localhost:3000

Binary file not shown.

View File

@ -1,15 +0,0 @@
{
"scripts": {
"dev": "bun run --hot src/index.ts"
},
"dependencies": {
"@hono/zod-validator": "^0.1.11",
"hono": "^4.0.3",
"mariadb": "^3.2.3",
"podcast": "^2.0.1",
"zod": "^3.22.4"
},
"devDependencies": {
"@types/bun": "latest"
}
}

View File

@ -1,60 +0,0 @@
import { Context, Hono } from 'hono';
import { Connection, createPool } from 'mariadb';
import User from './models/User';
const app = new Hono();
//
// bootstrap resources
//
// mariadb
const databasePool = createPool({
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_SCHEMA
});
app.onError((err: Error, c: Context) => {
console.log(err.message);
c.status(500);
if (process.env.MODE == 'development') {
return c.json({ err });
}
return c.json({ message: "An error occurred." });
});
//
// set routes
//
// Welcome message
app.get('/', async (c: Context) => {
return c.json({
message: "Hello from Slovocast"
});
});
// User actions
import users from './routes/user';
app.route("/users", users);
// Channel Actions
// add episode
// edit episode
// regenerate feed
app.get('/:channel', (c: Context) => {
return c.json({
channel: {
name: "Name",
}
});
});
// feed actions
export default app;

View File

@ -1,36 +0,0 @@
import { PoolConnection, Pool, createPool } from 'mariadb';
type ConnectionConfig = {
host: string,
user: string,
password: string,
database: string,
port: number,
};
export default class Database {
private static instance: Database;
private pool: Pool;
private constructor(config: ConnectionConfig) {
this.pool = createPool(config);
}
public static getInstance(): Database {
if (!Database.instance) {
Database.instance = new Database({
host: process.env.DATABASE_HOST as string,
user: process.env.DATABASE_USER as string,
password: process.env.DATABASE_PASSWORD as string,
database: process.env.DATABASE_SCHEMA as string,
port: 3306
});
}
return Database.instance;
}
public async getConnection(): Promise<PoolConnection> {
return this.pool.getConnection();
}
}

View File

@ -1,39 +0,0 @@
import { createConnection } from "mariadb";
// @ts-ignore
const SQL_ROOT_DIR: string = import.meta.dir + '/../../../sql';
async function run() {
const connection = await createConnection({
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_SCHEMA
});
const createUserQuery: string = await Bun.file(SQL_ROOT_DIR + '/01-users.sql').text();
const createImagesQuery: string = await Bun.file(SQL_ROOT_DIR + '/02-images.sql').text();
const createChannelsQuery: string = await Bun.file(SQL_ROOT_DIR + '/03-channels.sql').text();
const createEpisodesQuery: string = await Bun.file(SQL_ROOT_DIR + '/04-episodes.sql').text();
const results = Promise.all([
connection.query(createUserQuery),
connection.query(createImagesQuery),
connection.query(createChannelsQuery),
connection.query(createEpisodesQuery)
]);
return results;
}
run()
.then(r => {
console.log(r);
process.exit(0);
})
.catch(e => {
console.error(e);
process.exit(1);
})

View File

@ -1,11 +0,0 @@
type Channel = {
name: string,
description: string,
link: string,
lanuage: string,
copyright: string|undefined,
explicit: boolean,
category: string,
};
export default Channel;

View File

@ -1,10 +0,0 @@
type Episode = {
title: string,
link: string,
description: string,
duration: string,
length: number,
explicit: boolean,
};
export default Episode;

View File

@ -1,8 +0,0 @@
type Image = {
url: string,
title: string|undefined,
width: number|undefined,
height: number|undefined,
};
export default Image;

View File

@ -1,7 +0,0 @@
type User = {
email: string,
password: string,
name: string,
};
export default User;

View File

@ -1,16 +0,0 @@
import { Hono, Context } from 'hono';
const channelRoutes = new Hono();
channelRoutes.get('/:channelSlug', async (c: Context) => {
return c.json([
{
channel: {
name: 'Something'
}
}
]);
});
export default channelRoutes;

View File

@ -1,38 +0,0 @@
import { Hono, Context } from 'hono';
import { PoolConnection } from 'mariadb';
import User from 'slovo/models/User';
import Database from 'slovo/infrastructure/database';
const app = new Hono();
// add routes and methods here
app.post('/register', async (c: Context) => {
const user: User = await c.req.json<User>();
const conn: PoolConnection = await Database.getInstance().getConnection();
const query = `INSERT INTO users (username, name, password) VALUES (?, ?)`;
try {
const results = await conn.query(query, [
user.email,
user.name,
user.password
]);
console.log(results);
await conn.release();
return c.json({
message: "Registration successful.",
});
} catch (e) {
console.error(e);
c.status(500);
return c.json({
message: "Error registering user."
});
}
});
export default app;

View File

@ -1,11 +0,0 @@
{
"compilerOptions": {
"strict": true,
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx",
"baseUrl": "./src",
"paths": {
"slovo/*": [ "*" ]
}
}
}