Initial commit with Symfony 2.1+Vendors
Signed-off-by: Gergely POLONKAI (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace JMS\DiExtraBundle\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Alias;
|
||||
use JMS\DiExtraBundle\Exception\RuntimeException;
|
||||
use JMS\DiExtraBundle\Config\ServiceFilesResource;
|
||||
use Symfony\Component\Config\Resource\FileResource;
|
||||
use Symfony\Component\DependencyInjection\DefinitionDecorator;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use JMS\DiExtraBundle\Finder\PatternFinder;
|
||||
use Symfony\Component\HttpKernel\KernelInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
|
||||
class AnnotationConfigurationPass implements CompilerPassInterface
|
||||
{
|
||||
private $kernel;
|
||||
private $finder;
|
||||
|
||||
public function __construct(KernelInterface $kernel)
|
||||
{
|
||||
$this->kernel = $kernel;
|
||||
$this->finder = new PatternFinder('JMS\DiExtraBundle\Annotation');
|
||||
}
|
||||
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
$reader = $container->get('annotation_reader');
|
||||
$factory = $container->get('jms_di_extra.metadata.metadata_factory');
|
||||
$converter = $container->get('jms_di_extra.metadata.converter');
|
||||
|
||||
$directories = $this->getScanDirectories($container);
|
||||
if (!$directories) {
|
||||
$container->getCompiler()->addLogMessage('No directories configured for AnnotationConfigurationPass.');
|
||||
return;
|
||||
}
|
||||
|
||||
$files = $this->finder->findFiles($directories);
|
||||
$container->addResource(new ServiceFilesResource($files, $directories));
|
||||
foreach ($files as $file) {
|
||||
$container->addResource(new FileResource($file));
|
||||
require_once $file;
|
||||
|
||||
$className = $this->getClassName($file);
|
||||
|
||||
if (null === $metadata = $factory->getMetadataForClass($className)) {
|
||||
continue;
|
||||
}
|
||||
if (null === $metadata->getOutsideClassMetadata()->id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($converter->convert($metadata) as $id => $definition) {
|
||||
$container->setDefinition($id, $definition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getScanDirectories(ContainerBuilder $c)
|
||||
{
|
||||
$bundles = $this->kernel->getBundles();
|
||||
$scanBundles = $c->getParameter('jms_di_extra.bundles');
|
||||
$scanAllBundles = $c->getParameter('jms_di_extra.all_bundles');
|
||||
|
||||
$directories = $c->getParameter('jms_di_extra.directories');
|
||||
foreach ($bundles as $name => $bundle) {
|
||||
if (!$scanAllBundles && !in_array($name, $scanBundles, true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ('JMSDiExtraBundle' === $name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$directories[] = $bundle->getPath();
|
||||
}
|
||||
|
||||
return $directories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Only supports one namespaced class per file
|
||||
*
|
||||
* @throws \RuntimeException if the class name cannot be extracted
|
||||
* @param string $filename
|
||||
* @return string the fully qualified class name
|
||||
*/
|
||||
private function getClassName($filename)
|
||||
{
|
||||
$src = file_get_contents($filename);
|
||||
|
||||
if (!preg_match('/\bnamespace\s+([^;]+);/s', $src, $match)) {
|
||||
throw new RuntimeException(sprintf('Namespace could not be determined for file "%s".', $filename));
|
||||
}
|
||||
$namespace = $match[1];
|
||||
|
||||
if (!preg_match('/\bclass\s+([^\s]+)\s+(?:extends|implements|{)/s', $src, $match)) {
|
||||
throw new RuntimeException(sprintf('Could not extract class name from file "%s".', $filename));
|
||||
}
|
||||
|
||||
return $namespace.'\\'.$match[1];
|
||||
}
|
||||
}
|
55
vendor/jms/di-extra-bundle/JMS/DiExtraBundle/DependencyInjection/Compiler/IntegrationPass.php
vendored
Normal file
55
vendor/jms/di-extra-bundle/JMS/DiExtraBundle/DependencyInjection/Compiler/IntegrationPass.php
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace JMS\DiExtraBundle\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Alias;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
|
||||
/**
|
||||
* Integrates the bundle with external code.
|
||||
*
|
||||
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*/
|
||||
class IntegrationPass implements CompilerPassInterface
|
||||
{
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
// replace Symfony2's default controller resolver
|
||||
$container->setAlias('controller_resolver', new Alias('jms_di_extra.controller_resolver', false));
|
||||
|
||||
// replace SensioFrameworkExtraBundle's default template listener
|
||||
if ($container->hasDefinition('sensio_framework_extra.view.listener')) {
|
||||
$def = $container->getDefinition('sensio_framework_extra.view.listener');
|
||||
|
||||
// only overwrite if it has the default class otherwise the user has to do the integration manually
|
||||
if ('%sensio_framework_extra.view.listener.class%' === $def->getClass()) {
|
||||
$def->setClass('%jms_di_extra.template_listener.class%');
|
||||
}
|
||||
}
|
||||
|
||||
if ($container->hasDefinition('sensio_framework_extra.controller.listener')) {
|
||||
$def = $container->getDefinition('sensio_framework_extra.controller.listener');
|
||||
|
||||
if ('%sensio_framework_extra.controller.listener.class%' === $def->getClass()) {
|
||||
$def->setClass('%jms_di_extra.controller_listener.class%');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
namespace JMS\DiExtraBundle\DependencyInjection\Compiler;
|
||||
|
||||
use JMS\DiExtraBundle\Config\FastDirectoriesResource;
|
||||
|
||||
use Symfony\Component\Config\Resource\DirectoryResource;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
|
||||
class ResourceOptimizationPass implements CompilerPassInterface
|
||||
{
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
$resources = $directories = array();
|
||||
|
||||
$ref = new \ReflectionProperty('Symfony\Component\Config\Resource\DirectoryResource', 'pattern');
|
||||
$ref->setAccessible(true);
|
||||
|
||||
foreach ($container->getResources() as $resource) {
|
||||
if ($resource instanceof DirectoryResource) {
|
||||
if (null === $pattern = $ref->getValue($resource)) {
|
||||
$pattern = '*';
|
||||
}
|
||||
|
||||
$directories[$pattern][] = $resource->getResource();
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$resources[] = $resource;
|
||||
}
|
||||
|
||||
$sortFunc = function($a, $b) {
|
||||
return strlen($a) - strlen($b);
|
||||
};
|
||||
|
||||
foreach ($directories as $pattern => $pDirectories) {
|
||||
$newResources = array();
|
||||
|
||||
usort($pDirectories, $sortFunc);
|
||||
foreach ($pDirectories as $a) {
|
||||
foreach ($newResources as $b) {
|
||||
if (0 === strpos($a, $b)) {
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
|
||||
$newResources[] = $a;
|
||||
}
|
||||
|
||||
$directories[$pattern] = $newResources;
|
||||
}
|
||||
|
||||
foreach ($directories as $pattern => $pDirectories) {
|
||||
$newResource = new FastDirectoriesResource($pDirectories, $pattern);
|
||||
$newResource->update();
|
||||
$resources[] = $newResource;
|
||||
}
|
||||
|
||||
$ref = new \ReflectionProperty('Symfony\Component\DependencyInjection\ContainerBuilder', 'resources');
|
||||
$ref->setAccessible(true);
|
||||
$ref->setValue($container, $resources);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user