Skip to content

Improved Legacy Bootstrap for Laravel Integration #17

@waynetheisinger

Description

@waynetheisinger

Improved Legacy Bootstrap for Laravel Integration

To address the concerns and improve the bootstrap process, we can:

  1. Make the container globally accessible.
  2. Rename the container variable to reflect its purpose.
  3. Provide a convenient method for retrieving the cache manager with the appropriate store.

Steps

  1. Global Access to Container:

    • Use a singleton pattern to make the container accessible from anywhere in the application.
  2. Rename the Container:

    • Use a more descriptive name, such as LegacyLaravelApp.
  3. Convenient Cache Manager Access:

    • Provide a method to get the cache manager, defaulting to the desired store (e.g., redis).

Implementation

Bootstrap File (php/bootstrap/legacy/laravel.php)

<?php
// bootstrap/legacy/laravel.php
require_once __DIR__ . '/../../vendor/autoload.php';

use Illuminate\Cache\CacheManager;
use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Config\Repository as ConfigRepository;
use Illuminate\Redis\RedisManager;

class LegacyLaravelApp
{
    private static $instance = null;
    private $container;

    private function __construct()
    {
        $this->container = new Container;

        // Set up the event dispatcher
        $events = new Dispatcher($this->container);
        $this->container->instance('events', $events);

        // Set up the configuration
        $config = new ConfigRepository([
            'app' => require __DIR__ . '/../../config/app.php',
            'cache' => require __DIR__ . '/../../config/legacy/cache.php',
            'database' => require __DIR__ . '/../../config/legacy/database.php',
        ]);
        $this->container->instance('config', $config);

        $files = new Filesystem;
        $this->container->instance('files', $files);

        // Set up the Redis manager
        $redisConfig = $config->get('database.redis');
        $redisManager = new RedisManager($this->container, $redisConfig['client'], $redisConfig);
        $this->container->instance('redis', $redisManager);

        // Set up the Cache manager
        $cacheManager = new CacheManager($this->container);
        $this->container->instance('cache', $cacheManager);
    }

    public static function getInstance()
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    public function getContainer()
    {
        return $this->container;
    }

    public function getCache($store = 'redis')
    {
        return $this->container->make('cache')->store($store);
    }
}

// Make the instance globally accessible
function LegacyLaravelApp()
{
    return LegacyLaravelApp::getInstance();
}

return LegacyLaravelApp();

Usage in Legacy Application

You can now use the LegacyLaravelApp globally in your legacy application without requiring the bootstrap file multiple times.

  1. Accessing the Container:

    $container = LegacyLaravelApp()->getContainer();
  2. Accessing the Default Cache Manager:

    $cacheManager = LegacyLaravelApp()->getCache();
  3. Accessing a Specific Cache Store:

    $redisCacheManager = LegacyLaravelApp()->getCache('redis');

Example Usage in Legacy Code

// Example usage in some part of the legacy application
$cacheManager = LegacyLaravelApp()->getCache();
$redisCacheManager = LegacyLaravelApp()->getCache('redis');

// Using the container for other purposes
$container = LegacyLaravelApp()->getContainer();

Explanation

  1. Singleton Pattern:

    • Ensures that the container is only instantiated once and is accessible globally.
    • LegacyLaravelApp::getInstance() provides the instance of the container.
  2. Global Helper Function:

    • The LegacyLaravelApp() function makes it easy to access the singleton instance globally.
  3. Descriptive Naming:

    • LegacyLaravelApp reflects its purpose as a lightweight Laravel integration for a legacy application.
  4. Convenient Cache Access:

    • getCache() method simplifies access to the cache manager, defaulting to the redis store but allowing flexibility.

By following this structure, we ensure that the legacy application can seamlessly integrate Laravel's features while maintaining clean and maintainable code.

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions