Last active
February 9, 2021 08:40
-
-
Save pchelk1n/643e7d20815afee396efe8ad367092f1 to your computer and use it in GitHub Desktop.
Kohana, inject services in controller action with Symfony DependencyInjection Component
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// at the end, or move to some file | |
require __DIR__.'/../vendor/autoload.php'; | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
use Symfony\Component\DependencyInjection\Reference; | |
$containerBuilder = new ContainerBuilder(); | |
// example your classes | |
$containerBuilder->register(Test2::class, Test2::class); | |
$containerBuilder->register(Test::class, Test::class) | |
->addArgument(new Reference(Test2::class)) | |
Kohana::setContainer($containerBuilder); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"symfony/dependency-injection": "^4.3" | |
}, | |
"autoload": { | |
"psr-4": {"App\\": "src/"} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Kohana_Request_Client_Internal extends Request_Client | |
{ | |
public function execute_request(Request $request) | |
{ | |
// replace | |
// $method->invoke($controller); | |
$method->invoke($controller, ...$this->getActionServices($method)); | |
// .... | |
} | |
private function getActionServices(ReflectionMethod $method): array | |
{ | |
$services = []; | |
$container = Kohana::getContainer(); | |
$methodParameters = $method->getParameters(); | |
foreach ($methodParameters as $methodParameter) { | |
$parameterClassName = $methodParameter->getClass()->getName(); | |
if (!$container->has($parameterClassName)) { | |
continue; | |
} | |
$services[] = $container->get($parameterClassName); | |
} | |
return $services; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// applicaton/classes | |
use Symfony\Component\DependencyInjection\ContainerBuilder; | |
class Kohana extends Kohana_Core | |
{ | |
private static $container; | |
public static function setContainer(ContainerBuilder $container): void | |
{ | |
self::$container = $container; | |
} | |
public static function getContainer(): ContainerBuilder | |
{ | |
return self::$container; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
class Test | |
{ | |
private $test2; | |
public function __construct(Test2 $test2) | |
{ | |
$this->test2 = $test2; | |
} | |
public function do(): string | |
{ | |
return 'testing:'. $this->test2->do(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Test; | |
class Controller_Test extends Controller | |
{ | |
public function action_test(Test $test) | |
{ | |
echo $test->do(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
class Test2 | |
{ | |
public function do(): string | |
{ | |
return 'test 2'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment