8000 [DI] Add a base class for extension · sun/symfony@57e9d28 · GitHub
[go: up one dir, main page]

Skip to content

Commit 57e9d28

Browse files
vicbfabpot
authored andcommitted
[DI] Add a base class for extension
1 parent 8968bd0 commit 57e9d28

File tree

3 files changed

+116
-90
lines changed

3 files changed

+116
-90
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
2.2.0
5+
-----
6+
7+
* added an Extension base class with sensible defaults to be used in conjunction
8+
with the Config component.
9+
410
2.1.0
511
-----
612

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\DependencyInjection\Extension;
13+
14+
use Symfony\Component\DependencyInjection\Container;
15+
use Symfony\Component\Config\Resource\FileResource;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\Config\Definition\Processor;
18+
use Symfony\Component\Config\Definition\ConfigurationInterface;
19+
20+
/**
21+
* Provides useful features shared by many extensions.
22+
*
23+
* @author Fabien Potencier <fabien@symfony.com>
24+
*/
25+
abstract class Extension implements ExtensionInterface, ConfigurationExtensionInterface
26+
{
27+
/**
28+
* Returns the base path for the XSD files.
29+
*
30+
* @return string The XSD base path
31+
*/
32+
public function getXsdValidationBasePath()
33+
{
34+
return false;
35+
}
36+
37+
/**
38+
* Returns the namespace to be used for this extension (XML namespace).
39+
*
40+
* @return string The XML namespace
41+
*/
42+
public function getNamespace()
43+
{
44+
return 'http://example.org/schema/dic/'.$this->getAlias();
45+
}
46+
47+
/**
48+
* Returns the recommended alias to use in XML.
49+
*
50+
* This alias is also the mandatory prefix to use when using YAML.
51+
*
52+
* This convention is to remove the "Extension" postfix from the class
53+
* name and then lowercase and underscore the result. So:
54+
*
55+
* AcmeHelloExtension
56+
*
57+
* becomes
58+
*
59+
* acme_hello
60+
*
61+
* This can be overridden in a sub-class to specify the alias manually.
62+
*
63+
* @return string The alias
64+
*
65+
* @throws \BadMethodCallException When the extension name does not follow conventions
66+
*/
67+
public function getAlias()
68+
{
69+
$className = get_class($this);
70+
if (substr($className, -9) != 'Extension') {
71+
throw new \BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
72+
}
73+
$classBaseName = substr(strrchr($className, '\\'), 1, -9);
74+
75+
return Container::underscore($classBaseName);
76+
}
77+
78+
final protected function processConfiguration(ConfigurationInterface $configuration, array $configs)
79+
{
80+
$processor = new Processor();
81+
82+
return $processor->processConfiguration($configuration, $configs);
83+
}
84+
85+
/**
86+
* {@inheritDoc}
87+
*/
88+
public function getConfiguration(array $config, ContainerBuilder $container)
89+
{
90+
1E79 $reflected = new \ReflectionClass($this);
91+
$namespace = $reflected->getNamespaceName();
92+
93+
$class = $namespace . '\\Configuration';
94+
if (class_exists($class)) {
95+
$r = new \ReflectionClass($class);
96+
$container->addResource(new FileResource($r->getFileName()));
97+
98+
if (!method_exists($class, '__construct')) {
99+
$configuration = new $class();
100+
101+
return $configuration;
102+
}
103+
}
104+
105+
return null;
106+
}
107+
}

src/Symfony/Component/HttpKernel/DependencyInjection/Extension.php

Lines changed: 3 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,14 @@
1111

1212
namespace Symfony\Component\HttpKernel\DependencyInjection;
1313

14-
use Symfony\Component\Config\Definition\Processor;
15-
use Symfony\Component\Config\Resource\FileResource;
16-
use Symfony\Component\Config\Definition\ConfigurationInterface;
17-
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
18-
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
19-
use Symfony\Component\DependencyInjection\ContainerBuilder;
20-
use Symfony\Component\DependencyInjection\Container;
14+
use Symfony\Component\DependencyInjection\Extension\Extension as BaseExtension;
2115

2216
/**
23-
* Provides useful features shared by many extensions.
17+
* Allow adding classes to the class cache.
2418
*
2519
* @author Fabien Potencier <fabien@symfony.com>
2620
*/
27-
abstract class Extension implements ExtensionInterface, ConfigurationExtensionInterface
21+
abstract class Extension extends BaseExtension
2822
{
2923
private $classes = array();
3024

@@ -47,85 +41,4 @@ public function addClassesToCompile(array $classes)
4741
{
4842
$th 10000 is->classes = array_merge($this->classes, $classes);
4943
}
50-
51-
/**
52-
* Returns the base path for the XSD files.
53-
*
54-
* @return string The XSD base path
55-
*/
56-
public function getXsdValidationBasePath()
57-
{
58-
return false;
59-
}
60-
61-
/**
62-
* Returns the namespace to be used for this extension (XML namespace).
63-
*
64-
* @return string The XML namespace
65-
*/
66-
public function getNamespace()
67-
{
68-
return 'http://example.org/schema/dic/'.$this->getAlias();
69-
}
70-
71-
/**
72-
* Returns the recommended alias to use in XML.
73-
*
74-
* This alias is also the mandatory prefix to use when using YAML.
75-
*
76-
* This convention is to remove the "Extension" postfix from the class
77-
* name and then lowercase and underscore the result. So:
78-
*
79-
* AcmeHelloExtension
80-
*
81-
* becomes
82-
*
83-
* acme_hello
84-
*
85-
* This can be overridden in a sub-class to specify the alias manually.
86-
*
87-
* @return string The alias
88-
*
89-
* @throws \BadMethodCallException When the extension name does not follow conventions
90-
*/
91-
public function getAlias()
92-
{
93-
$className = get_class($this);
94-
if (substr($className, -9) != 'Extension') {
95-
throw new \BadMethodCallException('This extension does not follow the naming convention; you must overwrite the getAlias() method.');
96-
}
97-
$classBaseName = substr(strrchr($className, '\\'), 1, -9);
98-
99-
return Container::underscore($classBaseName);
100-
}
101-
102-
final protected function processConfiguration(ConfigurationInterface $configuration, array $configs, $normalizeKeys = true)
103-
{
104-
$processor = new Processor();
105-
106-
return $processor->processConfiguration($configuration, $configs, $normalizeKeys);
107-
}
108-
109-
/**
110-
* {@inheritDoc}
111-
*/
112-
public function getConfiguration(array $config, ContainerBuilder $container)
113-
{
114-
$reflected = new \ReflectionClass($this);
115-
$namespace = $reflected->getNamespaceName();
116-
117-
$class = $namespace . '\\Configuration';
118-
if (class_exists($class)) {
119-
$r = new \ReflectionClass($class);
120-
$container->addResource(new FileResource($r->getFileName()));
121-
122-
if (!method_exists($class, '__construct')) {
123-
$configuration = new $class();
124-
125-
return $configuration;
126-
}
127-
}
128-
129-
return null;
130-
}
13144
}

0 commit comments

Comments
 (0)
0