PHP code example of tourze / bundle-dependency

1. Go to this page and download the library: Download tourze/bundle-dependency library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tourze / bundle-dependency example snippets




use Tourze\BundleDependency\BundleDependencyInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class YourBundle extends Bundle implements BundleDependencyInterface
{
    public static function getBundleDependencies(): array
    {
        return [
            // Load in all environments
            'Vendor\RequiredBundle\RequiredBundle' => ['all' => true],
            
            // Load only in dev and test environments
            'Vendor\DebugBundle\DebugBundle' => ['dev' => true, 'test' => true],
            
            // Load only in production
            'Vendor\OptimizedBundle\OptimizedBundle' => ['prod' => true],
        ];
    }
}



use Tourze\BundleDependency\ResolveHelper;

// Resolve all dependencies for a set of bundles
$bundles = [
    'App\YourBundle\YourBundle' => ['all' => true],
];

foreach (ResolveHelper::resolveBundleDependencies($bundles) as $bundle => $environments) {
    // $bundle = 'Vendor\RequiredBundle\RequiredBundle'
    // $environments = ['all' => true]
}

// Or resolve by bundle name
foreach (ResolveHelper::resolveByBundleName('YourBundle') as $bundleName) {
    // Returns simplified bundle names
}



use Symfony\Component\HttpKernel\Kernel;
use Tourze\BundleDependency\ResolveHelper;

class AppKernel extends Kernel
{
    public function registerBundles(): iterable
    {
        $bundles = [
            'App\CoreBundle\CoreBundle' => ['all' => true],
            'App\ApiBundle\ApiBundle' => ['all' => true],
        ];

        // Automatically resolve and register all dependencies
        foreach (ResolveHelper::resolveBundleDependencies($bundles) as $bundle => $envs) {
            if (isset($envs['all']) || isset($envs[$this->environment])) {
                yield new $bundle();
            }
        }
    }
}

// BundleA depends on BundleB
// BundleB depends on BundleA
// No exception thrown, both bundles are resolved once

interface BundleDependencyInterface
{
    /**
     * Get bundle dependencies with their environment configuration
     *
     * @return array<class-string, array<string, bool>>
     */
    public static function getBundleDependencies(): array;
}

class ResolveHelper
{
    /**
     * Recursively resolve bundle dependencies
     *
     * @param array<class-string, array<string, bool>> $bundles Initial bundles
     * @return \Traversable<class-string, array<string, bool>> Resolved bundles
     */
    public static function resolveBundleDependencies(array $bundles): \Traversable;

    /**
     * Resolve dependencies by bundle name
     *
     * @param string $bundleName Bundle name (e.g., 'YourBundle')
     * @return \Traversable<string> Simplified bundle names
     */
    public static function resolveByBundleName(string $bundleName): \Traversable;
}
bash
# Run tests
./vendor/bin/phpunit packages/bundle-dependency/tests

# Run static analysis
php -d memory_limit=2G ./vendor/bin/phpstan analyse packages/bundle-dependency