initial commit

This commit is contained in:
Pavel 2024-09-13 11:43:03 +03:00
commit 281bcabd74
26 changed files with 3617 additions and 0 deletions

24
.docker/app/Dockerfile Normal file
View File

@ -0,0 +1,24 @@
FROM php:8.3-fpm-alpine
RUN apk --update add \
linux-headers \
autoconf \
shadow \
gcc \
libc-dev \
make \
postgresql-dev \
git \
libzip-dev \
jq \
&& docker-php-ext-configure pgsql -with-pgsql=/usr/include/postgresql/ \
&& docker-php-ext-configure intl \
&& docker-php-ext-configure pcntl --enable-pcntl \
&& docker-php-ext-install pgsql pdo_pgsql bcmath intl sockets zip pcntl posix \
&& rm -rf /var/cache/apk/* && rm -rf /etc/apk/cache
RUN pecl install xdebug && docker-php-ext-enable xdebug
RUN curl -L https://getcomposer.org/download/2.7.2/composer.phar -o /usr/bin/composer && chmod a+x /usr/bin/composer
COPY .docker/app/php.ini /usr/local/etc/php/conf.d/dev.ini

12
.docker/app/php.ini Normal file
View File

@ -0,0 +1,12 @@
date.timezone=Europe/Moscow
default_charset=UTF-8
display_errors=Off
log_errors=On
max_execution_time=90
max_file_uploads=10
max_input_time=120
max_input_vars=5000
memory_limit=1G
realpath_cache_size=16M
upload_max_filesize=25M
realpath_cache_ttl=600

6
.docker/app/xdebug.ini Normal file
View File

@ -0,0 +1,6 @@
xdebug.mode=debug,coverage
xdebug.start_with_request=yes
xdebug.client_host=172.17.0.1
xdebug.client_port=9001
xdebug.discover_client_host=false
zend_extension=xdebug

22
.docker/web/nginx.conf Normal file
View File

@ -0,0 +1,22 @@
server {
root /var/www/html;
location / {
try_files $uri @backend;
}
location ~* /\.(ht|svn|git) {
deny all;
}
location @backend {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_param SCRIPT_FILENAME /app/public/index.php;
fastcgi_pass app:9000;
fastcgi_connect_timeout 5s;
fastcgi_read_timeout 60s;
}
}

20
.env Normal file
View File

@ -0,0 +1,20 @@
# In all environments, the following files are loaded if they exist,
# the latter taking precedence over the former:
#
# * .env contains default values for the environment variables needed by the app
# * .env.local uncommitted file with local overrides
# * .env.$APP_ENV committed environment-specific defaults
# * .env.$APP_ENV.local uncommitted environment-specific overrides
#
# Real environment variables win over .env files.
#
# DO NOT DEFINE PRODUCTION SECRETS IN THIS FILE NOR IN ANY OTHER COMMITTED FILES.
# https://symfony.com/doc/current/configuration/secrets.html
#
# Run "composer dump-env prod" to compile .env files for production use (requires symfony/flex >=1.2).
# https://symfony.com/doc/current/best_practices.html#use-environment-variables-for-infrastructure-configuration
###> symfony/framework-bundle ###
APP_ENV=dev
APP_SECRET=d5142ed3649b2f799910d9caa0112204
###< symfony/framework-bundle ###

11
.gitignore vendored Normal file
View File

@ -0,0 +1,11 @@
###> symfony/framework-bundle ###
/.env.local
/.env.local.php
/.env.*.local
/config/secrets/prod/prod.decrypt.private.php
/public/bundles/
/var/
/vendor/
###< symfony/framework-bundle ###
.idea

21
bin/console Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/env php
<?php
use App\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
if (!is_dir(dirname(__DIR__).'/vendor')) {
throw new LogicException('Dependencies are missing. Try running "composer install".');
}
if (!is_file(dirname(__DIR__).'/vendor/autoload_runtime.php')) {
throw new LogicException('Symfony Runtime is missing. Try running "composer require symfony/runtime".');
}
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
return function (array $context) {
$kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
return new Application($kernel);
};

41
compose.yml Normal file
View File

@ -0,0 +1,41 @@
services:
web:
image: nginx:stable-alpine
volumes:
- .docker/web/nginx.conf:/etc/nginx/conf.d/default.conf
depends_on:
- app
- db
networks:
- app
labels:
traefik.enable: "true"
traefik.http.routers.sflogs.entrypoints: web
traefik.http.routers.sflogs.rule: "Host(`sflogs.test`)"
traefik.http.services.sflogs.loadbalancer.server.port: 80
app:
build:
context: .
dockerfile: .docker/app/Dockerfile
user: ${UID:-1000}:${GID:-1000}
volumes:
- .:/app
working_dir: /app
depends_on:
- db
environment:
- XDEBUG_MODE=debug,coverage
- COMPOSER_HOME=/tmp
networks:
- app
db:
image: postgres:16
environment:
POSTGRES_DB: db
POSTGRES_USER: db
POSTGRES_PASSWORD: db
networks:
- app
networks:
app:

69
composer.json Normal file
View File

@ -0,0 +1,69 @@
{
"type": "project",
"license": "proprietary",
"minimum-stability": "stable",
"prefer-stable": true,
"require": {
"php": ">=8.1",
"ext-ctype": "*",
"ext-iconv": "*",
"symfony/console": "6.4.*",
"symfony/dotenv": "6.4.*",
"symfony/flex": "^2",
"symfony/framework-bundle": "6.4.*",
"symfony/monolog-bundle": "^3.10",
"symfony/runtime": "6.4.*",
"symfony/yaml": "6.4.*"
},
"require-dev": {
"symfony/maker-bundle": "^1.58"
},
"config": {
"allow-plugins": {
"php-http/discovery": true,
"symfony/flex": true,
"symfony/runtime": true
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php72": "*",
"symfony/polyfill-php73": "*",
"symfony/polyfill-php74": "*",
"symfony/polyfill-php80": "*",
"symfony/polyfill-php81": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "6.4.*"
}
}
}

2993
composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

7
config/bundles.php Normal file
View File

@ -0,0 +1,7 @@
<?php
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
];

View File

@ -0,0 +1,19 @@
framework:
cache:
# Unique name of your app: used to compute stable namespaces for cache keys.
#prefix_seed: your_vendor_name/app_name
# The "app" cache stores to the filesystem by default.
# The data in this cache should persist between deploys.
# Other options include:
# Redis
#app: cache.adapter.redis
#default_redis_provider: redis://localhost
# APCu (not recommended with heavy random-write workloads as memory fragmentation can cause perf issues)
#app: cache.adapter.apcu
# Namespaced pools use the above "app" backend by default
#pools:
#my.dedicated.cache: null

View File

@ -0,0 +1,25 @@
# see https://symfony.com/doc/current/reference/configuration/framework.html
framework:
secret: '%env(APP_SECRET)%'
#csrf_protection: true
annotations: false
http_method_override: false
handle_all_throwables: true
# Enables session support. Note that the session will ONLY be started if you read or write from it.
# Remove or comment this section to explicitly disable session support.
session:
handler_id: null
cookie_secure: auto
cookie_samesite: lax
#esi: true
#fragments: true
php_errors:
log: true
when@test:
framework:
test: true
session:
storage_factory_id: session.storage.factory.mock_file

View File

@ -0,0 +1,67 @@
monolog:
channels:
- deprecation # Deprecations are logged in the dedicated "deprecation" channel when it exists
when@dev:
monolog:
handlers:
main:
type: stream
path: "php://stdout"
level: debug
formatter: 'monolog.formatter.jsonwithfields'
file:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
channels: ["!event"]
# uncomment to get logging in your browser
# you may have to allow bigger header sizes in your Web server configuration
#firephp:
# type: firephp
# level: info
#chromephp:
# type: chromephp
# level: info
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine", "!console"]
when@test:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
excluded_http_codes: [404, 405]
channels: ["!event"]
nested:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
when@prod:
monolog:
handlers:
main:
type: fingers_crossed
action_level: error
handler: nested
excluded_http_codes: [404, 405]
buffer_size: 50 # How many messages should be saved? Prevent memory leaks
nested:
type: stream
path: php://stderr
level: debug
formatter: monolog.formatter.jsonwithfields
console:
type: console
process_psr_3_messages: false
channels: ["!event", "!doctrine"]
deprecation:
type: stream
channels: [deprecation]
path: php://stderr
formatter: monolog.formatter.jsonwithfields

View File

@ -0,0 +1,12 @@
framework:
router:
utf8: true
# Configure how to generate URLs in non-HTTP contexts, such as CLI commands.
# See https://symfony.com/doc/current/routing.html#generating-urls-in-commands
#default_uri: http://localhost
when@prod:
framework:
router:
strict_requirements: null

5
config/preload.php Normal file
View File

@ -0,0 +1,5 @@
<?php
if (file_exists(dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php')) {
require dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php';
}

5
config/routes.yaml Normal file
View File

@ -0,0 +1,5 @@
controllers:
resource:
path: ../src/Controller/
namespace: App\Controller
type: attribute

View File

@ -0,0 +1,4 @@
when@dev:
_errors:
resource: '@FrameworkBundle/Resources/config/routing/errors.xml'
prefix: /_error

27
config/services.yaml Normal file
View File

@ -0,0 +1,27 @@
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
# Put parameters here that don't need to change on each machine where the app is deployed
# https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration
parameters:
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
# makes classes in src/ available to be used as services
# this creates a service per class whose id is the fully-qualified class name
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
monolog.formatter.jsonwithfields:
class: 'App\Monolog\JsonWithFieldsFormatter'
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones

9
public/index.php Normal file
View File

@ -0,0 +1,9 @@
<?php
use App\Kernel;
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
return function (array $context) {
return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
};

0
src/Controller/.gitignore vendored Normal file
View File

View File

@ -0,0 +1,47 @@
<?php
namespace App\Controller;
use App\Monolog\ContextedLogger;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
class BlursedController extends AbstractController
{
private LoggerInterface $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = new ContextedLogger($logger, [
'handler' => fn(): string => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[4]['function'],
'connection' => 'http://app.retailcrm.test',
'account' => '@account',
]);
}
#[Route('/blursed', name: 'app_blursed')]
public function index(): JsonResponse
{
for ($i = 0; $i < 50; $i++) {
$this->logger->info($i . ": Sample message output\nNew line!", [
'string' => 'here',
'int' => $i,
'float' => 2.3 + $i,
'array' => [1, 2 ,3],
'dictionary' => [
'alpha' => '1',
'beta' => '2',
'gamma' => '3'
],
'class' => new \stdClass(),
]);
}
return $this->json([
'message' => 'Welcome to your new controller!',
'path' => 'src/Controller/BlursedController.php',
]);
}
}

11
src/Kernel.php Normal file
View File

@ -0,0 +1,11 @@
<?php
namespace App;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
class Kernel extends BaseKernel
{
use MicroKernelTrait;
}

View File

@ -0,0 +1,33 @@
<?php
namespace App\Monolog;
use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;
class ContextedLogger extends AbstractLogger
{
public function __construct(
protected LoggerInterface $logger,
protected array $context = [],
) {
}
public function log($level, $message, array $context = []): void
{
$this->logger->log($level, $message, [...$this->prepareContext(), ...$context]);
}
protected function prepareContext(): array
{
$context = $this->context;
foreach ($context as $key => $value) {
if (is_callable($value)) {
$context[$key] = $value();
}
}
return $context;
}
}

View File

@ -0,0 +1,48 @@
<?php
namespace App\Monolog;
use Monolog\Formatter\JsonFormatter;
use Monolog\LogRecord;
class JsonWithFieldsFormatter extends JsonFormatter
{
protected const EXTRACTABLE_FIELDS = [
'connection',
'account',
'handler',
];
public function format(LogRecord $record): string
{
$normalized = $this->normalizeRecord($record);
if (isset($normalized['context']) && $normalized['context'] === []) {
if ($this->ignoreEmptyContextAndExtra) {
unset($normalized['context']);
} else {
$normalized['context'] = new \stdClass;
}
}
if (isset($normalized['extra']) && $normalized['extra'] === []) {
if ($this->ignoreEmptyContextAndExtra) {
unset($normalized['extra']);
} else {
$normalized['extra'] = new \stdClass;
}
}
if (!empty($normalized['context'])) {
foreach (static::EXTRACTABLE_FIELDS as $field) {
if (!array_key_exists($field, $normalized['context'])) {
continue;
}
$normalized[$field] = $normalized['context'][$field];
unset($normalized['context'][$field]);
}
}
return $this->toJson($normalized, true) . ($this->appendNewline ? "\n" : '');
}
}

79
symfony.lock Normal file
View File

@ -0,0 +1,79 @@
{
"symfony/console": {
"version": "6.4",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "main",
"version": "5.3",
"ref": "1781ff40d8a17d87cf53f8d4cf0c8346ed2bb461"
},
"files": [
"bin/console"
]
},
"symfony/flex": {
"version": "2.4",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "main",
"version": "1.0",
"ref": "146251ae39e06a95be0fe3d13c807bcf3938b172"
},
"files": [
".env"
]
},
"symfony/framework-bundle": {
"version": "6.4",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "main",
"version": "6.4",
"ref": "a91c965766ad3ff2ae15981801643330eb42b6a5"
},
"files": [
"config/packages/cache.yaml",
"config/packages/framework.yaml",
"config/preload.php",
"config/routes/framework.yaml",
"config/services.yaml",
"public/index.php",
"src/Controller/.gitignore",
"src/Kernel.php"
]
},
"symfony/maker-bundle": {
"version": "1.58",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "main",
"version": "1.0",
"ref": "fadbfe33303a76e25cb63401050439aa9b1a9c7f"
}
},
"symfony/monolog-bundle": {
"version": "3.10",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "main",
"version": "3.7",
"ref": "aff23899c4440dd995907613c1dd709b6f59503f"
},
"files": [
"config/packages/monolog.yaml"
]
},
"symfony/routing": {
"version": "6.4",
"recipe": {
"repo": "github.com/symfony/recipes",
"branch": "main",
"version": "6.2",
"ref": "e0a11b4ccb8c9e70b574ff5ad3dfdcd41dec5aa6"
},
"files": [
"config/packages/routing.yaml",
"config/routes.yaml"
]
}
}