Skip to content

Instantly share code, notes, and snippets.

@a-r-m-i-n
Last active June 9, 2021 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save a-r-m-i-n/a46d20ed10122b422c073f9d46107a85 to your computer and use it in GitHub Desktop.
Save a-r-m-i-n/a46d20ed10122b422c073f9d46107a85 to your computer and use it in GitHub Desktop.
Using Symfony on CLI without a Console application
#!/usr/bin/env php
<?php
use App\Entity\User;
use App\Kernel;
use Doctrine\Bundle\DoctrineBundle\Registry;
if (!is_file(dirname(__DIR__).'/vendor/autoload_runtime.php')) {
throw new LogicException('Symfony Runtime is missing. Try running "composer require symfony/runtime".');
}
require_once dirname(__DIR__).'/vendor/autoload_runtime.php';
return function (array $context) {
$kernel = new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']);
$kernel->boot();
$container = $kernel->getContainer();
/** @var Registry $doctrine */
$doctrine = $container->get('doctrine');
$em = $doctrine->getManagerForClass(User::class);
$user = $em->find(User::class, 1);
var_dump($user);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment