-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Labels
enhancementNew feature or requestNew feature or request
Description
Improved Legacy Bootstrap for Laravel Integration
To address the concerns and improve the bootstrap process, we can:
- Make the container globally accessible.
- Rename the container variable to reflect its purpose.
- Provide a convenient method for retrieving the cache manager with the appropriate store.
Steps
-
Global Access to Container:
- Use a singleton pattern to make the container accessible from anywhere in the application.
-
Rename the Container:
- Use a more descriptive name, such as
LegacyLaravelApp.
- Use a more descriptive name, such as
-
Convenient Cache Manager Access:
- Provide a method to get the cache manager, defaulting to the desired store (e.g.,
redis).
- Provide a method to get the cache manager, defaulting to the desired store (e.g.,
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.
-
Accessing the Container:
$container = LegacyLaravelApp()->getContainer();
-
Accessing the Default Cache Manager:
$cacheManager = LegacyLaravelApp()->getCache();
-
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
-
Singleton Pattern:
- Ensures that the container is only instantiated once and is accessible globally.
LegacyLaravelApp::getInstance()provides the instance of the container.
-
Global Helper Function:
- The
LegacyLaravelApp()function makes it easy to access the singleton instance globally.
- The
-
Descriptive Naming:
LegacyLaravelAppreflects its purpose as a lightweight Laravel integration for a legacy application.
-
Convenient Cache Access:
getCache()method simplifies access to the cache manager, defaulting to theredisstore 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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request