Skip to content

Commit

Permalink
bare minimum
Browse files Browse the repository at this point in the history
  • Loading branch information
jigar-dhulla committed Oct 14, 2022
0 parents commit f4aec50
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
build
composer.lock
docs
vendor
.php_cs.cache
.phpunit.result.cache
.php-cs-fixer.cache
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (c) Jigar Dhulla <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Extended Http to dump and die with Curl command

This will create a macro for `PendingRequest` which will expose the new method `ddWithCurl` for `Http` Facade.

## Installation

You can pull in the package via composer:

``` bash
composer require jigarakatidus/laravel-http-to-curl --dev
```

The package will automatically register itself

## Usage

```php
Http::ddWithCurl('variable')
->acceptJson()
->asForm()
->withBasicAuth('username', 'password')
->get('https://example.com/padfhj', [
'foo' => 'foobar',
'bar' => 'barfoo',
]);
```

Outputs

```bash
curl -H 'User-Agent: GuzzleHttp/7' -H 'Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ=' -H 'Host: example.com' -H 'Accept: application/json' -H 'Content-Type: application/x-www-form-urlencoded' -X 'GET' 'https://example.com/padfhj?foo=foobar&bar=barfoo'
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

## Testing

``` bash
$ composer test
```

## Contributing

Pull Requests are welcome.

## Security

If you've found a bug regarding security please mail [[email protected]](mailto:[email protected]) instead of using the issue tracker.

## Credits

- [Jigar Dhulla](https://github.com/jigarakatidus)
- [All Contributors](../../contributors)

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
57 changes: 57 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "jigarakatidus/laravel-http-to-curl",
"description": "Extended Http to dump and die with Curl command",
"keywords": [
"http",
"curl",
"laravel",
"laravel-http-to-curl"
],
"homepage": "https://github.com/jigarakatidus/laravel-http-to-curl",
"license": "MIT",
"authors": [
{
"name": "Jigar Dhulla",
"email": "[email protected]",
"homepage": "https://github.com/jigarakatidus",
"role": "Developer"
}
],
"require": {
"php": "^8.0",
"illuminate/support": "^8.71|^9.0",
"jigarakatidus/command-line-generator": "dev-develop"
},
"require-dev": {
"mockery/mockery": "^1.4.2",
"orchestra/testbench": "^6.23|^7.0",
"phpunit/phpunit": "^9.4.4"
},
"suggest": {},
"autoload": {
"psr-4": {
"jigarakatidus\\HttpToCurl\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"jigarakatidus\\HttpToCurl\\Test\\": "tests"
}
},
"scripts": {
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes",
"test": "vendor/bin/phpunit"
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"jigarakatidus\\HttpToCurl\\HttpToCurlServiceProvider"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
42 changes: 42 additions & 0 deletions src/CurlCommandLineGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace jigarakatidus\HttpToCurl;

use Illuminate\Http\Client\Request;
use jigarakatidus\CommandLineGenerator;

class CurlCommandLineGenerator
{
public static function generate(Request $request, array $options): string
{
// Set binary
$commandGenerator = new CommandLineGenerator('curl');

// Set Headers
foreach ($request->headers() as $key => $headerValues) {
foreach ($headerValues as $value) {
$commandGenerator->addOption('H', sprintf('%s: %s', ucfirst($key), $value));
}
}

if(in_array($request->method(), ['post', 'put', 'patch'])) {
// Set Data according to Json
if($request->isJson()) {
$data = json_encode($request->data());
}

// Set Data according to Form
if($request->isForm()) {
$data = http_build_query($request->data());
}

$commandGenerator->addOption('d', $data);

// TODO Multipart
}

$commandGenerator->addOption('X', strtoupper($request->method()));
$commandGenerator->addArgument($request->url());
return $commandGenerator->generate();
}
}
27 changes: 27 additions & 0 deletions src/HttpToCurlServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace jigarakatidus\HttpToCurl;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Http\Client\Request;
use Illuminate\Support\ServiceProvider;
use Symfony\Component\VarDumper\VarDumper;

class HttpToCurlServiceProvider extends ServiceProvider
{
public function boot()
{
PendingRequest::macro('ddWithCurl', function() {
/** @var PendingRequest $this */
$values = func_get_args();

return self::beforeSending(function (Request $request, array $options) use ($values) {
foreach(array_merge($values, [CurlCommandLineGenerator::generate($request, $options)]) as $value) {
VarDumper::dump($value);
}

exit(1);
});
});
}
}

0 comments on commit f4aec50

Please sign in to comment.