Neos\Flow\Package\PackageFactory::detectFlowPackageFilePath PHP Method

detectFlowPackageFilePath() public method

Detects if the package contains a package file and returns the path and classname.
public detectFlowPackageFilePath ( string $packageKey, string $absolutePackagePath ) : array
$packageKey string The package key
$absolutePackagePath string Absolute path to the package
return array The path to the package file and classname for this package or an empty array if none was found.
    public function detectFlowPackageFilePath($packageKey, $absolutePackagePath)
    {
        if (!is_dir($absolutePackagePath)) {
            throw new Exception\InvalidPackagePathException(sprintf('The given package path "%s" is not a readable directory.', $absolutePackagePath), 1445904440);
        }
        $composerManifest = ComposerUtility::getComposerManifest($absolutePackagePath);
        if (!ComposerUtility::isFlowPackageType(isset($composerManifest['type']) ? $composerManifest['type'] : '')) {
            return [];
        }
        $possiblePackageClassPaths = [Files::concatenatePaths(['Classes', 'Package.php']), Files::concatenatePaths(['Classes', str_replace('.', '/', $packageKey), 'Package.php'])];
        $foundPackageClassPaths = array_filter($possiblePackageClassPaths, function ($packageClassPathAndFilename) use($absolutePackagePath) {
            $absolutePackageClassPath = Files::concatenatePaths([$absolutePackagePath, $packageClassPathAndFilename]);
            return is_file($absolutePackageClassPath);
        });
        if ($foundPackageClassPaths === []) {
            return [];
        }
        if (count($foundPackageClassPaths) > 1) {
            throw new Exception\CorruptPackageException(sprintf('The package "%s" contains multiple possible "Package.php" files. Please make sure that only one "Package.php" exists in the autoload root(s) of your Flow package.', $packageKey), 1457454840);
        }
        $packageClassPathAndFilename = reset($foundPackageClassPaths);
        $absolutePackageClassPath = Files::concatenatePaths([$absolutePackagePath, $packageClassPathAndFilename]);
        $packageClassContents = file_get_contents($absolutePackageClassPath);
        $packageClassName = (new PhpAnalyzer($packageClassContents))->extractFullyQualifiedClassName();
        if ($packageClassName === null) {
            throw new Exception\CorruptPackageException(sprintf('The package "%s" does not contain a valid package class. Check if the file "%s" really contains a class.', $packageKey, $packageClassPathAndFilename), 1327587091);
        }
        return ['className' => $packageClassName, 'pathAndFilename' => $packageClassPathAndFilename];
    }

Usage Example

 /**
  * @param string $packageKey
  * @param string $packagePath
  * @param array $composerManifest
  * @param string $state
  * @return array
  */
 protected function preparePackageStateConfiguration($packageKey, $packagePath, $composerManifest, $state = self::PACKAGE_STATE_ACTIVE)
 {
     $autoload = isset($composerManifest['autoload']) ? $composerManifest['autoload'] : [];
     return ['state' => $state, 'packageKey' => $packageKey, 'packagePath' => str_replace($this->packagesBasePath, '', $packagePath), 'composerName' => $composerManifest['name'], 'autoloadConfiguration' => $autoload, 'packageClassInformation' => $this->packageFactory->detectFlowPackageFilePath($packageKey, $packagePath)];
 }