-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[2.1] Method Injection #881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
37ad063
[DependencyInjection] adds #835 (method injection)
schmittjoh 1628ab9
[DependencyInjection] added merging logic
schmittjoh 10107ca
[DependencyInjection] added generation pass
schmittjoh 2b7ab78
[DependencyInjection] added doc comment
schmittjoh bdcf802
[DependencyInjection] added dumpValue
schmittjoh 440dcb3
Merge remote branch 'origin/master' into methodInjection
schmittjoh a8679cc
[DependencyInjection] added some doc comments
schmittjoh 648c778
[DependencyInjection] updated PassConfig
schmittjoh 15f35f1
[HttpKernel][DependencyInjection] fixed integration
schmittjoh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
src/Symfony/Component/DependencyInjection/Compiler/GenerateLookupMethodClassesPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\Compiler; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\DependencyInjection\Parameter; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
/** | ||
* Generates the classes which implement the requested lookup methods. | ||
* | ||
* @author Johannes M. Schmitt <schmittjoh@gmail.com> | ||
*/ | ||
class GenerateLookupMethodClassesPass implements CompilerPassInterface | ||
{ | ||
private $generatedClasses = array(); | ||
private $container; | ||
private $currentId; | ||
private $cacheDir; | ||
|
||
public function __construct($cacheDir) | ||
{ | ||
$this->cacheDir = $cacheDir; | ||
} | ||
|
||
public function process(ContainerBuilder $container) | ||
{ | ||
$this->container = $container; | ||
$this->generatedClasses = array(); | ||
$this->cleanUpCacheDir($this->cacheDir); | ||
|
||
foreach ($container->getDefinitions() as $id => $definition) { | ||
if ($definition->isSynthetic() || $definition->isAbstract()) { | ||
continue; | ||
} | ||
if (!$methods = $definition->getLookupMethods()) { | ||
continue; | ||
} | ||
|
||
$this->currentId = $id; | ||
$this->generateClass($definition, $this->cacheDir); | ||
} | ||
} | ||
|
||
private function cleanUpCacheDir($dir) | ||
{ | ||
if (!is_dir($dir)) { | ||
if (false === @mkdir($dir, 0777, true)) { | ||
throw new \RuntimeException(sprintf('The cache directory "%s" could not be created.', $dir)); | ||
} | ||
|
||
return; | ||
} | ||
|
||
if (!is_writable($dir)) { | ||
throw new \RuntimeException(sprintf('The cache directory "%s" is not writable.', $dir)); | ||
} | ||
|
||
foreach (new \DirectoryIterator($dir) as $file) { | ||
if ('.' === $file->getFileName() || !is_file($file->getPathName())) { | ||
continue; | ||
} | ||
|
||
if (false === @unlink($file->getPathName())) { | ||
throw new \RuntimeException(sprintf('Could not delete auto-generated file "%s".', $file->getPathName())); | ||
} | ||
} | ||
} | ||
|
||
private function generateClass(Definition $definition, $cacheDir) | ||
{ | ||
$code = <<<'EOF' | ||
<?php | ||
|
||
namespace Symfony\Component\DependencyInjection\LookupMethodClasses; | ||
%s | ||
/** | ||
* This class has been auto-generated by the Symfony Dependency Injection | ||
* Component. | ||
* | ||
* Manual changes to it will be lost. | ||
* | ||
* You can modify this class by changing your "lookup_method" configuration | ||
* for service "%s". | ||
*/ | ||
class %s extends \%s | ||
{ | ||
private $__symfonyDependencyInjectionContainer; | ||
%s | ||
} | ||
EOF; | ||
|
||
// other file requirement | ||
if ($file = $definition->getFile()) { | ||
$require = sprintf("\nrequire_once %s;\n", var_export($file, true)); | ||
} else { | ||
$require = ''; | ||
} | ||
|
||
// get class name | ||
$class = new \ReflectionClass($definition->getClass()); | ||
$i = 1; | ||
do { | ||
$className = $class->getShortName(); | ||
|
||
if ($i > 1) { | ||
$className .= '_'.$i; | ||
} | ||
|
||
$i += 1; | ||
} while (isset($this->generatedClasses[$className])); | ||
$this->generatedClasses[$className] = true; | ||
|
||
$lookupMethod = <<<'EOF' | ||
|
||
%s function %s() | ||
{ | ||
return %s; | ||
} | ||
EOF; | ||
$lookupMethods = ''; | ||
foreach ($definition->getLookupMethods() as $name => $value) { | ||
if (!$class->hasMethod($name)) { | ||
throw new \RuntimeException(sprintf('The class "%s" has no method named "%s".', $class->getName(), $name)); | ||
} | ||
$method = $class->getMethod($name); | ||
if ($method->isFinal()) { | ||
throw new \RuntimeException(sprintf('The method "%s::%s" is marked as final and cannot be declared as lookup-method.', $class->getName(), $name)); | ||
} | ||
if ($method->isPrivate()) { | ||
throw new \RuntimeException(sprintf('The method "%s::%s" is marked as private and cannot be declared as lookup-method.', $class->getName(), $name)); | ||
} | ||
if ($method->getParameters()) { | ||
throw new \RuntimeException(sprintf('The method "%s::%s" must have a no-arguments signature if you want to use it as lookup-method.', $class->getName(), $name)); | ||
} | ||
|
||
$lookupMethods .= sprintf($lookupMethod, | ||
$method->isPublic() ? 'public' : 'protected', | ||
$name, | ||
$this->dumpValue($value) | ||
); | ||
} | ||
|
||
$code = sprintf($code, $require, $this->currentId, $className, $class->getName(), $lookupMethods); | ||
file_put_contents($cacheDir.'/'.$className.'.php', $code); | ||
require_once $cacheDir.'/'.$className.'.php'; | ||
$definition->setClass('Symfony\Component\DependencyInjection\LookupMethodClasses\\'.$className); | ||
$definition->setFile($cacheDir.'/'.$className.'.php'); | ||
$definition->setProperty('__symfonyDependencyInjectionContainer', new Reference('service_container')); | ||
$definition->setLookupMethods(array()); | ||
} | ||
|
||
private function dumpValue($value) | ||
{ | ||
if ($value instanceof Parameter) { | ||
return var_export($this->container->getParameter((string) $value), true); | ||
} else if ($value instanceof Reference) { | ||
$id = (string) $value; | ||
if ($this->container->hasAlias($id)) { | ||
$this->container->setAlias($id, (string) $this->container->getAlias()); | ||
} else if ($this->container->hasDefinition($id)) { | ||
$this->container->getDefinition($id)->setPublic(true); | ||
} | ||
|
||
return '$this->__symfonyDependencyInjectionContainer->get('.var_export($id, true).', '.var_export($value->getInvalidBehavior(), true).')'; | ||
} else if (is_array($value) || is_scalar($value) || null === $value) { | ||
return var_export($value, true); | ||
} | ||
|
||
throw new \RuntimeException(sprintf('Invalid value for lookup method of service "%s": %s', $this->currentId, json_encode($value))); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why add the pass here instead of in the container?