Skip to content

Commit fa6ce74

Browse files
committed
支持Symfony:5.4
1 parent aa6ec8f commit fa6ce74

File tree

12 files changed

+155
-43
lines changed

12 files changed

+155
-43
lines changed

Command/PluginCreateCommand.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,31 @@
22

33
namespace Codeages\PluginBundle\Command;
44

5+
use Symfony\Component\Console\Command\Command;
56
use Symfony\Component\Console\Input\InputArgument;
67
use Symfony\Component\Console\Input\InputInterface;
78
use Symfony\Component\Console\Output\OutputInterface;
9+
use Symfony\Component\DependencyInjection\ContainerInterface;
810
use Symfony\Component\Filesystem\Filesystem;
9-
use Sensio\Bundle\GeneratorBundle\Model\Bundle;
10-
use Sensio\Bundle\GeneratorBundle\Command\GeneratorCommand;
11-
use Sensio\Bundle\GeneratorBundle\Generator\BundleGenerator;
1211

13-
class PluginCreateCommand extends GeneratorCommand
12+
class PluginCreateCommand extends Command
1413
{
14+
/**
15+
* @var ContainerInterface
16+
*/
17+
private $container;
18+
19+
public function __construct(ContainerInterface $container)
20+
{
21+
$this->container = $container;
22+
parent::__construct();
23+
}
24+
25+
public function getContainer()
26+
{
27+
return $this->container;
28+
}
29+
1530
protected function configure()
1631
{
1732
$this
@@ -22,7 +37,6 @@ protected function configure()
2237

2338
protected function execute(InputInterface $input, OutputInterface $output)
2439
{
25-
$biz = $this->getContainer()->get('biz');
2640
$code = $input->getArgument('code');
2741

2842
if (!$code) {
@@ -35,7 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
3549

3650
$name = ucfirst($code);
3751
$pluginName = $name.'Plugin';
38-
$rootDir = dirname($this->getContainer()->getParameter('kernel.root_dir'));
52+
$rootDir = dirname($this->container->getParameter('kernel.root_dir'));
3953

4054
$output->writeln(sprintf('Create plugin <comment>%s</comment> :', $name));
4155

Command/PluginRegisterCommand.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,33 @@
22

33
namespace Codeages\PluginBundle\Command;
44

5-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
5+
use Psr\Container\ContainerInterface;
6+
use Symfony\Component\Console\Command\Command;
67
use Symfony\Component\Console\Input\InputArgument;
78
use Symfony\Component\Console\Input\InputInterface;
89
use Symfony\Component\Console\Input\InputOption;
910
use Symfony\Component\Console\Output\OutputInterface;
1011
use Codeages\PluginBundle\System\PluginRegister;
1112
use AppBundle\Common\BlockToolkit;
1213

13-
class PluginRegisterCommand extends ContainerAwareCommand
14+
class PluginRegisterCommand extends Command
1415
{
16+
/**
17+
* @var ContainerInterface
18+
*/
19+
private $container;
20+
21+
public function __construct(ContainerInterface $container)
22+
{
23+
$this->container = $container;
24+
parent::__construct();
25+
}
26+
27+
public function getContainer()
28+
{
29+
return $this->container;
30+
}
31+
1532
protected function configure()
1633
{
1734
$this

Command/PluginRemoveCommand.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,32 @@
22

33
namespace Codeages\PluginBundle\Command;
44

5-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
5+
use Psr\Container\ContainerInterface;
6+
use Symfony\Component\Console\Command\Command;
67
use Symfony\Component\Console\Input\InputArgument;
78
use Symfony\Component\Console\Input\InputInterface;
89
use Symfony\Component\Console\Input\InputOption;
910
use Symfony\Component\Console\Output\OutputInterface;
1011
use Codeages\PluginBundle\System\PluginRegister;
1112

12-
class PluginRemoveCommand extends ContainerAwareCommand
13+
class PluginRemoveCommand extends Command
1314
{
15+
/**
16+
* @var ContainerInterface
17+
*/
18+
private $container;
19+
20+
public function __construct(ContainerInterface $container)
21+
{
22+
$this->container = $container;
23+
parent::__construct();
24+
}
25+
26+
public function getContainer()
27+
{
28+
return $this->container;
29+
}
30+
1431
protected function configure()
1532
{
1633
$this

DependencyInjection/Configuration.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,10 @@ class Configuration implements ConfigurationInterface
1717
*/
1818
public function getConfigTreeBuilder()
1919
{
20-
$treeBuilder = new TreeBuilder();
21-
$rootNode = $treeBuilder->root('codeages_plugin');
22-
2320
// Here you should define the parameters that are allowed to
2421
// configure your bundle. See the documentation linked above for
2522
// more information on that topic.
2623

27-
return $treeBuilder;
24+
return new TreeBuilder('codeages_plugin');
2825
}
2926
}

Event/LazyDispatcher.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Codeages\PluginBundle\Event;
44

55
use Symfony\Component\DependencyInjection\ContainerInterface;
6-
use Symfony\Component\EventDispatcher\Event;
76
use Symfony\Component\EventDispatcher\EventDispatcher;
87
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
98

@@ -19,18 +18,8 @@ public function __construct(ContainerInterface $container)
1918
$this->container = $container;
2019
}
2120

22-
public function dispatch($eventName, Event $event = null)
21+
public function dispatch(object $event, ?string $eventName = null): object
2322
{
24-
if (null === $event) {
25-
$event = new Event();
26-
}
27-
28-
/**
29-
* 已经在symfony3.0 废弃,禁止使用
30-
*/
31-
// $event->setDispatcher($this);
32-
// $event->setName($eventName);
33-
3423
$subscribers = $this->container->get('codeags_plugin.event.lazy_subscribers');
3524

3625
$callbacks = $subscribers->getCallbacks($eventName);

Loader/ThemeTwigLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function findTemplate($name, $throw = true)
3434

3535
if ($file === false || null === $file) {
3636
if ($throw) {
37-
throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', $logicalName));
37+
throw new \Twig\Error\LoaderError(sprintf('Unable to find template "%s".', $logicalName));
3838
}
3939

4040
return false;

Resources/config/services.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
services:
2+
Codeages\PluginBundle\Command\:
3+
resource: '../../Command/*'
4+
arguments:
5+
- "@service_container"
6+
tags: ['console.command']
7+
28
codeages_plugin.dict_collector:
39
class: Codeages\PluginBundle\System\DictCollector
410
arguments:
@@ -25,7 +31,7 @@ services:
2531
public: false
2632
tags:
2733
- { name: twig.extension }
28-
34+
2935
codeages_plugin.slot_twig_extension:
3036
class: Codeages\PluginBundle\Twig\SlotExtension
3137
public: false
@@ -40,6 +46,7 @@ services:
4046
arguments:
4147
- "@codeages_plugin.dict_collector"
4248
- "@service_container"
49+
- '@request_stack'
4350

4451
tags:
4552
- { name: twig.extension }

Tests/Event/LazyDispatcherTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Codeages\PluginBundle\Tests\Event;
4+
5+
use Codeages\Biz\Framework\Event\Event;
6+
use Codeages\PluginBundle\Event\LazyDispatcher;
7+
use Codeages\PluginBundle\Event\LazySubscribers;
8+
use PHPUnit\Framework\TestCase;
9+
use Symfony\Component\DependencyInjection\ContainerBuilder;
10+
11+
class LazyDispatcherTest extends TestCase
12+
{
13+
const CACHE_DIR = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'cache';
14+
15+
public function testDispatch()
16+
{
17+
$kernel = $this->mockKernel();
18+
19+
$container = new ContainerBuilder();
20+
$container->set('kernel', $kernel);
21+
$container->set('codeags_plugin.event.lazy_subscribers', new LazySubscribers($container));
22+
23+
$lazyDispatcher = new LazyDispatcher($container);
24+
$lazyDispatcher->dispatch(new Event(), 'test4');
25+
26+
$this->assertTrue(true);
27+
28+
unlink(self::CACHE_DIR.DIRECTORY_SEPARATOR.'event_map.php');
29+
unlink(self::CACHE_DIR.DIRECTORY_SEPARATOR.'event_map.php.meta');
30+
}
31+
32+
private function mockKernel()
33+
{
34+
$testKernel = $this->getMockBuilder('Codeages\PluginBundle\Tests\Event\Fixture\TestKernel')
35+
->setConstructorArgs(['test', false])
36+
->onlyMethods(['getCacheDir'])
37+
->getMockForAbstractClass();
38+
39+
$testKernel->method('getCacheDir')
40+
->willReturn(__DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'cache');
41+
42+
if (file_exists(self::CACHE_DIR.DIRECTORY_SEPARATOR.'event_map.php')) {
43+
unlink(self::CACHE_DIR.DIRECTORY_SEPARATOR.'event_map.php');
44+
}
45+
46+
if (file_exists(self::CACHE_DIR.DIRECTORY_SEPARATOR.'event_map.php.meta')) {
47+
unlink(self::CACHE_DIR.DIRECTORY_SEPARATOR.'event_map.php.meta');
48+
}
49+
50+
return $testKernel;
51+
}
52+
}

Tests/Event/LazySubscribersTest.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
class LazySubscribersTest extends TestCase
1212
{
13+
const CACHE_DIR = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'cache';
14+
1315
public function testGetEventMapWithoutService()
1416
{
1517
$kernel = $this->mockKernel();
@@ -68,9 +70,8 @@ public function testGetCallbacks()
6870
$test3Callbacks = $lazySubscribers->getCallbacks('test3');
6971
$this->assertEquals($test3, $test3Callbacks);
7072

71-
$cacheFileDir = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'cache';
72-
unlink($cacheFileDir.DIRECTORY_SEPARATOR.'event_map.php');
73-
unlink($cacheFileDir.DIRECTORY_SEPARATOR.'event_map.php.meta');
73+
unlink(self::CACHE_DIR.DIRECTORY_SEPARATOR.'event_map.php');
74+
unlink(self::CACHE_DIR.DIRECTORY_SEPARATOR.'event_map.php.meta');
7475
}
7576

7677
private function mockKernel()
@@ -81,7 +82,15 @@ private function mockKernel()
8182
->getMockForAbstractClass();
8283

8384
$testKernel->method('getCacheDir')
84-
->willReturn(__DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'cache');
85+
->willReturn(self::CACHE_DIR);
86+
87+
if (file_exists(self::CACHE_DIR.DIRECTORY_SEPARATOR.'event_map.php')) {
88+
unlink(self::CACHE_DIR.DIRECTORY_SEPARATOR.'event_map.php');
89+
}
90+
91+
if (file_exists(self::CACHE_DIR.DIRECTORY_SEPARATOR.'event_map.php.meta')) {
92+
unlink(self::CACHE_DIR.DIRECTORY_SEPARATOR.'event_map.php.meta');
93+
}
8594

8695
return $testKernel;
8796
}

Twig/DictExtension.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,30 @@
22

33
namespace Codeages\PluginBundle\Twig;
44

5-
class DictExtension extends \Twig_Extension
5+
use Symfony\Component\HttpFoundation\RequestStack;
6+
use Twig\Extension\AbstractExtension;
7+
use Symfony\Component\DependencyInjection\ContainerInterface;
8+
use Codeages\PluginBundle\System\DictCollector;
9+
10+
class DictExtension extends AbstractExtension
611
{
712
protected $collector;
813
protected $locale;
914
protected $container;
15+
protected $requestStack;
1016

11-
public function __construct($collector, $container)
17+
public function __construct(DictCollector $collector, ContainerInterface $container, RequestStack $requestStack)
1218
{
1319
$this->collector = $collector;
1420
$this->container = $container;
21+
$this->requestStack = $requestStack;
1522
}
1623

1724
public function getFunctions()
1825
{
1926
return array(
20-
new \Twig_SimpleFunction('dict', array($this, 'getDict')),
21-
new \Twig_SimpleFunction('dict_text', array($this, 'getDictText'), array('is_safe' => array('html'))),
27+
new \Twig\TwigFunction('dict', array($this, 'getDict')),
28+
new \Twig\TwigFunction('dict_text', array($this, 'getDictText'), array('is_safe' => array('html'))),
2229
);
2330
}
2431

@@ -44,8 +51,7 @@ public function getName()
4451
private function getLocale()
4552
{
4653
if (!$this->locale) {
47-
$locale = $this->container->get('request')->getLocale();
48-
$this->locale = $locale;
54+
$this->locale = $this->requestStack->getMainRequest()->getLocale();
4955
}
5056

5157
return $this->locale;

0 commit comments

Comments
 (0)