Symfony 5 Generating a URL from Console Command - php

I'm new to Symfony and am using 5.x. I have created a Console command using Symfony\Component\Console\Command\Command and am trying to use Symfony\Component\HttpClient\HttpClient to POST to a URL. I need to generate the URL to a route running on the same machine (but in future this may possibly change to a different machine), so the host could be like localhost or example.com, and the port of the API is custom. I have searched on the web but the only possible solution I got involved the use of Symfony\Component\Routing\Generator\UrlGeneratorInterface, and the web is cluttered with code samples for old versions of Symfony, and I haven't yet managed to get this working.
My latest attempt was:
public function __construct(UrlGeneratorInterface $router)
{
parent::__construct();
$this->router = $router;
}
but I don't really understand how to inject the parameter UrlGeneratorInterface $router to the constructor. I get an error that the parameter was not supplied. Do I have to create an instance of UrlGenerator elsewhere and inject it over here, or is there a simpler way to just generate an absolute URL in Symfony from within a Command? I don't really understand containers yet.
$url = $context->generate('view', ['Param' => $message['Param']], UrlGeneratorInterface::ABSOLUTE_URL);
services.yaml:
App\Command\MyCommand:
arguments: ['#router.default']
Is there a simpler way to generate a URL from a Console Command by
explicitly specifying host, protocol, port, route, parameters etc?
Why isn't UrlGeneratorInterface or RouterInterface autowiring?
Do I need to specify wiring manually as $router.default in
services.yaml if I also have autowiring enabled?
I understand that the execute function implementation may be
incorrect, but I couldn't get to fixing that without first getting
the constructor working. This is still, work in progress.
EDIT:
Updated gist: https://gist.github.com/tSixTM/86a29ee75dbd117c8f8571d458ed72db
EDIT 2: Made the problem statement clearer by adding question points: I slept on it :)
EDIT 3:
#!/usr/bin/env php
<?php
// application.php
require __DIR__.'/vendor/autoload.php';
use Symfony\Component\Console\Application;
$application = new Application();
$application->add(new App\Command\MyCommand());
$application->run();

I tinkered around with your gist and found the following to work:
https://gist.github.com/Matts/528c249a82e5844164039c4f6c0db046
The problem that you seemed to have, was not due to your service declaration, rather it was that you were missing the declaration of the private $router variable in MyCommand, see line 25.
So you can keep the services.yaml as you show in your gist, no changes required to the autowire variable, also you don't have to manually declare the command
Further, you don't need to fetch $context from the router, you can also set the base URL in your framework.yaml, here you can find where I found this.
Please note that I removed some code from the execute, this was due to me not having access to your other files. You can just re-add this.

Well, it wasn't all that straightforward figuring this out. A lot of the docs are out of date or don't address this issue completely. This is what I got so far:
services.yaml:
Symfony\Component\Routing\RouterInterface:
arguments: ['#router']
application.php:
#!/usr/bin/env php
<?php
// application.php
require __DIR__.'/vendor/autoload.php';
require __DIR__.'/src/Kernel.php';
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Dotenv\Dotenv;
$dotenv = new Dotenv();
$dotenv->load(__DIR__.'/.env', __DIR__.'/.env.local');
$kernel = new App\Kernel(getenv('APP_ENV'), getenv('APP_DEBUG'));
$kernel->boot();
$container = $kernel->getContainer();
$application = new Application($kernel);
$application->add(new App\Command\MyCommand($container->get('router')));
$application->run();
Note: I changed the Application import to Symfony\Bundle\FrameworkBundle\Console\Application
MyCommand.php:
<?php
// src/Command/MyCommand.php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Command\LockableTrait;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\HttpClient\HttpClient;
use App\SQSHelper;
class MyCommand extends Command
{
use LockableTrait;
// the name of the command (the part after "bin/console")
protected static $defaultName = 'app:my-command';
protected $router;
public function __construct(RouterInterface $router)
{
parent::__construct();
$this->router = $router;
}
protected function configure()
{
}
protected function execute(InputInterface $input, OutputInterface $output)
{
if($this->lock()) { // Prevent running more than one instance
$endpoint =
$queueName = 'Queue';
$queue = new SQSHelper();
while($queue->getApproxNumberOfMessages($queueName)) {
$message = $queue->receiveMessage($queueName);
if($message) {
if($message['__EOQ__'] ?? FALSE) // End-of-Queue marker received
break;
$context = $this->router->getContext();
$context->setHost('localhost');
$context->setHttpPort('49100');
$context->setHttpsPort('49100');
$context->setScheme('https');
$context->setBaseUrl('');
$url = $this->router->generate('ep', ['MessageId' => $message['MessageId']], UrlGeneratorInterface::ABSOLUTE_URL);
$client = HttpClient::create();
$response = $client->request('POST', $url, [
'headers' => ['Content-Type' => 'application/json'],
'body' => $message['Body'] // Already JSON encoded
]);
}
}
$this->release(); // Release lock
// this method must return an integer number with the "exit status code"
// of the command. You can also use these constants to make code more readable
// return this if there was no problem running the command
// (it's equivalent to returning int(0))
return Command::SUCCESS;
// or return this if some error happened during the execution
// (it's equivalent to returning int(1))
// return Command::FAILURE;
}
}
}
If anything feels off or if you could offer a better solution or improvements, please contribute...
Thanks Matt Smeets for your invaluable help figuring out there is no problem with the command, and if you can suggest a better alternative for the application.php, I'll accept your answer.

Solution introduced with Symfony 5.1 :
https://symfony.com/doc/current/routing.html#generating-urls-in-commands
Generating URLs in commands works the same as generating URLs in services. The only difference is that commands are not executed in the HTTP context. Therefore, if you generate absolute URLs, you’ll get http://localhost/ as the host name instead of your real host name.
The solution is to configure the default_uri option to define the “request context” used by commands when they generate URLs:
# config/packages/routing.yaml
framework:
router:
# ...
default_uri: 'https://example.org/my/path/'

Related

Laravel testing requests

I'm working on a large Laravel app, currently on v8.45.1 which has never had tests, so I'm working to get it to a point where we can start writing unit & feature tests.
I'm hitting an issue where the two request classes (App\Core\Request and App\Core\FormRequest) both use a trait RequestTrait which holds a set of utility methods.
This obviously works fine in local/staging/production, but when I run the test suite it complains that none of the methods provided by the trait exist:
Method Illuminate\Http\Request::isFromTrustedSource does not exist.
They are being called in various places as Request::isFromTrustedSource() or request()->isFromTrustedSource().
I can imagine that when running the app in the test environment, there may be differences to the request. Is it using a different class, or does the trait not apply for some reason?
I think, I found your problem - App\Core\Request extends Illuminate\Http\Request and in index.php you use App\Core\Request
The problem is in Illuminate\Foundation\Testing\Concerns\MakesHttpRequests::call()
When you use $this->get(...) in test suite - this method bootstrap app with standard request - not with your App\Core\Request
You can override this method in base tests/TestCase.php and pass your own request.
Unfortunately, it has no contract, than you cannot work with this through $this->app->bind()
Something like this:
class TestCase extends BaseTestCase
{
public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
{
//other code
$response = $kernel->handle(
$request = \App\Core\Request::createFromBase($symfonyRequest)
);
//other code
}
}

How to use wildcard route in league/route in Lazy Loading way

I am using league/route 4.2.
I am trying to implement a lazy loading controller using wildcard.
class Router
{
private $container;
private $router;
function __construct()
{
$this->container = new Container();
$this->router = new \League\Route\Router();
$this->mapRouters();
$this->dispatch();
}
private function mapRouters() {
$this->router->map('GET', '/', [MainController::class, 'index']);
//$this->router->map('GET', 'Main', 'Nanitelink\Module\Main\MainController::index');
$this->router->map('GET', '{module}', 'Nanitelink\Module\{module}\{module}Controller::index');
}
private function dispatch() {
$request = $this->container->get('Zend\Diactoros\ServerRequest');
$response = $this->router->dispatch($request);
$emitter = $this->container->get('Zend\HttpHandlerRunner\Emitter\SapiEmitter');
$emitter->emit($response);
}
}
Now I know for a fact that the commented route works.
I tried to replace it with wildcard, but probably I am not getting the syntax correctly, but it throws following exception.
Class 'Nanitelink\Module\{module}\{module}Controller' not found
What is the right way to use wildcard routing in league/route?
Documentation does not explain how to use wildcard in lazy loading way.
This was the discussion on github with the package manager.
https://github.com/thephpleague/route/issues/247
So to keep the answer short, the package does not and will not allow the requirement to replace the callable to be named using variables matched to the wildcard.
I did suggest a source code change in github, if anyone need the information.

Accessing and using Symfony model layer from Outside the Symfony applications

What I have is a symfony application, which contains some entities along with some repositories. A second non-symfony application should interface with the first one for interacting with some logic written in it (in this very moment just using the entities and their proper repositories).
Keep in mind that the first application could have its own autoload register etc.
I thought of an API class for external applications, which stays in the app directory. To use that the application should require a script. Here is the idea:
app/authInterface.php that the external application should require:
$loader = require __DIR__.'/autoload.php';
require_once (__DIR__.'/APIAuth.php');
return new APIAuth();
and an example of a working APIAuth I wrote (the code is kind of messy: remember this is just a try but you can get the idea):
class APIAuth
{
public function __construct()
{
//dev_local is a personal configuration I'm using.
$kernel = new AppKernel('dev_local', false);
$kernel->loadClassCache();
$kernel->boot();
$doctrine = $kernel->getContainer()->get('doctrine');
$em = $doctrine->getManager();
$users = $em->getRepository('BelkaTestBundle:User')->findUsersStartingWith('thisisatry');
}
by calling it by the shell everything works and I'm happy with it:
php app/authInterface.php
but I'm wondering if I'm doing in the best way possible in terms of:
resources am I loading just the resources I really need to run my code? Do I really need the kernel? That way everything is properly loaded - including the DB connection- but I'm not that sure if there are other ways to do it lighter
symfony logics am I interacting with symfony the right way? Are there better ways?
Symfony allows using its features from the command line. If you use a CronJob or another application, and want to call your Symfony application, you have two general options:
Generating HTTP endpoints in your Symfony application
Generating a command which executes code in your Symfony application
Both options will be discussed below.
HTTP endpoint (REST API)
Create a route in your routing configuration to route a HTTP request to a Controller/Action.
# app/config/routing.yml
test_api:
path: /test/api/v1/{api_key}
defaults: { _controller: AppBundle:Api:test }
which will call the ApiController::testAction method.
Then, implement the testAction with your code you want to excecute:
use Symfony\Component\HttpFoundation\Response;
public function testAction() {
return new Response('Successful!');
}
Command
Create a command line command which does something in your application. This can be used to execute code which can use any Symfony service you have defined in your (web)application.
It might look like:
// src/AppBundle/Command/TestCommand.php
namespace AppBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class GreetCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('myapp:section:thecommand')
->setDescription('Test command')
->addArgument(
'optionname',
InputArgument::OPTIONAL,
'Test option'
)
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$option = $input->getArgument('optionname');
if ($option) {
$text = 'Test '.$option;
} else {
$text = 'Test!';
}
$output->writeln($text);
}
}
Look here for documentation.
Call your command using something like
bin/console myapp:section:thecommand --optionname optionvalue
(Use app/console for pre-3.0 Symfony installations.)
Use whichever option you think is best.
One word of advice. Do not try to use parts of the Symfony framework when your application is using the full Symfony framework. Most likely you will walk into trouble along the way and you're making your own life hard.
Use the beautiful tools you have at your disposal when you are already using Symfony to build your application.

Can I create one Client and reuse it for all my functional tests?

tl;dr: Will this potentially keep my tests from working properly?
I'm trying to write functional tests for my Symfony project, and working from examples in The Symfony Book. So far, each method of the test class starts out with the same line of code:
namespace Tests\AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class SomeControllerTest extends WebTestCase
{
public function testSomethingOnRouteX()
{
$client = static::createClient();
// set up and assert
}
public function testSomethingElseOnRouteX()
{
$client = static::createClient();
// different set up and assert
}
}
I would like to remove this redundant code, but I am not sure if I should do this.
I added a constructor where I created the client.
public function __construct()
{
parent::__construct();
$this->client = static::createClient();
}
then in the various test methods I can just use $this->client without needing to create it repeatedly. This seems to work so far (I don't have many tests yet.) But I'm new enough to this framework, and to this type of testing in general that I'm not sure if it will cause problems down the road.
The recommended way to do it is to either use the setUp() method, or the #before hook. Both are called before each test method, so you're safe as the state won't be shared between test cases. Cleanup is also done for you automatically after each test case is run (implemented in the WebTestCase class).
namespace Tests\AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Client;
class SomeControllerTest extends WebTestCase
{
/**
* #var Client
*/
private $client;
protected setUp()
{
$this->client = self::createClient();
}
public function testSomethingOnRouteX()
{
// set up and assert
$this->client->request('GET', '/something');
}
public function testSomethingElseOnRouteX()
{
// different set up and assert
$this->client->request('GET', '/something-else');
}
}
Alternatively to setUp() you can use the #before hook:
namespace Tests\AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Bundle\FrameworkBundle\Client;
class SomeControllerTest extends WebTestCase
{
/**
* #var Client
*/
private $client;
/**
* #before
*/
protected function setUp()
{
$this->client = self::createClient();
}
// ...
}
If you try it, you should probably use the setUp-method instead. Reusing a client might introduce side effects, which is something you try to avoid. If your tests start to randomly fail, you probably want to try to go back to creating a new client per test. I wouldn't recommend it, but more from a gut feeling than actual bad experience. It should work fine most of the time, but when you suddenly reuse a client with for example a differently set header and it doesn't work you will have a headache.
I don't think there will be a huge performance gain as ui-tests are slow anyway, so trying to have as few test cases is probably a better way to go (look for test pyramid, if you don't know what I mean) if that's your aim.
It may work or may not work, depending on what you're testing.
A new client is like a fresh browser installation. No cookies are set, no history, no actual page, and so on.
If you're testing auth for example, it would be really bad if testCanNotAccessInternalResourceIfNotLoggedIn would use the client which is still logged in because testLogInWithCorrectCredentialsWorks ran before it and therefore fails. Sure, you can make sure you logout the user before accessing the resource but just creating a clean new browser instance is the easiest and least error-prone way to do it.

Using Goutte with Symfony2 in Controller

I'm trying to scrape a page and I'm not very familiar with php frameworks, so I've been trying to learn Symfony2. I have it up and running, and now I'm trying to use Goutte. It's installed in the vendor folder, and I have a bundle I'm using for my scraping project.
Question is, is it good practice to do scraping from a Controller? And how? I have searched forever and cannot figure out how to use Goutte from a bundle, since it's buried deep withing the file structure.
<?php
namespace ontf\scraperBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Goutte\Client;
class ThingController extends Controller
{
public function somethingAction($something)
{
$client = new Client();
$crawler = $client->request('GET', 'http://www.symfony.com/blog/');
echo $crawler->text();
return $this->render('scraperBundle:Thing:index.html.twig');
// return $this->render('scraperBundle:Thing:index.html.twig', array(
// 'something' => $something
// ));
}
}
I'm not sure I have heard of "good practices" as far as scraping goes but you may be able to find some in the book PHP Architect's Guide to Web Scraping with PHP.
These are some guidelines I have used in my own projects:
Scraping is a slow process, consider delegating that task to a background process.
Background process normally run as a cron job that executing a CLI application or a worker that is constantly running.
Use a process control system to manage your workers. Take a look at supervisord
Save every scraped file (the "raw" version), and log every error. This will enable you to detect problems. Use Rackspace Cloud Files or AWS S3 to archive these files.
Use the Symfony2 Console tool to create the commands to run your scraper. You can save the commands in your bundle under the Command directory.
Run your Symfony2 commands using the following flags to prevent running out of memory: php app/console scraper:run example.com --env=prod --no-debug Where app/console is where the Symfony2 console applicaiton lives, scraper:run is the name of your command, example.com is an argument to indicate the page you want to scrape, and the --env=prod --no-debug are the flags you should use to run in production. see code below for example.
Inject the Goutte Client into your command like such:
Ontf/ScraperBundle/Resources/services.yml
services:
goutte_client:
class: Goutte\Client
scraperCommand:
class: Ontf\ScraperBundle\Command\ScraperCommand
arguments: ["#goutte_client"]
tags:
- { name: console.command }
And your command should look something like this:
<?php
// Ontf/ScraperBundle/Command/ScraperCommand.php
namespace Ontf\ScraperBundle\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Goutte\Client;
abstract class ScraperCommand extends Command
{
private $client;
public function __construct(Client $client)
{
$this->client = $client;
parent::__construct();
}
protected function configure()
{
->setName('scraper:run')
->setDescription('Run Goutte Scraper.')
->addArgument(
'url',
InputArgument::REQUIRED,
'URL you want to scrape.'
);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$url = $input->getArgument('url');
$crawler = $this->client->request('GET', $url);
echo $crawler->text();
}
}
You Should take a Symfony-Controller if you want to return a response, e.G a html output.
if you only need the function for calculating or storing stuff in database,
You should create a Service class that represents the functionality of your Crawler, e.G
class CrawlerService
{
function getText($url){
$client = new Client();
$crawler = $client->request('GET', $url);
return $crawler->text();
}
and to execute it i would use a Console Command
If you want to return a Response use a Controller

Categories