Skip to content

Commit e491ff7

Browse files
committed
First commit
0 parents  commit e491ff7

34 files changed

+1861
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
/vendor/
3+
composer.lock
4+
.phpunit.result.cache

CustomRequestResponserBundle.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Prokl\CustomRequestResponserBundle;
4+
5+
use Prokl\CustomRequestResponserBundle\DependencyInjection\CustomRequestResponserExtension;
6+
use Symfony\Component\HttpKernel\Bundle\Bundle;
7+
8+
/**
9+
* Class CustomRequestResponserBundle
10+
* @package Prokl\CustomRequestResponserBundle
11+
*
12+
* @since 04.12.2020
13+
*/
14+
class CustomRequestResponserBundle extends Bundle
15+
{
16+
/**
17+
* @return CustomRequestResponserExtension
18+
*/
19+
public function getContainerExtension(): CustomRequestResponserExtension
20+
{
21+
return new CustomRequestResponserExtension;
22+
}
23+
}

DependencyInjection/Configuration.php

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Prokl\CustomRequestResponserBundle\DependencyInjection;
4+
5+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
6+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7+
use Symfony\Component\Config\Definition\ConfigurationInterface;
8+
9+
/**
10+
* Class Configuration
11+
* @package Local\Bundles\CustomArgumentResolverBundle\DependencyInjection
12+
*
13+
* @since 04.12.2020
14+
*/
15+
class Configuration implements ConfigurationInterface
16+
{
17+
/**
18+
* @return TreeBuilder
19+
*/
20+
public function getConfigTreeBuilder()
21+
{
22+
$treeBuilder = new TreeBuilder('custom_request_responser');
23+
$rootNode = $treeBuilder->getRootNode();
24+
25+
\assert($rootNode instanceof ArrayNodeDefinition);
26+
27+
$rootNode
28+
->children()
29+
->arrayNode('defaults')
30+
->useAttributeAsKey('name')
31+
->prototype('boolean')->end()
32+
->defaultValue([
33+
'enabled' => false,
34+
])
35+
->end()
36+
->end()
37+
38+
->children()
39+
->arrayNode('middlewares_disabled')
40+
->useAttributeAsKey('name')
41+
->prototype('boolean')->defaultValue(true)->end()
42+
->end()
43+
->end()
44+
45+
->children()
46+
->arrayNode('bitrix_middlewares_disabled')
47+
->useAttributeAsKey('name')
48+
->prototype('boolean')->defaultValue(true)->end()
49+
->end()
50+
->end()
51+
52+
->children()
53+
->arrayNode('headers')
54+
->arrayPrototype()
55+
->beforeNormalization()
56+
->ifTrue(static function ($v): bool {
57+
return is_string($v) && strpos($v, ':') !== false;
58+
})
59+
->then(static function (string $v): array {
60+
[$name, $value] = explode(':', $v, 2);
61+
return ['name' => trim($name), 'value' => trim($value)];
62+
})
63+
->end()
64+
->beforeNormalization()
65+
->ifTrue(static function ($v): bool {
66+
return is_array($v)
67+
&& count($v) === 1
68+
&& is_string(key($v))
69+
&& is_string(reset($v));
70+
})
71+
->then(static function (array $v): array {
72+
return ['name' => key($v), 'value' => reset($v)];
73+
})
74+
->end()
75+
->normalizeKeys(false)
76+
->children()
77+
->scalarNode('name')->end()
78+
->scalarNode('value')->end()
79+
->scalarNode('condition')
80+
->defaultNull()
81+
->end()
82+
->end()
83+
->end()
84+
->end()
85+
->end()
86+
;
87+
88+
return $treeBuilder;
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Prokl\CustomRequestResponserBundle\DependencyInjection;
6+
7+
use Exception;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
use Symfony\Component\Config\FileLocator;
10+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
11+
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
12+
13+
/**
14+
* Class CustomRequestResponserExtension
15+
* @package Prokl\CustomRequestResponserBundle\DependencyInjection
16+
*
17+
* @since 04.12.2020
18+
*/
19+
class CustomRequestResponserExtension extends Extension
20+
{
21+
private const DIR_CONFIG = '/../Resources/config';
22+
23+
/**
24+
* @inheritDoc
25+
*
26+
* @return string
27+
*/
28+
public function getAlias()
29+
{
30+
return 'custom_request_responser';
31+
}
32+
33+
/**
34+
* @inheritDoc
35+
*/
36+
public function load(array $configs, ContainerBuilder $container) : void
37+
{
38+
39+
$configuration = new Configuration();
40+
$config = $this->processConfiguration($configuration, $configs);
41+
42+
if (!$config['defaults']['enabled']) {
43+
return;
44+
}
45+
46+
$container->setParameter('custom_request_responser', $config);
47+
48+
$loader = new YamlFileLoader(
49+
$container,
50+
new FileLocator(__DIR__ . self::DIR_CONFIG)
51+
);
52+
53+
$loader->load('services.yaml');
54+
$loader->load('listeners.yaml');
55+
56+
if (defined('B_PROLOG_INCLUDED') && B_PROLOG_INCLUDED === true) {
57+
$loader->load('bitrix.yaml');
58+
}
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Prokl\CustomRequestResponserBundle\Event\Interfaces;
4+
5+
use Symfony\Component\HttpKernel\Event\ResponseEvent;
6+
7+
/**
8+
* Interface OnKernelResponseHandlerInterface
9+
* @package Prokl\CustomRequestResponserBundle\Event\Interfaces
10+
*
11+
* @since 20.10.2020
12+
*/
13+
interface OnKernelResponseHandlerInterface
14+
{
15+
/**
16+
* Обработчик события kernel.request.
17+
*
18+
* @param ResponseEvent $event Объект события.
19+
*
20+
* @return void
21+
*/
22+
public function handle(ResponseEvent $event): void;
23+
}
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Prokl\CustomRequestResponserBundle\Event\Listeners;
4+
5+
use Prokl\CustomRequestResponserBundle\Event\Interfaces\OnKernelResponseHandlerInterface;
6+
use Symfony\Component\HttpFoundation\Response;
7+
use Symfony\Component\HttpKernel\Event\ResponseEvent;
8+
9+
/**
10+
* Class CompressHtmlResponse
11+
*
12+
* @package Prokl\CustomRequestResponserBundle\Event\Listeners
13+
*
14+
* @since 22.12.2020
15+
*/
16+
class CompressHtmlResponse implements OnKernelResponseHandlerInterface
17+
{
18+
/**
19+
* Событие kernel.response.
20+
*
21+
* Минификация Response для txt/html ответа.
22+
*
23+
* @param ResponseEvent $event Объект события.
24+
*
25+
* @return void
26+
*
27+
*/
28+
public function handle(ResponseEvent $event): void
29+
{
30+
// Фильтрация внешних нативных маршрутов.
31+
if (!$event->isMasterRequest()
32+
||
33+
$event->getResponse()->getStatusCode() === 404
34+
) {
35+
return;
36+
}
37+
38+
$response = $event->getResponse();
39+
40+
$contentType = $response->headers->get('content-type');
41+
42+
if (is_array($contentType)) {
43+
$contentType = reset($contentType);
44+
}
45+
46+
if (get_class($response) === Response::class
47+
&& ($contentType === null || strpos($contentType, 'text/html') === 0)
48+
) {
49+
$response->setContent(
50+
trim(preg_replace('/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/',
51+
PHP_EOL,
52+
preg_replace('/\h+/u', ' ',
53+
$response->getContent())))
54+
);
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)