diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5a720ed --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +build +composer.lock +docs +vendor +.php_cs.cache +.phpunit.result.cache +.php-cs-fixer.cache \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..186b175 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright (c) Jigar Dhulla + +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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..adc482b --- /dev/null +++ b/README.md @@ -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 [jigar.tidus@gmail.com](mailto:jigar.tidus@gmail.com) 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. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..70e4e3c --- /dev/null +++ b/composer.json @@ -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": "jigar.tidus@gmail.com", + "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 +} \ No newline at end of file diff --git a/src/CurlCommandLineGenerator.php b/src/CurlCommandLineGenerator.php new file mode 100644 index 0000000..761eeb1 --- /dev/null +++ b/src/CurlCommandLineGenerator.php @@ -0,0 +1,42 @@ +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(); + } +} \ No newline at end of file diff --git a/src/HttpToCurlServiceProvider.php b/src/HttpToCurlServiceProvider.php new file mode 100644 index 0000000..0b36c4f --- /dev/null +++ b/src/HttpToCurlServiceProvider.php @@ -0,0 +1,27 @@ +