-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support files[] autoloading * cache files as container parameter * read files from legacy info file * check if parameter for file autoload exists
- Loading branch information
Showing
13 changed files
with
236 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Retrofit\Drupal\DependencyInjection\Compiler; | ||
|
||
use Drupal\Core\Extension\InfoParser; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
final class FilesAutoloaderPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
$filesAutoloadRegistry = []; | ||
$appRoot = $container->getParameter('app.root'); | ||
assert(is_string($appRoot) || is_null($appRoot)); | ||
$infoParser = new InfoParser($appRoot); | ||
/** @var array<string, array{pathname: string}> $container_modules */ | ||
$container_modules = $container->getParameter('container.modules'); | ||
$files = []; | ||
foreach ($container_modules as $name => $module) { | ||
$modulePath = dirname($module['pathname']); | ||
$info = $infoParser->parse($module['pathname']); | ||
$files[] = array_map( | ||
static fn(string $file) => "$modulePath/$file", | ||
$info['files'] ?? [] | ||
); | ||
|
||
$legacyInfoFilePath = "$modulePath/$name.info"; | ||
if (file_exists($legacyInfoFilePath)) { | ||
$legacyInfoFile = file_get_contents($legacyInfoFilePath); | ||
if ($legacyInfoFile !== false) { | ||
$legacyInfo = drupal_parse_info_format($legacyInfoFile); | ||
$files[] = array_map( | ||
static fn(string $file) => "$modulePath/$file", | ||
$legacyInfo['files'] ?? [] | ||
); | ||
} | ||
} | ||
} | ||
$files = array_unique(array_merge(...$files)); | ||
foreach ($files as $file) { | ||
$matches = []; | ||
$contents = file_get_contents($file); | ||
if ($contents === false) { | ||
continue; | ||
} | ||
$result = preg_match_all( | ||
'/^\s*(?:abstract|final)?\s*(class|interface|trait)\s+([a-zA-Z0-9_]+)/m', | ||
$contents, | ||
$matches | ||
); | ||
if ($result !== false) { | ||
foreach ($matches[2] as $itemName) { | ||
$filesAutoloadRegistry[$itemName] = $file; | ||
} | ||
} | ||
} | ||
$container->setParameter('files_autoload_registry', $filesAutoloadRegistry); | ||
} | ||
} |
This file contains 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 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 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,3 @@ | ||
name: node_example | ||
type: module | ||
core_version_requirement: '*' |
This file contains 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,5 @@ | ||
<?php | ||
|
||
class FooBar { | ||
|
||
} |
This file contains 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,5 @@ | ||
name = Node example | ||
description = Demonstrates a custom content type and uses the field api. | ||
package = Example modules | ||
core = 7.x | ||
files[] = foobar.inc |
This file contains 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 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,5 @@ | ||
<?php | ||
|
||
class SomeClass { | ||
|
||
} |
This file contains 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,3 @@ | ||
name: theming_example | ||
type: module | ||
core_version_requirement: '*' |
This file contains 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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Retrofit\Drupal\Tests\Integration; | ||
|
||
final class FileClassAutoloaderTest extends IntegrationTestCase | ||
{ | ||
protected static $modules = [ | ||
'system', | ||
]; | ||
|
||
protected static function getTestModules(): array | ||
{ | ||
return ['retrofit_fixtures']; | ||
} | ||
|
||
public function testAutoload(): void | ||
{ | ||
self::assertTrue(class_exists(\SomeClass::class)); | ||
self::assertTrue(class_exists(\FooBar::class)); | ||
} | ||
} |
This file contains 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