Skip to content

Commit

Permalink
Add symfony
Browse files Browse the repository at this point in the history
  • Loading branch information
saeedvaziry committed Dec 25, 2024
1 parent fea8502 commit 6a50af1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use TweakPHP\Client\Loaders\ComposerLoader;
use TweakPHP\Client\Loaders\LaravelLoader;
use TweakPHP\Client\Loaders\LoaderInterface;
use TweakPHP\Client\Loaders\SymfonyLoader;
use TweakPHP\Client\Loaders\WordPressLoader;

class Loader
Expand All @@ -19,14 +20,18 @@ public static function load(string $path)
return new LaravelLoader($path);
}

if (file_exists($path . '/vendor/autoload.php')) {
return new ComposerLoader($path);
if (file_exists($path . '/vendor/autoload.php') && file_exists($path . '/symfony.lock') && file_exists($path . '/src/Kernel.php')) {
return new SymfonyLoader($path);
}

if (file_exists($path . '/wp-load.php')) {
return new WordPressLoader($path);
}

if (file_exists($path . '/vendor/autoload.php')) {
return new ComposerLoader($path);
}

return null;
}
}
60 changes: 60 additions & 0 deletions src/Loaders/SymfonyLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace TweakPHP\Client\Loaders;

class SymfonyLoader extends ComposerLoader
{
private $kernel;

/**
* @param string $path
*/
public function __construct(string $path)
{
parent::__construct($path);

// Include the Composer autoloader
require_once $path . '/vendor/autoload.php';

// Initialize the Symfony Kernel
$env = $_SERVER['APP_ENV'] ?? 'dev';
$debug = ($_SERVER['APP_DEBUG'] ?? '1') === '1';

$kernelClass = $this->findKernelClass($path);
$this->kernel = new $kernelClass($env, $debug);
$this->kernel->boot();
}

/**
* Find the application's Kernel class dynamically.
*
* @param string $path
* @return string
*/
private function findKernelClass(string $path): string
{
$kernelFile = $path . '/src/Kernel.php';
if (!file_exists($kernelFile)) {
throw new \RuntimeException('Kernel.php file not found in the src directory.');
}

require_once $kernelFile;
return 'App\\Kernel';
}

/**
* @return string
*/
public function name(): string
{
return 'Symfony';
}

/**
* @return string
*/
public function version(): string
{
return \Symfony\Component\HttpKernel\Kernel::VERSION;
}
}

0 comments on commit 6a50af1

Please sign in to comment.