Skip to content

Commit fda9f4b

Browse files
committed
Merge pull request #224 from symfony2admingenerator/feat-28comp
Symfony 2.8 & 3.0 compatibility
2 parents 26f2c70 + a956e11 commit fda9f4b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1293
-1892
lines changed

AdmingeneratorGeneratorBundle.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
namespace Admingenerator\GeneratorBundle;
44

5-
use Admingenerator\GeneratorBundle\DependencyInjection\Compiler\TwigLoaderCompilerPass;
65
use Symfony\Component\HttpKernel\Bundle\Bundle;
76
use Symfony\Component\DependencyInjection\ContainerBuilder;
87
use Symfony\Component\DependencyInjection\ContainerInterface;
98
use Admingenerator\GeneratorBundle\ClassLoader\AdmingeneratedClassLoader;
10-
use Admingenerator\GeneratorBundle\DependencyInjection\Compiler\ValidatorCompilerPass;
11-
use Admingenerator\GeneratorBundle\DependencyInjection\Compiler\FormCompilerPass;
9+
use Admingenerator\GeneratorBundle\DependencyInjection\Compiler\TwigLoaderPass;
10+
use Admingenerator\GeneratorBundle\DependencyInjection\Compiler\ValidatorPass;
11+
use Admingenerator\GeneratorBundle\DependencyInjection\Compiler\FormPass;
12+
use Symfony\Component\HttpKernel\KernelInterface;
1213

1314
class AdmingeneratorGeneratorBundle extends Bundle
1415
{
@@ -17,6 +18,19 @@ class AdmingeneratorGeneratorBundle extends Bundle
1718
*/
1819
private $classLoaderInitialized = false;
1920

21+
/**
22+
* @var KernelInterface
23+
*/
24+
private $kernel;
25+
26+
/**
27+
* @param KernelInterface $kernel
28+
*/
29+
public function __construct(KernelInterface $kernel)
30+
{
31+
$this->kernel = $kernel;
32+
}
33+
2034
/**
2135
* (non-PHPdoc)
2236
* @see \Symfony\Component\HttpKernel\Bundle\Bundle::boot()
@@ -36,17 +50,17 @@ public function build(ContainerBuilder $container)
3650

3751
parent::build($container);
3852

39-
$container->addCompilerPass(new ValidatorCompilerPass());
40-
$container->addCompilerPass(new FormCompilerPass());
41-
$container->addCompilerPass(new TwigLoaderCompilerPass());
53+
$container->addCompilerPass(new ValidatorPass());
54+
$container->addCompilerPass(new FormPass());
55+
$container->addCompilerPass(new TwigLoaderPass());
4256
}
4357

4458
/**
4559
* @return \Admingenerator\GeneratorBundle\DependencyInjection\AdmingeneratorGeneratorExtension
4660
*/
4761
public function getContainerExtension()
4862
{
49-
$this->extension = new DependencyInjection\AdmingeneratorGeneratorExtension();
63+
$this->extension = new DependencyInjection\AdmingeneratorGeneratorExtension($this->kernel);
5064

5165
return $this->extension;
5266
}
@@ -61,9 +75,9 @@ private function initAdmingeneratorClassLoader(ContainerInterface $container)
6175
if (!$this->classLoaderInitialized) {
6276
$this->classLoaderInitialized = true;
6377

64-
$AdmingeneratedClassLoader = new AdmingeneratedClassLoader();
65-
$AdmingeneratedClassLoader->setBasePath($container->getParameter('kernel.cache_dir'));
66-
$AdmingeneratedClassLoader->register();
78+
$admingeneratedClassLoader = new AdmingeneratedClassLoader();
79+
$admingeneratedClassLoader->setBasePath($container->getParameter('kernel.cache_dir'));
80+
$admingeneratedClassLoader->register();
6781
}
6882
}
6983
}

Builder/Admin/ActionsBuilder.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,10 @@ public function getBatchActions()
6565

6666
protected function setUserBatchActionConfiguration(Action $action)
6767
{
68-
$builderOptions = $this->getVariable(
69-
sprintf('batch_actions[%s]', $action->getName()),
70-
array(),
71-
true
72-
);
68+
$batchActions = $this->getVariable('batch_actions', array());
69+
$builderOptions = is_array($batchActions) && array_key_exists($action->getName(), $batchActions)
70+
? $batchActions[$action->getName()]
71+
: array();
7372

7473
$globalOptions = $this->getGenerator()->getFromYaml(
7574
'params.batch_actions.'.$action->getName(), array()

Builder/Admin/BaseBuilder.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,10 @@ protected function createColumn($columnName, $withForms = false)
182182
));
183183
}
184184

185-
$fieldOptions = $this->getVariable(
186-
sprintf('fields[%s]', $column->getName()),
187-
array(),
188-
true
189-
);
185+
$fields = $this->getVariable('fields', array());
186+
$fieldOptions = is_array($fields) && array_key_exists($column->getName(), $fields)
187+
? $fields[$column->getName()]
188+
: array();
190189

191190
if (array_key_exists('addFormOptions', $fieldOptions)) {
192191
$column->setAddFormOptions($fieldOptions['addFormOptions']);
@@ -221,22 +220,16 @@ public function setColumnClass($columnClass)
221220
*/
222221
protected function getFieldOption(Column $column, $optionName, $default = null)
223222
{
224-
$options = $this->getVariable(
225-
sprintf('fields[%s]', $column->getName()),
226-
array(),
227-
true
228-
);
223+
$optionsFields = $this->getVariable('fields', array());
224+
$options = is_array($optionsFields) && array_key_exists($column->getName(), $optionsFields) ? $optionsFields[$column->getName()] : array();
229225

230226
return isset($options[$optionName]) ? $options[$optionName] : $default;
231227
}
232228

233229
protected function setUserColumnConfiguration(Column $column)
234230
{
235-
$options = $this->getVariable(
236-
sprintf('fields[%s]', $column->getName()),
237-
array(),
238-
true
239-
);
231+
$optionsFields = $this->getVariable('fields', array());
232+
$options = is_array($optionsFields) && array_key_exists($column->getName(), $optionsFields) ? $optionsFields[$column->getName()] : array();
240233

241234
foreach ($options as $option => $value) {
242235
$column->setProperty($option, $value);
@@ -389,11 +382,8 @@ public function getActions()
389382

390383
protected function setUserActionConfiguration(Action $action)
391384
{
392-
$builderOptions = $this->getVariable(
393-
sprintf('actions[%s]', $action->getName()),
394-
array(),
395-
true
396-
);
385+
$actions = $this->getVariable('actions', array());
386+
$builderOptions = is_array($actions) && array_key_exists($action->getName(), $actions) ? $actions[$action->getName()] : array();
397387

398388
$globalOptions = $this->getGenerator()->getFromYaml(
399389
'params.actions.'.$action->getName(),
@@ -464,11 +454,10 @@ public function getObjectActions()
464454

465455
protected function setUserObjectActionConfiguration(Action $action)
466456
{
467-
$builderOptions = $this->getVariable(
468-
sprintf('object_actions[%s]', $action->getName()),
469-
array(),
470-
true
471-
);
457+
$objectActions = $this->getVariable('object_actions', array());
458+
$builderOptions = is_array($objectActions) && array_key_exists($action->getName(), $objectActions)
459+
? $objectActions[$action->getName()]
460+
: array();
472461

473462
$globalOptions = $this->getGenerator()->getFromYaml(
474463
'params.object_actions.'.$action->getName(),

Builder/Admin/EditBuilder.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,19 @@ public function getYamlKey()
1919
{
2020
return 'edit';
2121
}
22+
23+
/**
24+
* Retrieve the FQCN formType used by this builder
25+
*
26+
* @return string
27+
*/
28+
public function getFormType()
29+
{
30+
return sprintf(
31+
'%s\\%s\\Form\Type\\%s\\EditType',
32+
$this->getVariable('namespace_prefix'),
33+
$this->getVariable('bundle_name'),
34+
$this->getBaseGeneratorName()
35+
);
36+
}
2237
}

Builder/Admin/ListBuilder.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,21 @@ public function getYamlKey()
3838
return 'list';
3939
}
4040

41+
/**
42+
* Retrieve the FQCN formType used by this builder
43+
*
44+
* @return string
45+
*/
46+
public function getFormType()
47+
{
48+
return sprintf(
49+
'%s\\%s\\Form\Type\\%s\\FiltersType',
50+
$this->getVariable('namespace_prefix'),
51+
$this->getVariable('bundle_name'),
52+
$this->getBaseGeneratorName()
53+
);
54+
}
55+
4156
public function getFilterColumns()
4257
{
4358
if (null === $this->filterColumns) {
@@ -161,11 +176,10 @@ public function getBatchActions()
161176

162177
protected function setUserBatchActionConfiguration(Action $action)
163178
{
164-
$builderOptions = $this->getVariable(
165-
sprintf('batch_actions[%s]', $action->getName()),
166-
array(),
167-
true
168-
);
179+
$batchActions = $this->getVariable('batch_actions', array());
180+
$builderOptions = is_array($batchActions) && array_key_exists($action->getName(), $batchActions)
181+
? $batchActions[$action->getName()]
182+
: array();
169183

170184
$globalOptions = $this->getGenerator()->getFromYaml(
171185
'params.batch_actions.'.$action->getName(),

Builder/Admin/NewBuilder.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,19 @@ public function getYamlKey()
1919
{
2020
return 'new';
2121
}
22+
23+
/**
24+
* Retrieve the FQCN formType used by this builder
25+
*
26+
* @return string
27+
*/
28+
public function getFormType()
29+
{
30+
return sprintf(
31+
'%s\\%s\\Form\Type\\%s\\NewType',
32+
$this->getVariable('namespace_prefix'),
33+
$this->getVariable('bundle_name'),
34+
$this->getBaseGeneratorName()
35+
);
36+
}
2237
}

CacheWarmer/GeneratorCacheWarmer.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
77
use Symfony\Component\DependencyInjection\ContainerInterface;
88
use Admingenerator\GeneratorBundle\Exception\GeneratedModelClassNotFoundException;
9+
use Admingenerator\GeneratorBundle\Filesystem\GeneratorsFinder;
910

1011
/**
1112
* Generate all admingenerated bundle on warmup
@@ -14,22 +15,25 @@
1415
*/
1516
class GeneratorCacheWarmer implements CacheWarmerInterface
1617
{
17-
18+
/**
19+
* @var ContainerInterface
20+
*/
1821
protected $container;
1922

23+
/**
24+
* @var GeneratorsFinder
25+
*/
2026
protected $finder;
2127

22-
protected $yaml_datas = array();
23-
2428
/**
2529
* Constructor.
2630
*
2731
* @param ContainerInterface $container The dependency injection container
2832
*/
29-
public function __construct(ContainerInterface $container, GeneratorFinder $finder)
33+
public function __construct(ContainerInterface $container)
3034
{
3135
$this->container = $container;
32-
$this->finder = $finder;
36+
$this->finder = new GeneratorsFinder($container->get('kernel'));
3337
}
3438

3539
/**
@@ -43,7 +47,7 @@ public function warmUp($cacheDir)
4347
$this->propelInit();
4448
}
4549

46-
foreach ($this->finder->findAllGeneratorYamls() as $yaml) {
50+
foreach ($this->finder->findAll() as $yaml) {
4751
try {
4852
$this->buildFromYaml($yaml);
4953
} catch (GeneratedModelClassNotFoundException $e) {
@@ -68,10 +72,8 @@ public function isOptional()
6872

6973
protected function buildFromYaml($file)
7074
{
71-
$this->parseYaml($file);
72-
$service = $this->yaml_datas['generator'];
73-
74-
$generator = $this->container->get($service);
75+
$generatorConfiguration = Yaml::parse(file_get_contents($file));
76+
$generator = $this->container->get($generatorConfiguration['generator']);
7577
$generator->setGeneratorYml($file);
7678

7779
// windows support too
@@ -81,12 +83,7 @@ protected function buildFromYaml($file)
8183
$generator->setBaseGeneratorName('');
8284
}
8385

84-
$generator->build();
85-
}
86-
87-
protected function parseYaml($file)
88-
{
89-
$this->yaml_datas = Yaml::parse(file_get_contents($file));
86+
$generator->build(true);
9087
}
9188

9289
/**

ClassLoader/AdmingeneratedClassLoader.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
*/
1111
class AdmingeneratedClassLoader
1212
{
13-
protected $base_path;
13+
/**
14+
* @var string
15+
*/
16+
protected $basePath;
1417

1518
/**
1619
* Registers this instance as an autoloader.
@@ -22,9 +25,13 @@ public function register($prepend = false)
2225
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
2326
}
2427

25-
public function setBasePath($base_path)
28+
/**
29+
* @param string $basePath
30+
* @return string
31+
*/
32+
public function setBasePath($basePath)
2633
{
27-
return $this->base_path = $base_path;
34+
return $this->basePath = $basePath;
2835
}
2936

3037
/**
@@ -35,14 +42,14 @@ public function setBasePath($base_path)
3542
public function loadClass($class)
3643
{
3744
if (0 === strpos($class, 'Admingenerated')) {
38-
$file_path = $this->base_path.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
45+
$filePath = $this->basePath.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
3946

40-
if (!file_exists($file_path)) {
47+
if (!file_exists($filePath)) {
4148
$this->generateEmptyController($class);
4249
}
4350

44-
if (file_exists($file_path)) {
45-
require $file_path;
51+
if (file_exists($filePath)) {
52+
require $filePath;
4653
}
4754
}
4855
}
@@ -52,7 +59,7 @@ public function loadClass($class)
5259
*/
5360
protected function generateEmptyController($class)
5461
{
55-
$generator = new EmptyGenerator($this->base_path);
62+
$generator = new EmptyGenerator($this->basePath);
5663

5764
$parts = explode('\\',$class);
5865
$controllerName = $parts[count($parts) - 1];
@@ -71,7 +78,7 @@ protected function generateEmptyController($class)
7178
'require_pk' => 'ListController' != $controllerName // We don't care about ActionsController and filters
7279
));
7380

74-
$generator->writeOnDisk($this->base_path);
81+
$generator->writeOnDisk($this->basePath);
7582
}
7683

7784
}

0 commit comments

Comments
 (0)