Skip to content

Commit 8908ab9

Browse files
committed
Upgrading Symfony components to be compatible with Symfony 5
1 parent 0366e9a commit 8908ab9

File tree

6 files changed

+158
-6
lines changed

6 files changed

+158
-6
lines changed

composer.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@
2828
"mouf/classname-mapper": "~1.0",
2929
"doctrine/cache": "^1.6",
3030
"greenlion/php-sql-parser": "^4.3.0",
31-
"phlib/logger": "^3.0.1",
32-
"symfony/console": "^2 || ^3 || ^4",
31+
"symfony/console": "^2 || ^3 || ^4 || ^5",
3332
"mouf/utils.log.psr.multi-logger": "^1.0",
34-
"symfony/filesystem": "^2.7 || ^3 || ^4",
33+
"symfony/filesystem": "^2.7 || ^3 || ^4 || ^5",
3534
"ramsey/uuid": "^3.7",
3635
"doctrine/annotations": "^1.6",
3736
"zendframework/zend-code": "^3.3.1",
@@ -46,7 +45,7 @@
4645
"php-coveralls/php-coveralls": "^2.1",
4746
"wa72/simplelogger" : "^1.0",
4847
"friendsofphp/php-cs-fixer": "^2.15.1",
49-
"symfony/process": "^3 || ^4",
48+
"symfony/process": "^3 || ^4 || ^5",
5049
"thecodingmachine/tdbm-fluid-schema-builder": "^1.0.0",
5150
"phpstan/phpstan": "^0.11.5",
5251
"thecodingmachine/phpstan-strict-rules": "^0.11.0",

src/Commands/GenerateCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5757
$tdbmService->generateAllDaosAndBeans();
5858

5959
$multiLogger->notice('Finished regenerating DAOs and beans');
60+
61+
return 0;
6062
}
6163
}

src/TDBMService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
use TheCodingMachine\TDBM\QueryFactory\FindObjectsFromSqlQueryFactory;
4040
use TheCodingMachine\TDBM\QueryFactory\FindObjectsQueryFactory;
4141
use TheCodingMachine\TDBM\QueryFactory\FindObjectsFromRawSqlQueryFactory;
42+
use TheCodingMachine\TDBM\Utils\Logs\LevelFilter;
4243
use TheCodingMachine\TDBM\Utils\ManyToManyRelationshipPathDescriptor;
4344
use TheCodingMachine\TDBM\Utils\NamingStrategyInterface;
4445
use TheCodingMachine\TDBM\Utils\TDBMDaoGenerator;
45-
use Phlib\Logger\Decorator\LevelFilter;
4646
use Psr\Log\LoggerInterface;
4747
use Psr\Log\LogLevel;
4848
use Psr\Log\NullLogger;

src/Utils/Logs/LevelFilter.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\TDBM\Utils\Logs;
5+
6+
use Psr\Log\AbstractLogger;
7+
use Psr\Log\InvalidArgumentException;
8+
use Psr\Log\LoggerInterface;
9+
use Psr\Log\LogLevel;
10+
use function array_search;
11+
use function sprintf;
12+
13+
class LevelFilter extends AbstractLogger
14+
{
15+
/**
16+
* Logging levels from syslog protocol defined in RFC 5424
17+
*
18+
* @var string[] $levels Logging levels
19+
*/
20+
private static $levels = array(
21+
LogLevel::EMERGENCY, // 0
22+
LogLevel::ALERT, // 1
23+
LogLevel::CRITICAL, // 2
24+
LogLevel::ERROR, // 3
25+
LogLevel::WARNING, // 4
26+
LogLevel::NOTICE, // 5
27+
LogLevel::INFO, // 6
28+
LogLevel::DEBUG // 7
29+
);
30+
31+
/**
32+
* @var int
33+
*/
34+
private $logLevel;
35+
/**
36+
* @var LoggerInterface
37+
*/
38+
private $logger;
39+
40+
/**
41+
* @param LoggerInterface $logger
42+
* @param string $level \Psr\Log\LogLevel string
43+
*/
44+
public function __construct(LoggerInterface $logger, $level)
45+
{
46+
$this->logger = $logger;
47+
48+
$this->logLevel = array_search($level, self::$levels, true);
49+
if ($this->logLevel === false) {
50+
throw new InvalidArgumentException(
51+
sprintf(
52+
'Cannot use logging level "%s"',
53+
$level
54+
)
55+
);
56+
}
57+
}
58+
59+
/**
60+
* Logs with an arbitrary level.
61+
*
62+
* @param mixed $level
63+
* @param string $message
64+
* @param array $context
65+
*
66+
* @return void
67+
*/
68+
public function log($level, $message, array $context = array())
69+
{
70+
$levelCode = array_search($level, self::$levels, true);
71+
if ($levelCode === false) {
72+
throw new InvalidArgumentException(
73+
sprintf(
74+
'Cannot use unknown logging level "%s"',
75+
$level
76+
)
77+
);
78+
}
79+
if ($levelCode > $this->logLevel) {
80+
return;
81+
}
82+
83+
$this->logger->log($level, $message, $context);
84+
}
85+
}

tests/TDBMDaoGeneratorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,7 @@ public function testCorrectTypeForPrimaryKeyAfterSave(): void
15521552
*/
15531553
public function testPSR2Compliance(): void
15541554
{
1555-
$process = new Process('vendor/bin/php-cs-fixer fix src/Test/ --dry-run --diff --rules=@PSR2');
1555+
$process = new Process(['vendor/bin/php-cs-fixer', 'fix', 'src/Test/', '--dry-run', '--diff', '--rules=@PSR2']);
15561556
$process->run();
15571557

15581558
// executes after the command finishes

tests/Utils/Logs/LevelFilterTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace TheCodingMachine\TDBM\Utils\Logs;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Psr\Log\LoggerInterface;
7+
use Psr\Log\LogLevel;
8+
9+
class LevelFilterTest extends TestCase
10+
{
11+
public function testIsPsrLog()
12+
{
13+
$levelFilter = new LevelFilter($this->getMockLoggerInterface(), LogLevel::DEBUG);
14+
$this->assertInstanceOf('\Psr\Log\LoggerInterface', $levelFilter);
15+
}
16+
17+
public function testLog()
18+
{
19+
$loggerInterface = $this->getMockLoggerInterface();
20+
21+
// We expect this test to call the loggerInterface twice to log the ERROR and CRITICAL messages
22+
// and to ignore the WARNING log message
23+
$loggerInterface->expects($this->exactly(2))
24+
->method('log')
25+
->withConsecutive(
26+
[LogLevel::ERROR],
27+
[LogLevel::CRITICAL]
28+
);
29+
30+
$levelFilter = new LevelFilter($loggerInterface, LogLevel::ERROR);
31+
32+
// Log message with equal priority to the level filter (should be get logged)
33+
$levelFilter->log(LogLevel::ERROR, 'TEST ERROR MESSAGE');
34+
35+
// Log message with higher priority than the level filter (should be logged)
36+
$levelFilter->log(LogLevel::CRITICAL, 'TEST CRITICAL MESSAGE');
37+
38+
// Log message with lower priority than the level filter (should not be logged)
39+
$levelFilter->log(LogLevel::WARNING, 'TEST WARNING MESSAGE');
40+
}
41+
42+
public function testInvalidConstructorLogLevel()
43+
{
44+
// We expect an exception to be thrown when specifying an invalid logging level in the constructor
45+
$this->expectException('\Psr\Log\InvalidArgumentException');
46+
$levelFilter = new LevelFilter($this->getMockLoggerInterface(), 'InvalidLogLevel');
47+
}
48+
49+
public function testInvalidLogLogLevel()
50+
{
51+
// We expect an exception to be thrown when specifying an invalid logging level in the log method parameter
52+
$levelFilter = new LevelFilter($this->getMockLoggerInterface(), LogLevel::DEBUG);
53+
54+
$this->expectException('\Psr\Log\InvalidArgumentException');
55+
$levelFilter->log('InvalidLogLevel', 'TEST LOG MESSAGE');
56+
}
57+
58+
/**
59+
* @return LoggerInterface
60+
*/
61+
protected function getMockLoggerInterface()
62+
{
63+
$loggerInterface = $this->createMock('\Psr\Log\LoggerInterface');
64+
return $loggerInterface;
65+
}
66+
}

0 commit comments

Comments
 (0)