Skip to content

Commit

Permalink
ProxyGenerator: do not generate long file names
Browse files Browse the repository at this point in the history
  • Loading branch information
JanTvrdik committed Aug 9, 2023
1 parent 0a357f1 commit 8136e31
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 42 deletions.
8 changes: 4 additions & 4 deletions src/Proxy/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Autoloader
* Resolves proxy class name to a filename based on the following pattern.
*
* 1. Remove Proxy namespace from class name.
* 2. Remove namespace separators from remaining class name.
* 2. Replace namespace separators with directly separators in remaining class name.
* 3. Return PHP filename from proxy-dir with the result from 2.
*
* @param string $proxyDir
Expand All @@ -47,10 +47,10 @@ public static function resolveFile($proxyDir, $proxyNamespace, $className)
}

// remove proxy namespace from class name
$classNameRelativeToProxyNamespace = substr($className, strlen($proxyNamespace));
$classNameRelativeToProxyNamespace = substr($className, strlen($proxyNamespace) + 1);

// remove namespace separators from remaining class name
$fileName = str_replace('\\', '', $classNameRelativeToProxyNamespace);
// replace namespace separators in remaining class name
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $classNameRelativeToProxyNamespace);

return $proxyDir . DIRECTORY_SEPARATOR . $fileName . '.php';
}
Expand Down
4 changes: 2 additions & 2 deletions src/Proxy/ProxyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,8 @@ public function getProxyFileName($className, $baseDirectory = null)
{
$baseDirectory = $baseDirectory ?: $this->proxyDirectory;

return rtrim($baseDirectory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . Proxy::MARKER
. str_replace('\\', '', $className) . '.php';
return rtrim($baseDirectory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . Proxy::MARKER . DIRECTORY_SEPARATOR
. str_replace('\\', DIRECTORY_SEPARATOR, $className) . '.php';
}

/**
Expand Down
10 changes: 6 additions & 4 deletions tests/Common/Proxy/AutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ class AutoloaderTest extends TestCase
{
public static function dataResolveFile()
{
$sep = DIRECTORY_SEPARATOR;

return [
['/tmp', 'MyProxy', 'MyProxy\RealClass', '/tmp' . DIRECTORY_SEPARATOR . 'RealClass.php'],
['/tmp', 'MyProxy', 'MyProxy\__CG__\RealClass', '/tmp' . DIRECTORY_SEPARATOR . '__CG__RealClass.php'],
['/tmp', 'MyProxy\Subdir', 'MyProxy\Subdir\__CG__\RealClass', '/tmp' . DIRECTORY_SEPARATOR . '__CG__RealClass.php'],
['/tmp', 'MyProxy', 'MyProxy\__CG__\Other\RealClass', '/tmp' . DIRECTORY_SEPARATOR . '__CG__OtherRealClass.php'],
['/tmp', 'MyProxy', 'MyProxy\RealClass', "/tmp{$sep}RealClass.php"],
['/tmp', 'MyProxy', 'MyProxy\__CG__\RealClass', "/tmp{$sep}__CG__{$sep}RealClass.php"],
['/tmp', 'MyProxy\Subdir', 'MyProxy\Subdir\__CG__\RealClass', "/tmp{$sep}__CG__{$sep}RealClass.php"],
['/tmp', 'MyProxy', 'MyProxy\__CG__\Other\RealClass', "/tmp{$sep}__CG__{$sep}Other{$sep}RealClass.php"],
];
}

Expand Down
64 changes: 32 additions & 32 deletions tests/Common/Proxy/ProxyGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function testClassWithSleepProxyGeneration()
$this->generateAndRequire($proxyGenerator, $metadata);
}

$classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxySleepClass.php');
$classCode = file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/SleepClass.php');
self::assertEquals(1, substr_count($classCode, 'function __sleep'));
self::assertEquals(1, substr_count($classCode, 'parent::__sleep()'));
}
Expand All @@ -119,7 +119,7 @@ public function testClassWithStaticPropertyProxyGeneration()
$this->generateAndRequire($proxyGenerator, $metadata);
}

$classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyStaticPropertyClass.php');
$classCode = file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/StaticPropertyClass.php');
self::assertEquals(1, substr_count($classCode, 'function __sleep'));
self::assertStringNotContainsString('protectedStaticProperty', $classCode);
}
Expand All @@ -141,7 +141,7 @@ public function testClassWithCallableTypeHintOnProxiedMethod()
$this->generateAndRequire($proxyGenerator, $metadata);
}

$classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyCallableTypeHintClass.php');
$classCode = file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/CallableTypeHintClass.php');

self::assertEquals(1, substr_count($classCode, 'call(callable $foo)'));
}
Expand All @@ -156,7 +156,7 @@ public function testClassWithVariadicArgumentOnProxiedMethod()
$this->generateAndRequire($proxyGenerator, $metadata);
}

$classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyVariadicTypeHintClass.php');
$classCode = file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/VariadicTypeHintClass.php');

self::assertEquals(1, substr_count($classCode, 'function addType(...$types)'));
self::assertEquals(1, substr_count($classCode, '__invoke($this, \'addType\', [$types])'));
Expand All @@ -173,7 +173,7 @@ public function testClassWithScalarTypeHintsOnProxiedMethods()
$this->generateAndRequire($proxyGenerator, $metadata);
}

$classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyScalarTypeHintsClass.php');
$classCode = file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/ScalarTypeHintsClass.php');

self::assertEquals(1, substr_count($classCode, 'function singleTypeHint(string $param)'));
self::assertEquals(1, substr_count($classCode, 'function multipleTypeHints(int $a, float $b, bool $c, string $d)'));
Expand All @@ -193,7 +193,7 @@ public function testClassWithReturnTypesOnProxiedMethods()
$this->generateAndRequire($proxyGenerator, $metadata);
}

$classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyReturnTypesClass.php');
$classCode = file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/ReturnTypesClass.php');

self::assertEquals(1, substr_count($classCode, 'function returnsClass(): \stdClass'));
self::assertEquals(1, substr_count($classCode, 'function returnsScalar(): int'));
Expand All @@ -214,7 +214,7 @@ public function testClassWithNullableTypeHintsOnProxiedMethods()
$this->generateAndRequire($proxyGenerator, $metadata);
}

$classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyNullableTypeHintsClass.php');
$classCode = file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/NullableTypeHintsClass.php');

self::assertEquals(1, substr_count($classCode, 'function nullableTypeHintInt(?int $param)'));
self::assertEquals(1, substr_count($classCode, 'function nullableTypeHintObject(?\stdClass $param)'));
Expand All @@ -234,7 +234,7 @@ public function testClassWithNullableReturnTypesOnProxiedMethods()
$this->generateAndRequire($proxyGenerator, $metadata);
}

$classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyNullableTypeHintsClass.php');
$classCode = file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/NullableTypeHintsClass.php');

self::assertEquals(1, substr_count($classCode, 'function returnsNullableInt(): ?int'));
self::assertEquals(1, substr_count($classCode, 'function returnsNullableObject(): ?\stdClass'));
Expand All @@ -260,12 +260,12 @@ public function testClassWithNullableOptionalNonLastParameterOnProxiedMethods()

self::assertStringContainsString(
'public function midSignatureNullableParameter(\stdClass $param = NULL, $secondParam)',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyNullableNonOptionalHintClass.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/NullableNonOptionalHintClass.php')
);

self::assertStringContainsString(
'public function midSignatureNotNullableHintedParameter(string $param = \'foo\', $secondParam)',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyNullableNonOptionalHintClass.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/NullableNonOptionalHintClass.php')
);
}

Expand All @@ -288,13 +288,13 @@ public function testClassWithPhp71NullableOptionalNonLastParameterOnProxiedMetho

self::assertStringContainsString(
'public function midSignatureNullableParameter(string $param = NULL, $secondParam)',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPhp71NullableDefaultedNonOptionalHintClass.php'),
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/Php71NullableDefaultedNonOptionalHintClass.php'),
'Signature allows nullable type, although explicit "?" marker isn\'t used in the proxy'
);

self::assertStringContainsString(
'public function midSignatureNotNullableHintedParameter(?string $param = \'foo\', $secondParam)',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPhp71NullableDefaultedNonOptionalHintClass.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/Php71NullableDefaultedNonOptionalHintClass.php')
);
}

Expand All @@ -308,7 +308,7 @@ public function testClassWithVoidReturnType()
$this->generateAndRequire($proxyGenerator, $metadata);
}

$classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyVoidReturnTypeClass.php');
$classCode = file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/VoidReturnTypeClass.php');

self::assertEquals(1, substr_count($classCode, 'function returnsVoid(): void'));
}
Expand All @@ -323,7 +323,7 @@ public function testClassWithIterableTypeHint()
$this->generateAndRequire($proxyGenerator, $metadata);
}

$classCode = file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyIterableTypeHintClass.php');
$classCode = file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/IterableTypeHintClass.php');

self::assertEquals(1, substr_count($classCode, 'function parameterType(iterable $param)'));
self::assertEquals(1, substr_count($classCode, 'function returnType(): iterable'));
Expand Down Expand Up @@ -433,7 +433,7 @@ public function testPhp8CloneWithVoidReturnType()

self::assertStringContainsString(
'public function __clone(): void',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPhp8MagicCloneClass.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/Php8MagicCloneClass.php')
);
}

Expand All @@ -453,12 +453,12 @@ public function testPhp8UnionTypes()

self::assertStringContainsString(
'setValue(\stdClass|array $value): float|bool',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPhp8UnionTypes.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/Php8UnionTypes.php')
);

self::assertStringContainsString(
'setNullableValue(\stdClass|array|null $value): float|bool|null',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPhp8UnionTypes.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/Php8UnionTypes.php')
);
}

Expand All @@ -478,7 +478,7 @@ public function testPhp8MixedType()

self::assertStringContainsString(
'foo(mixed $bar): mixed',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPhp8MixedType.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/Php8MixedType.php')
);
}

Expand All @@ -498,12 +498,12 @@ public function testPhp8StaticType()

self::assertStringContainsString(
'foo(mixed $bar): static',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPhp8StaticType.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/Php8StaticType.php')
);

self::assertStringContainsString(
'fooNull(mixed $bar): ?static',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPhp8StaticType.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/Php8StaticType.php')
);
}

Expand All @@ -523,7 +523,7 @@ public function testPhp81IntersectionType()

self::assertStringContainsString(
'setFoo(\Traversable&\Countable $foo): \Traversable&\Countable',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81IntersectionTypes.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/PHP81IntersectionTypes.php')
);
}

Expand All @@ -543,17 +543,17 @@ public function testPhp81NeverType()

self::assertStringContainsString(
'__get($name): never',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NeverType.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/PHP81NeverType.php')
);

self::assertStringContainsString(
'__set($name, $value): never',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NeverType.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/PHP81NeverType.php')
);

self::assertStringContainsString(
'finishHim(): never',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NeverType.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/PHP81NeverType.php')
);
}

Expand All @@ -575,7 +575,7 @@ public function testEnumDefaultInPublicProperty() : void

$this->assertStringContainsString(
'use Doctrine;',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPhp81EnumPublicPropertyType.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/Php81EnumPublicPropertyType.php')
);

$object = new \Doctrine\Tests\Common\ProxyProxy\__CG__\Doctrine\Tests\Common\Proxy\Php81EnumPublicPropertyType();
Expand All @@ -600,37 +600,37 @@ public function testPhp81NewInInitializers()

self::assertStringContainsString(
'onlyInitializer($foo = new \stdClass()): void',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/PHP81NewInInitializers.php')
);

self::assertStringContainsString(
'typed(\DateTimeInterface $foo = new \DateTimeImmutable(\'now\')): void',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/PHP81NewInInitializers.php')
);

self::assertStringContainsString(
'arrayInDefault(array $foo = [new \DateTimeImmutable(\'2022-08-22 16:20\', new \DateTimeZone(\'Europe/Warsaw\'))]): void',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/PHP81NewInInitializers.php')
);

self::assertStringContainsString(
'scalarConstInDefault(string $foo = \'foo\'): void',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/PHP81NewInInitializers.php')
);

self::assertStringContainsString(
'constInDefault(array $foo = \Doctrine\Tests\Common\Util\TestAsset\ConstProvider::FOO): void',
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/PHP81NewInInitializers.php')
);

self::assertStringContainsString(
"globalEolInDefault(string \$foo = '\n'): void",
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/PHP81NewInInitializers.php')
);

self::assertStringContainsString(
"specialCharacterInDefault(string \$foo = '\n'): void",
file_get_contents(__DIR__ . '/generated/__CG__DoctrineTestsCommonProxyPHP81NewInInitializers.php')
file_get_contents(__DIR__ . '/generated/__CG__/Doctrine/Tests/Common/Proxy/PHP81NewInInitializers.php')
);
}

Expand Down

0 comments on commit 8136e31

Please sign in to comment.