Skip to content

Commit

Permalink
adds support for generating relative file names
Browse files Browse the repository at this point in the history
  • Loading branch information
schmittjoh committed Sep 13, 2015
1 parent a2ffe12 commit 8d5b474
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions DependencyInjection/Compiler/PointcutMatchingPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use CG\Core\ClassUtils;
use CG\Core\DefaultNamingStrategy;
use CG\Generator\RelativePath;
use JMS\AopBundle\Exception\RuntimeException;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\DependencyInjection\Reference;
Expand Down Expand Up @@ -103,8 +104,8 @@ private function processDefinition(Definition $definition, $pointcuts, &$interce
return;
}

if ($file = $definition->getFile()) {
require_once $file;
if ($originalFilename = $definition->getFile()) {
require_once $originalFilename;
}

if (!class_exists($definition->getClass())) {
Expand Down Expand Up @@ -158,25 +159,52 @@ private function processDefinition(Definition $definition, $pointcuts, &$interce

$interceptors[ClassUtils::getUserClass($class->name)] = $classAdvices;

$proxyFilename = $this->cacheDir.'/'.str_replace('\\', '-', $class->name).'.php';

$generator = new InterceptionGenerator();
$generator->setFilter(function(\ReflectionMethod $method) use ($classAdvices) {
return isset($classAdvices[$method->name]);
});
if ($file) {
$generator->setRequiredFile($file);

if ($originalFilename) {
$relativeOriginalFilename = $this->relativizePath($proxyFilename, $originalFilename);
if ($relativeOriginalFilename[0] === '.') {
$generator->setRequiredFile(new RelativePath($relativeOriginalFilename));
} else {
$generator->setRequiredFile($relativeOriginalFilename);
}
}
$enhancer = new Enhancer($class, array(), array(
$generator
));
$enhancer->setNamingStrategy(new DefaultNamingStrategy('EnhancedProxy'.substr(md5($this->container->getParameter('jms_aop.cache_dir')), 0, 8)));
$enhancer->writeClass($filename = $this->cacheDir.'/'.str_replace('\\', '-', $class->name).'.php');
$definition->setFile($filename);
$enhancer->writeClass($proxyFilename);
$definition->setFile($proxyFilename);
$definition->setClass($enhancer->getClassName($class));
$definition->addMethodCall('__CGInterception__setLoader', array(
new Reference('jms_aop.interceptor_loader')
));
}

private function relativizePath($targetPath, $path)
{
$commonPath = dirname($targetPath);

$level = 0;
while ( ! empty($commonPath)) {
if (0 === strpos($path, $commonPath)) {
$relativePath = str_repeat('../', $level).substr($path, strlen($commonPath) + 1);

return $relativePath;
}

$commonPath = dirname($commonPath);
$level += 1;
}

return $path;
}

private function addResources(\ReflectionClass $class)
{
do {
Expand Down

0 comments on commit 8d5b474

Please sign in to comment.