Symfony - Sylius : Symfony VarDumper server no output inside console - symfony

Context
I'm trying to use the Symfony VarDumper server to debug controller calls from an external API.
I'm a using Sylius 1.11 : the Symfony VarDumper is already as a core dependencies of the framework.
I have followed all the explanations here :
https://symfony.com/doc/5.4/components/var_dumper.html
https://symfony.com/blog/new-in-symfony-4-1-vardumper-server
Here is my debug.yaml :
#config/packages/debug.yaml
debug:
dump_destination: "tcp://%env(VAR_DUMPER_SERVER)%"
Then, I'm running the command to run the Symfony webserver : symfony server:start --no-tls and the one for the VarDumper webserver : symfony console server:dump
Here is the output of the VarDumper webserver :
Here is my controller with a dump instruction inside :
<?php
class AvailabilityAction extends AbstractController
{
public function __invoke(Request $request)
{
dump('hellofriend');
return new JsonResponse([
"sessions" => [
[
"startTimeLocal" => '2022-12-10 10:00:00',
"endTimeLocal" => '2022-12-10 12:00:00',
"seats" => 200,
"seatsAvailable" => 200
]
]
]);
}
}
Here the ouput of the curl command to test the route :
Issue
As we can see on the following picture, we can't see the output of the dump command. This is catch by the VarDumper webserver.. this is almost perfect.. but the output of the dump command not showing inside the VarDumper webserver output

Related

How can I use the "dump()" twig function in a template that is going to be rendered on production?

I would like to implement a debug output in our test environments in which I would like to output service requests that the application sends.
For this I wanted to use the symfony/twig function dump(), because here the output is wonderfully formatted for all types of variables and also offers the option of opening and closing the structure.
Pseudo-code would be something like this
{% if debugEnabled %}
{{dump (debugInfos)}}
{% endif %}
Unfortunately, "dump" is part of the Symfony DebugBundle, which for good reasons is not loaded in Prod environments and which should stay that way:
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true],
In the Symfony documentation says:
By design, the dump() function is only available in the dev and test
environments, to avoid leaking sensitive information in production. In
fact, trying to use the dump() function in the prod environment will
result in a PHP error.
I don't want to use dump() in production environments at all, but only locally to output our service requests.
However, I cannot implement a code like above because an error always occurs in production (undefined function dump()) of course, since dump() is not loaded at all.
You can create your own function which tests if the extension is loaded.
As you can see in the source the function twig_var_dump is registered, so you can test if the function exists or not to determine if the debug extension was loaded or not.
Should be fine to register your own function as dump as long as your extension gets loaded after the debug extension
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Twig\Environment;
class AppExtension extends AbstractExtension
{
public function getFunctions()
{
return [
new TwigFunction('dump', [$this, 'dump'] , ['needs_context' => true, 'needs_environment' => true, ]),
];
}
public function dump(Environment $env, $context, ...$vars)
{
if (!function_exists('\twig_var_dump')) return;
return \twig_var_dump($env, $context, ...$vars);
}
}
sidenote: Not tested this but should work
Create your own extension that's only loaded in production.
Create a folder (for example Twig) in your src directory.
Make that folder excluded from autowiring if you use the default:
App\:
resource: '../src/'
exclude:
- '../src/DependencyInjection/'
- '../src/Entity/'
- '../src/Kernel.php'
- '../src/Tests/'
- '../src/Twig/' # This is the directory excluded from autowiring
Then create a config/services_prod.yaml file with this content:
services:
App\Twig\FakeDebugExtension: # replace with your class name
tags:
- twig.extension # not needed if you use autoconfiguration
Then create a class with this content:
<?php
namespace App\Twig;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
final class FakeDebugExtension extends AbstractExtension
{
public function getFunctions(): array
{
return [
new TwigFunction('dump', [$this, 'fakeDump']),
];
}
public function fakeDump(...$arguments): void
{
}
}

Cannot autowire argument $article : Symfony 5

I'am currenty face to this error when i try to get my article page : Cannot autowire argument $article of "App\Controller\HomeController::show()": it references class "App\Entity\Article" but no such service exists.
I just created a very new small symfony 5 application :
The code in my controller is :
/**
* #Route("/show/{id}", name="show")
*/
public function show(Article $article): Response
{
if(!$article) {
return $this->redirectToRoute('home');
}
return $this->render('show/index.html.twig', [
'article' => $article,
]);
}
I'am following a tutorial on Udemy and i guess that i did the same thing than the former. Maybe version difference ?
Thanks in advance for your helps.
Thierry
The error you have is saying that it cannot find the auto-wire path to your entity. Using the Symfony installer will automatically configure parts like this for you.
The main part which auto-wires the Entity to the Dependency Injection layer is through Doctrine configuration and the Symfony ParamConverter found as part of the SensioFrameworkExtraBundle.
The ParamConverter has a lot of options to be able to convert parameters inside of the #Route into an Entity. Using your example above we see that Symfony will internally call ArticleRepository->findOneById($id) and return the result to your controller.
EDIT: Removed all mentions of Doctrine configurations. The errors that are displayed when Doctrine is not auto-wiring are different.
Same problem here (with attributs instead of annotations) on Symfony 5
I have even tried to explicite ParamConverter and Entity:
#[Route('/article/{id}/editer', name: 'edit_article', requirements: ["id" => "\d+"], methods: ['GET','POST'])]
#[ParamConverter('id', options: ['mapping' => ['id' => 'id']])]
#[Entity('article', expr: 'repository.findOneBySlug(id)')]
public function editArticle(Article $article, Request $request,
CategoryRepository $categoryRepository, EntityManagerInterface $manager): Response
{
dump($article); die;
}
but i have got the error message:
Cannot autowire argument $id of "App\Controller\DefaultController::editArticle()":
it references class "App\Entity\Article" but no such service exists.
Although there is:
use App\Entity\Article;
And the solution like #nico-haase said is to install this bundle
composer require sensio/framework-extra-bundle

OroPlatform: add custom field on core Entity

I'm currently working on an OroPlatform project and I need to add a custom field on the BusinessUnit core entity.
I have read the Oro documentation section about the way to extend core entities : https://doc.oroinc.com/backend/entities/extend-entities/#id1
<?php
namespace MyBundle\Bundle\AppBundle\Migrations\Schema\v1_0;
use Doctrine\DBAL\Schema\Schema;
use Oro\Bundle\EntityExtendBundle\EntityConfig\ExtendScope;
use Oro\Bundle\MigrationBundle\Migration\Migration;
use Oro\Bundle\MigrationBundle\Migration\QueryBag;
class AddColumnsToBusinessUnit implements Migration
{
public function up(Schema $schema, QueryBag $queries)
{
$table = $schema->getTable('oro_business_unit');
$table->addColumn('siret', 'string', [
'oro_options' => [
'extend' => ['owner' => ExtendScope::OWNER_CUSTOM],
'entity' => ['label' => 'siret'],
],
]);
}
}
When I run the command symfony console oro:migration:load --force, it works and the migration is applied to my database.
Now, I want a required field. I have seen the instruction 'notnull' => true to setup a non nullable field on the database.
Everything works well, but my field hasn't any JavaScript validation on the organization/business_unit/create route. Any ideas ?
You can validate the new field by extending the validation metadata that is already defined for the core entity you are extending.
To do this, please follow the official Symfony documentation and use the YML format:
https://symfony.com/doc/4.4/validation.html#constraint-configuration
The constraint that you can use for the field is "not blank."
Here is an example:
# src/<YourBundlePath>/Resources/config/validation.yml
Oro\Bundle\OrganizationBundle\Entity\BusinessUnit:
properties:
siret:
- NotBlank: ~

Symfony 4 disable profiler in controller action

I have an action in the controller for mass instert in the database...
So this uses a lot of resources and the profiler is caching everything and server goes down.
How can i disable the profiler (and all the debug services) in one action on the controller?
The controller looks like :
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Sync\Incomming\Syncronize;
/**
* #Route("/sync")
*/
class SyncController extends AbstractController
{
private $syncronize;
public function __construct(Syncronize $syncronize)
{
$this->syncronize = $syncronize;
}
/**
* #Route("/",name="sync_index")
*/
public function index(Request $request, Profiler $profiler)
{
$message = "Hello";
return $this->render( 'sync/output.html.twig', ['message' => $message ]);
}
}
if I try to autowire the profiler in the constructor method then I get the error public function __construct(Syncronize $syncronize, Profiler $profiler):
Cannot autowire service "App\Controller\SyncController": argument
"$profiler" of method "__construct()" references class
"Symfony\Component\HttpKernel\Profiler\Profiler" but no such service
exists. You should maybe alias this class to the existing "profiler"
service.
if I try to autowire the profiler in the index method then I get the error public function index(Request $request, Profiler $profiler):
Controller "App\Controller\SyncController::index()" requires that you
provide a value for the "$profiler" argument. Either the argument is
nullable and no null value has been provided, no default value has
been provided or because there is a non optional argument after this
one.
EDIT
For big queries disabling the profiler was not the solution... Actually you need to disable the setSQLLogger:
$em->getConnection()->getConfiguration()->setSQLLogger(null);
Symfony 3.4 / 4
https://symfony.com/doc/4.0/profiler/matchers.html
use Symfony\Component\HttpKernel\Profiler\Profiler;
class DefaultController
{
// ...
public function someMethod(Profiler $profiler)
{
// for this particular controller action, the profiler is disabled
$profiler->disable();
// ...
}
}
If you have an error with autowiring
# config/services.yaml
services:
Symfony\Component\HttpKernel\Profiler\Profiler: '#profiler'
Old:
If you want to disable the profiler from a controller, you can like this:
use Symfony\Component\HttpKernel\Profiler\Profiler;
// ...
class DefaultController
{
// ...
public function someMethod(Profiler $profiler)
{
// for this particular controller action, the profiler is disabled
$profiler->disable();
// ...
}
}
Source: https://symfony.com/doc/current/profiler/matchers.html
Another way would be to use: $this->get('profiler')->disable();
Older:
Simply switch the app to prod mode and disable debug mode.
To do this, open the .env file on the server in your favourite editor (Note: You should never commit this file to Git, as you store secrets in there!).
In that file, you should see a section starting with: ###> symfony/framework-bundle ###
Just below that there is a APP_ENV=dev and APP_DEBUG=1, change these two lines to APP_ENV=prod and APP_DEBUG=0. Then save the file.
Next you should clear the cache for prod mode and install the assets. To do this, run the following commands:
php bin/console cache:clear --env=prod --no-debug
php bin/console cache:warmup --env=prod --no-debug
php bin/console assets:install --env=prod --no-debug --symlink
If you now load the application, it is in prod mode, which includes more caching and is faster as debug is disabled.
Note:
There will still be a timelimit for PHP. If you still hit that limit, you can either change your PHP setting or alternatively you could run the import from CLI, as CLI usually has no timelimit. If users need to be able to upload on their own, I'd suggest having them upload the file, enter a "note" about the file to a db and have a cronjob reading that db for not imported files and import them.

Symfony Profiler on Console Command

When running a Symfony app in the dev environment, the web debug toolbar allows me to see how many queries that Doctrine generated. Is there a similar profiler option for Console commands?
As described in the docs, the profiler only collects information for all requests. I believe there is no collector for the console commands. One of the ways to get more insight into the queries executed by Doctrine is to check your log files. For example, you can do something like this on Unix based systems:
tail -f app/logs/dev.log | grep doctrine
Also see: http://symfony.com/doc/current/book/internals.html#profiler
Yeah, --verbose is useful as #manolo mentioned. You can control what gets output in -v -vv -vvv from the monolog handler config
monolog:
handlers:
main:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.log"
level: debug
console:
type: console
bubble: false
verbosity_levels:
VERBOSITY_VERBOSE: INFO
VERBOSITY_VERY_VERBOSE: DEBUG
channels: ["!doctrine"]
console_very_verbose:
type: console
bubble: false
verbosity_levels:
VERBOSITY_VERBOSE: NOTICE
VERBOSITY_VERY_VERBOSE: NOTICE
VERBOSITY_DEBUG: DEBUG
channels: ["doctrine"]
Notice how you can even disable a channel -v or --verbose will only output non doctrine logs at the specified verbose levels.
First of all, symfony profiler depends on Request. Thats why its cant be used in console commands out of the box and, probably, it will not be fixed. Related symfony issue
But you still can access default DBAL profiling logger. It should be instance of Doctrine\DBAL\Logging\DebugStack
It have public queries property, which hold all executed queries, parameters, execution time etc.
Whenever you will need to debug actual queries - you can do in such a way
/** #var $em Registry */
$em = $this->getContainer()->get('doctrine')->getManager();
$profiler = $this->getContainer()->get('doctrine.dbal.logger.profiling.default');
$shop = $em->getRepository(Shop::class)->find(7);
$sku = $em->getRepository(Sku::class)->find(45);
// clear profiles data, we want to profile Criteria below
$profiler->queries = [];
$shopWares = $shop->getShopWarePagesForSku($sku);
$output->writeln(var_export($profiler->queries));
It would generate output like
array (
3 =>
array (
'sql' => 'SELECT ...... FROM ShopWarePage t0 WHERE (t0.sku_id = ? AND t0.sku_id = ?)',
'params' =>
array (
0 => 45,
1 => 7,
),
'types' =>
array (
0 => 'integer',
1 => 'integer',
),
'executionMS' => 0.00075292587280273438,
),
)
It's possible to run a command from the controller or other services. So all command information will be in the profiler.
There is an example from symfony docs
// src/Controller/DebugTwigController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
class DebugTwigController extends AbstractController
{
public function debugTwig(KernelInterface $kernel): Response
{
$application = new Application($kernel);
$application->setAutoExit(false);
$input = new ArrayInput([
'command' => 'debug:twig',
// (optional) define the value of command arguments
'fooArgument' => 'barValue',
// (optional) pass options to the command
'--bar' => 'fooValue',
]);
// You can use NullOutput() if you don't need the output
$output = new BufferedOutput();
$application->run($input, $output);
// return the output, don't use if you used NullOutput()
$content = $output->fetch();
// return new Response(""), if you used NullOutput()
return new Response($content);
}
}

Resources