Skip to content

Instantly share code, notes, and snippets.

@lyrixx
Last active July 18, 2024 09:51
Show Gist options
  • Save lyrixx/2ea147609dc632d26e366aef60f61a9f to your computer and use it in GitHub Desktop.
Save lyrixx/2ea147609dc632d26e366aef60f61a9f to your computer and use it in GitHub Desktop.
Symfony Playgound with its Container and a custom configuration
<?php
use App\Kernel;
require __DIR__.'/vendor/autoload_runtime.php';
use Symfony\Component\Console\Output\StreamOutput;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
return function () {
$k = new class('dev', true) extends Kernel implements CompilerPassInterface {
public function process(ContainerBuilder $container)
{
$container->getAlias('logger')->setPublic(true);
$container->getDefinition('monolog.handler.console')->setPublic(true);
}
};
$k->boot();
$c = $k->getContainer();
$c->get('monolog.handler.console')->setOutput(
new StreamOutput(fopen('php://stdout', 'w'),
StreamOutput::VERBOSITY_VERY_VERBOSE,
));
$logger = $c->get('logger');
$em = $c->get('doctrine.orm.entity_manager');
// Start of playground
};
@lyrixx
Copy link
Author

lyrixx commented Sep 19, 2023

I often need to test "something" in my application, and it helps me to test that thing.

For example

  • Do I setup my CASCADE relation in the good direction : what does happen when I delete a project, does it also delete the organization
  • How does my Doctrine type behave if my column is empty
  • etc

It could be also solved with

  • an integration test
  • a custom debug command
  • a custom debug controller
  • etc.

But I have this habit for a very long time, and I like to be able to "hack" and prototype quickly. So in almost every projects I work on have this test.php file.

@gravataLonga
Copy link

Oh ok, gotcha you.

@Spomky
Copy link

Spomky commented Nov 10, 2023

Very good! Thanks you.
It is worth mentioning that this command can be executed using symfony php test.php

@florianorineveu
Copy link

Very useful, thx for sharing! 🔥

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment