ixudra / curl

Custom PHP curl library for the Laravel 5 framework - developed by Ixudra
MIT License
561 stars 128 forks source link

Feature Request: Xdebug param support #62

Closed nahuelhds closed 7 years ago

nahuelhds commented 7 years ago

Feature reuest.

I found my self extending the libray in my app just to add XDEBUG_SESSION_START param to the URL. I'd be nice to be added automatically by a env param or a second arg to the "to" method.

What I'm doing is to use the APP_DEBUG default env param to know when to add the XDEBUG param and if I need a value for that param then I use a seconds custom env variable to do so.

elimentz commented 7 years ago

Can you provide some source code? An example of what you're trying to do?

nahuelhds commented 7 years ago

Hi. I've extended your classes to add a simple check that adds "XDEBUG_SESSION_START=" at the end of the url used in the CURL request. That in order to be able to debug my API when I called from an outside project.

I've created three files. Being "Aviron" the project name...

The service (a clean copy of yours)

// app\Aviron\Curl\CurlService.php
<?php

namespace App\Aviron\Curl;

class CurlService {

    /**
     * @param $url string   The URL to which the request is to be sent
     * @return App\Aviron\Curl\Builder
     */
    public function to($url)
    {
        $builder = new Builder();

        return $builder->to($url);
    }

}

The builder (here is the actual extension to your lib).

// \app\Aviron\Curl\Builder.php
<?php

namespace App\Aviron\Curl;

class Builder extends \Ixudra\Curl\Builder
{

    /**
     * XDebug parameter to pass on the CURL request
     *
     * @var string
     */
    private $xdebugSessionName = '1';

    /**
     * Set the URL to which the request is to be sent
     *
     * @param $url string   The URL to which the request is to be sent
     * @return Builder
     */
    public function to($url)
    {

        $finalUrl = $url;
        $isDebug = env('APP_DEBUG', false);

        if ($isDebug) {
            $char = strpos($url, '?') ? '&' : '?';
            $finalUrl .= $char . 'XDEBUG_SESSION_START=' . env('XDEBUG_SESSION_NAME', $this->xdebugSessionName);
        }

        return parent::to($finalUrl);
    }
}

The service provider

// app\Providers\CurlServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class CurlServiceProvider extends ServiceProvider
{

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {

        $this->app['Curl'] = $this->app->share(
            function ($app) {
                return new \App\Aviron\Curl\CurlService();
            }
        );
    }
}

Finally, at the app config, into the providers array I've replaced yours with mine.

// config\app.php

    'providers' => [
        // ...
        App\Providers\CurlServiceProvider::class,
        //...
     ]

Finally in the environment configuration I've created a variable named XDEBUG_SESSION_NAME that for use it if I need the session name to be an specific one. If the var is not set, defaults to "1" as you can see in the Builder::to method.

It checks if the app is in debug mode through APP_DEBUG bool env var. If it is then it adds the XDEBUG_SESSION_START param to the URL with a "?" or a "&" if there are already other get params already set.

That way I can xdebug my API from my external project without having to use Postman in order to replicate the request that the project does to the API.

elimentz commented 7 years ago

Included in v 6.12.0

        $response = \Curl::to('http://foo.com/bar')
            ->withData( array( 'Foo' => 'Bar' ) )
            ->enableXDebug()
            ->get();

By default, the package fill add the session name "session_1" to the URL. I didn't include any environment variables because the package is framework independent, despite the strong Laravel focus. This should not be a problem for you though. If you pass an empty string to the function, no parameters will be added. this means that you can use an environment variable as argument. By not defining it in your production environment and by setting the default to an empty string, xDebug will only be enabled when needed.

        $response = \Curl::to('http://foo.com/bar')
            ->withData( array( 'Foo' => 'Bar' ) )
            ->enableXDebug( env('XDEBUG_SESSION_NAME', '') )
            ->get();

@nahuelhds I hope this addresses your need. I didn't test this thoroughly since I don't know how to set this up in xDebug so I'd appreciate some feedback from your part.

nahuelhds commented 7 years ago

This is good advance. Thanks!

Particuarly I've need a wrapper to enable xdebugging to all the posts without having to add lines to every Curl request that's made inside the outside project. The way I've done it I just enable it for all the requests made under the Curl::to method.

Anyway this is superb. Thank you really!

nahuelhds commented 7 years ago

Last, a little mention in the readme will be great for anyone to know