Skip to content

Commit

Permalink
Reduce to minimum
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed Dec 3, 2023
1 parent 04408af commit af617eb
Show file tree
Hide file tree
Showing 50 changed files with 323 additions and 1,824 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ This is a skeleton to quickly set up a new [Slim 4](https://www.slimframework.co

## Requirements

* PHP 8.1+
* MySQL 5.7+ or MariaDB
* PHP 8.2

## Installation

Expand Down
29 changes: 0 additions & 29 deletions bin/console

This file was deleted.

12 changes: 3 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,26 @@
"slim-framework",
"skeleton",
"slim",
"slim4",
"apache"
"slim4"
],
"require": {
"php": "^8.1",
"ext-json": "*",
"ext-pdo": "*",
"cakephp/database": "^5",
"cakephp/validation": "^5",
"fig/http-message-util": "^1.1",
"monolog/monolog": "^3",
"nyholm/psr7": "^1.8.1",
"nyholm/psr7-server": "^1.1",
"php-di/php-di": "^7",
"psr/clock": "^1.0",
"selective/basepath": "^2",
"slim/slim": "^4",
"symfony/clock": "^7"
"slim/slim": "^4"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3",
"phpstan/phpstan": "1.*",
"phpunit/phpunit": "^10",
"selective/test-traits": "^4",
"squizlabs/php_codesniffer": "^3",
"symfony/console": "^7"
"squizlabs/php_codesniffer": "^3"
},
"autoload": {
"psr-4": {
Expand Down
38 changes: 0 additions & 38 deletions config/container.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
<?php

use App\Handler\DefaultErrorHandler;
use Cake\Database\Connection;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Clock\ClockInterface;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ServerRequestFactoryInterface;
Expand All @@ -19,9 +17,6 @@
use Slim\Factory\AppFactory;
use Slim\Interfaces\RouteParserInterface;
use Slim\Middleware\ErrorMiddleware;
use Symfony\Component\Clock\NativeClock;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputOption;

return [
// Application settings
Expand Down Expand Up @@ -69,21 +64,6 @@
return new BasePathMiddleware($container->get(App::class));
},

// Database connection
Connection::class => function (ContainerInterface $container) {
return new Connection($container->get('settings')['db']);
},

PDO::class => function (ContainerInterface $container) {
$driver = $container->get(Connection::class)->getDriver();

$class = new ReflectionClass($driver);
$method = $class->getMethod('getPdo');
$method->setAccessible(true);

return $method->invoke($driver);
},

LoggerInterface::class => function (ContainerInterface $container) {
$settings = $container->get('settings')['logger'];

Expand Down Expand Up @@ -116,22 +96,4 @@

return $errorMiddleware;
},

Application::class => function (ContainerInterface $container) {
$application = new Application();

$application->getDefinition()->addOption(
new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', 'dev')
);

foreach ($container->get('settings')['commands'] as $class) {
$application->add($container->get($class));
}

return $application;
},

ClockInterface::class => function () {
return new NativeClock();
},
];
29 changes: 2 additions & 27 deletions config/defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,46 +27,21 @@
// Log file location
'path' => __DIR__ . '/../logs',
// Default log level
'level' => \Monolog\Level::Info,
'level' => \Psr\Log\LogLevel::DEBUG,
];

// Database settings
$settings['db'] = [
'driver' => \Cake\Database\Driver\Mysql::class,
'host' => 'localhost',
'encoding' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
// Enable identifier quoting
'quoteIdentifiers' => true,
// Set to null to use MySQL servers timezone
'timezone' => null,
// Disable meta data cache
'cacheMetadata' => false,
// Disable query logging
'log' => false,
// Turn off persistent connections
'persistent' => false,
// PDO options
'flags' => [
// Turn off persistent connections
'options' => [
PDO::ATTR_PERSISTENT => false,
// Enable exceptions
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
// Emulate prepared statements
PDO::ATTR_EMULATE_PREPARES => true,
// Set default fetch mode to array
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
// Convert numeric values to strings when fetching.
// Since PHP 8.1 integers and floats in result sets will be returned using native PHP types.
// This option restores the previous behavior.
PDO::ATTR_STRINGIFY_FETCHES => true,
],
];

// Console commands
$settings['commands'] = [
\App\Console\ExampleCommand::class,
\App\Console\SetupCommand::class,
];

return $settings;
1 change: 0 additions & 1 deletion config/local.dev.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
ini_set('display_startup_errors', '1');

$settings['error']['display_error_details'] = true;
$settings['logger']['level'] = \Monolog\Level::Debug;

// Database
$settings['db']['database'] = 'slim_skeleton_dev';
Expand Down
14 changes: 0 additions & 14 deletions config/local.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,5 @@
// Database
$settings['db']['database'] = 'slim_skeleton_test';

// Mocked Logger settings
$settings['logger'] = [
'test' => new \Monolog\Handler\TestHandler(),
];

// API credentials for phpunit
$settings['api_auth'] = [
// Allow http for testing
'secure' => false,
'users' => [
'api-user' => 'secret',
],
];

return $settings;
};
2 changes: 0 additions & 2 deletions config/middleware.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
<?php

use App\Middleware\ValidationMiddleware;
use Selective\BasePath\BasePathMiddleware;
use Slim\App;
use Slim\Middleware\ErrorMiddleware;

return function (App $app) {
$app->addBodyParsingMiddleware();
$app->add(ValidationMiddleware::class);
$app->addRoutingMiddleware();
$app->add(BasePathMiddleware::class);
$app->add(ErrorMiddleware::class);
Expand Down
13 changes: 0 additions & 13 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,7 @@
// Define app routes

use Slim\App;
use Slim\Routing\RouteCollectorProxy;

return function (App $app) {
$app->get('/', \App\Action\Home\HomeAction::class)->setName('home');

// API
$app->group(
'/api',
function (RouteCollectorProxy $app) {
$app->get('/customers', \App\Action\Customer\CustomerFinderAction::class);
$app->post('/customers', \App\Action\Customer\CustomerCreatorAction::class);
$app->get('/customers/{customer_id}', \App\Action\Customer\CustomerReaderAction::class);
$app->put('/customers/{customer_id}', \App\Action\Customer\CustomerUpdaterAction::class);
$app->delete('/customers/{customer_id}', \App\Action\Customer\CustomerDeleterAction::class);
}
);
};
82 changes: 59 additions & 23 deletions docs/console.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,14 @@ parent: Advanced

# Console

## Composer scripts
## Installation

To list all composer scripts, run:
You'll need to install the Symfony Console component to add command-line capabilities to your project.

```
composer list
```

## Console Commands
Use Composer to do this:

The default console executable is: `bin/console.php`

The default console command class directory is: `src/Console`

To start the console and list all available commands, run:

``` bash
php bin/console
```
composer require symfony/console
```

## Creating a console command
Expand Down Expand Up @@ -59,20 +49,58 @@ final class ExampleCommand extends Command
}
```

To register a new command you have to open `config/defaults.php`
and add a new array entry to `$settings['commands']`.
## Register the Console Application

To integrate the Console application with your application,
you'll need to register it. Create a file, e.g., `bin/console.php`, and add the following code

```php
<?php

use App\Console\ExampleCommand;
use DI\ContainerBuilder;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;

require_once __DIR__ . '/../vendor/autoload.php';

$env = (new ArgvInput())->getParameterOption(['--env', '-e'], 'dev');

if ($env) {
$_ENV['APP_ENV'] = $env;
}

/** @var ContainerInterface $container */
$container = (new ContainerBuilder())
->addDefinitions(__DIR__ . '/../config/container.php')
->build();

try {
/** @var Application $application */
$application = $container->get(Application::class);

// Register your console commands here
$application->add($container->get(ExampleCommand::class));

exit($application->run());
} catch (Throwable $exception) {
echo $exception->getMessage();
exit(1);
}

```

Set permissions:

```php
$settings['commands'] = [
// ...
\App\Console\ExampleCommand::class,
];
chmod +x bin/console.php
```

To start to example command, run:

``` bash
php bin/console example
php bin/console.php example
```

The output:
Expand All @@ -81,7 +109,15 @@ The output:
Hello, World!
```

Read more:
## Console Commands

To list all available commands, run:

``` bash
php bin/console.php
```

## Read more

* [Symfony Console Commands](https://symfony.com/doc/current/console.html)
* [Console](https://ko-fi.com/s/5f182b4b22) (Slim 4 - eBook Vol. 1)
Loading

0 comments on commit af617eb

Please sign in to comment.