This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added backward compatability for Nette projects
- Loading branch information
1 parent
f3bc96b
commit 8aa89ea
Showing
4 changed files
with
129 additions
and
22 deletions.
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,114 @@ | ||
<?php | ||
|
||
/* | ||
* This code is under BSD 3-Clause "New" or "Revised" License. | ||
* | ||
* PHP version 7 and above required | ||
* | ||
* @category DependencyInjection | ||
* | ||
* @author Divine Niiquaye Ibok <divineibok@gmail.com> | ||
* @copyright 2019 Biurad Group (https://biurad.com/) | ||
* @license https://opensource.org/licenses/BSD-3-Clause License | ||
* | ||
* @link https://www.biurad.com/projects/dependencyinjection | ||
* @since Version 0.1 | ||
*/ | ||
|
||
namespace BiuradPHP\DependencyInjection\Concerns; | ||
|
||
use Composer\Autoload\ClassLoader; | ||
use InvalidArgumentException; | ||
use Nette\DI\CompilerExtension; | ||
use Nette\NotSupportedException; | ||
use Nette\Utils\Strings; | ||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
use ReflectionClass; | ||
use ReflectionObject; | ||
use RuntimeException; | ||
use SplFileInfo; | ||
|
||
class ImportsLocator | ||
{ | ||
/** | ||
* Returns the file path for a given compiler extension resource. | ||
* | ||
* A Resource can be a file or a directory. | ||
* | ||
* The resource name must follow the following pattern: | ||
* | ||
* "@CompilerExtension/path/to/a/file.something" | ||
* | ||
* where CompilerExtension is the name of the nette-di extension | ||
* and the remaining part is the relative path in the class. | ||
* | ||
* @param CompilerExtension $extension | ||
* @param string $name | ||
* @param bool $throw | ||
* | ||
* @return string The absolute path of the resource | ||
* | ||
* @throws InvalidArgumentException if the file cannot be found or the name is not valid | ||
* @throws RuntimeException if the name contains invalid/unsafe characters | ||
* @throws NotSupportedException if the $name doesn't match in $extension | ||
*/ | ||
public static function getLocation(CompilerExtension $extension, string $name, bool $throw = true) | ||
{ | ||
if ('@' !== $name[0] && true === $throw) { | ||
throw new InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name)); | ||
} | ||
|
||
if (false !== strpos($name, '..')) { | ||
throw new RuntimeException(sprintf('File name "%s" contains invalid characters (..).', $name)); | ||
} | ||
|
||
$path = ''; | ||
if (false !== strpos($bundleName = substr($name, 1), '/')) { | ||
[$bundleName, $path] = explode('/', $bundleName, 2); | ||
} | ||
|
||
if (false === strpos(get_class($extension), $bundleName)) { | ||
throw new NotSupportedException(sprintf('Resource path is not supported for %s', $bundleName)); | ||
} | ||
|
||
/** @var RecursiveIteratorIterator|SplFileInfo[] $iterator */ | ||
$iterator = new RecursiveIteratorIterator( | ||
new RecursiveDirectoryIterator($bundlePath = self::findComposerDirectory($extension)), | ||
RecursiveIteratorIterator::LEAVES_ONLY | ||
); | ||
|
||
foreach ($iterator as $file) { | ||
if (strlen($file->getPathname()) === strlen($bundlePath.$path) && file_exists($bundlePath.$path)) { | ||
return strtr($file->getPathname(), ['\\' => '/']); | ||
} | ||
} | ||
|
||
throw new InvalidArgumentException(sprintf('Unable to find file "%s".', $name)); | ||
} | ||
|
||
/** | ||
* @param CompilerExtension $extension | ||
* | ||
* @return string | ||
*/ | ||
private static function findComposerDirectory(CompilerExtension $extension): string | ||
{ | ||
$path = dirname((new ReflectionClass(ClassLoader::class))->getFileName()); | ||
$directory = dirname((new ReflectionObject($extension))->getFileName()); | ||
|
||
$packagist = json_decode(file_get_contents($path.'/installed.json'), true); | ||
|
||
foreach ($packagist as $package) { | ||
$packagePath = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, dirname($path, 1) .'/'. $package['name']); | ||
if (!Strings::startsWith($directory, $packagePath)) { | ||
continue; | ||
} | ||
$pathPrefix = current($package['autoload']['psr-4'] ?? $package['autoload']['psr-0'] ?? $package['autoload']['classmap']); | ||
|
||
return sprintf('%s/%s/', $packagePath, rtrim($pathPrefix, '/')); | ||
} | ||
|
||
return dirname($directory, 1).'/'; | ||
} | ||
} |
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