- Created separate MoneyFlix project directory - Copied Node.js redirector from production server - Added CRM PHP code (Redirects module, Core classes, Config, Routes, Views) - Created comprehensive documentation (README_MONEYFLIX.md) - Added sync script to keep CRM code updated (sync-from-crm.sh) - Configured .gitignore for Node.js and PHP - Set up proper project structure for multi-client isolation This separates MoneyFlix from the main CRM to avoid confusion between different client systems and provides a dedicated environment for MoneyFlix-specific development and maintenance.
159 lines
4.3 KiB
PHP
159 lines
4.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Core;
|
|
|
|
/**
|
|
* Service Container para Dependency Injection
|
|
* Permite isolamento de módulos e injeção de dependências
|
|
*/
|
|
class ServiceContainer
|
|
{
|
|
private static ?ServiceContainer $instance = null;
|
|
private array $services = [];
|
|
private array $singletons = [];
|
|
private array $moduleServices = [];
|
|
|
|
private function __construct() {}
|
|
|
|
public static function getInstance(): ServiceContainer
|
|
{
|
|
if (self::$instance === null) {
|
|
self::$instance = new self();
|
|
}
|
|
return self::$instance;
|
|
}
|
|
|
|
/**
|
|
* Registar um serviço
|
|
*/
|
|
public function register(string $name, callable $factory, bool $singleton = false): void
|
|
{
|
|
$this->services[$name] = [
|
|
'factory' => $factory,
|
|
'singleton' => $singleton
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Registar um serviço específico de um módulo
|
|
*/
|
|
public function registerModuleService(string $module, string $name, callable $factory, bool $singleton = false): void
|
|
{
|
|
$this->moduleServices[$module][$name] = [
|
|
'factory' => $factory,
|
|
'singleton' => $singleton
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Resolver um serviço
|
|
*/
|
|
public function resolve(string $name)
|
|
{
|
|
// Verificar se é singleton já instanciado
|
|
if (isset($this->singletons[$name])) {
|
|
return $this->singletons[$name];
|
|
}
|
|
|
|
// Verificar serviços globais
|
|
if (isset($this->services[$name])) {
|
|
$service = $this->services[$name];
|
|
$instance = $service['factory']();
|
|
|
|
if ($service['singleton']) {
|
|
$this->singletons[$name] = $instance;
|
|
}
|
|
|
|
return $instance;
|
|
}
|
|
|
|
// Verificar se é um serviço de módulo no formato "Module:ServiceName"
|
|
if (strpos($name, ':') !== false) {
|
|
[$module, $serviceName] = explode(':', $name, 2);
|
|
if (isset($this->moduleServices[$module][$serviceName])) {
|
|
$service = $this->moduleServices[$module][$serviceName];
|
|
$instance = $service['factory']();
|
|
|
|
if ($service['singleton']) {
|
|
$this->singletons[$name] = $instance;
|
|
}
|
|
|
|
return $instance;
|
|
}
|
|
}
|
|
|
|
// Verificar serviços de módulos (formato antigo)
|
|
foreach ($this->moduleServices as $module => $services) {
|
|
if (isset($services[$name])) {
|
|
$service = $services[$name];
|
|
$instance = $service['factory']();
|
|
|
|
if ($service['singleton']) {
|
|
$this->singletons[$name] = $instance;
|
|
}
|
|
|
|
return $instance;
|
|
}
|
|
}
|
|
|
|
throw new \InvalidArgumentException("Service '$name' not found");
|
|
}
|
|
|
|
/**
|
|
* Verificar se um serviço existe
|
|
*/
|
|
public function has(string $name): bool
|
|
{
|
|
if (isset($this->services[$name])) {
|
|
return true;
|
|
}
|
|
|
|
// Verificar se é um serviço de módulo no formato "Module:ServiceName"
|
|
if (strpos($name, ':') !== false) {
|
|
[$module, $serviceName] = explode(':', $name, 2);
|
|
if (isset($this->moduleServices[$module][$serviceName])) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
// Verificar serviços de módulos (formato antigo)
|
|
foreach ($this->moduleServices as $services) {
|
|
if (isset($services[$name])) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Limpar todos os serviços (útil para testes)
|
|
*/
|
|
public function clear(): void
|
|
{
|
|
$this->services = [];
|
|
$this->singletons = [];
|
|
$this->moduleServices = [];
|
|
}
|
|
|
|
/**
|
|
* Obter lista de serviços registados
|
|
*/
|
|
public function getRegisteredServices(): array
|
|
{
|
|
$services = [];
|
|
|
|
foreach ($this->services as $name => $service) {
|
|
$services[] = $name;
|
|
}
|
|
|
|
foreach ($this->moduleServices as $module => $moduleServices) {
|
|
foreach ($moduleServices as $name => $service) {
|
|
$services[] = "$module:$name";
|
|
}
|
|
}
|
|
|
|
return $services;
|
|
}
|
|
}
|