123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace Fgm\ComposerCheck;
- class LoaderFactory {
-
- protected $directory;
-
- public function __construct(string $directory) {
- $validatedDirectory = self::validateDirectory($directory);
- if (empty($validatedDirectory)) {
- throw new \InvalidArgumentException("Unusable directory: $directory");
- }
- $this->directory = $validatedDirectory;
- }
-
- public static function validateDirectory(string $directory) {
- $path = realpath($directory);
-
- if (empty($path) || !is_dir($path) || !is_readable($path)) {
- return FALSE;
- }
- foreach (['json', 'lock'] as $kind) {
- $filePath = realpath("${path}/composer.${kind}");
- if (empty($filePath) || !is_file($filePath) || !is_readable($filePath)) {
- return FALSE;
- }
- }
- return $path;
- }
-
- public function createLoader(string $type) {
- $className = __NAMESPACE__ . '\\' . ucfirst($type) . 'Loader';
- if (!class_exists($className)) {
- throw new \InvalidArgumentException("Unknown loader type: $type.");
- }
-
- $loader = new $className($this->directory);
- return $loader;
- }
- }
|