From 74f1b5a3b3157639cda0a8451bde3d2e989ec8bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Robles?= Date: Tue, 26 Dec 2023 20:50:05 +0100 Subject: [PATCH] initial commit --- .editorconfig | 15 ++++ .gitattributes | 8 +++ .github/workflows/phpstan.yml | 36 ++++++++++ .github/workflows/pint.yml | 22 ++++++ .github/workflows/publish.yml | 26 +++++++ .github/workflows/tests.yml | 64 +++++++++++++++++ .gitignore | 8 +++ .vscode/extensions.json | 5 ++ LICENSE | 21 ++++++ README.md | 72 +++++++++++++++++++ SECURITY.md | 15 ++++ composer.json | 61 ++++++++++++++++ dependabot.yml | 8 +++ phpstan.neon | 11 +++ phpunit.coverage.dist.xml | 29 ++++++++ phpunit.dist.xml | 21 ++++++ pint.json | 3 + src/Carbone.php | 66 +++++++++++++++++ ...teDocumentFromTemplateIdAndJsonDataSet.php | 33 +++++++++ .../RetreiveGeneratedDocumentFromRenderId.php | 29 ++++++++ .../Status/FetchTheApiStatusAndVersion.php | 23 ++++++ src/Requests/Template/CheckTemplateExists.php | 24 +++++++ .../Template/DeleteTemplateFromTemplateId.php | 29 ++++++++ .../DownloadTemplateFromTemplateId.php | 29 ++++++++ src/Requests/Template/UploadTemplate.php | 50 +++++++++++++ .../Template/UploadTemplateAsBase64.php | 46 ++++++++++++ src/Resource.php | 13 ++++ src/Resource/Render.php | 27 +++++++ src/Resource/Status.php | 15 ++++ src/Resource/Template.php | 61 ++++++++++++++++ .../Template/UploadTemplateResponse.php | 20 ++++++ src/ServiceProvider.php | 38 ++++++++++ tests/ServiceProviderTest.php | 14 ++++ tests/TestCase.php | 22 ++++++ 34 files changed, 964 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .github/workflows/phpstan.yml create mode 100644 .github/workflows/pint.yml create mode 100644 .github/workflows/publish.yml create mode 100644 .github/workflows/tests.yml create mode 100644 .gitignore create mode 100644 .vscode/extensions.json create mode 100644 LICENSE create mode 100644 README.md create mode 100644 SECURITY.md create mode 100644 composer.json create mode 100644 dependabot.yml create mode 100644 phpstan.neon create mode 100644 phpunit.coverage.dist.xml create mode 100644 phpunit.dist.xml create mode 100644 pint.json create mode 100644 src/Carbone.php create mode 100644 src/Requests/Render/GenerateDocumentFromTemplateIdAndJsonDataSet.php create mode 100644 src/Requests/Render/RetreiveGeneratedDocumentFromRenderId.php create mode 100644 src/Requests/Status/FetchTheApiStatusAndVersion.php create mode 100644 src/Requests/Template/CheckTemplateExists.php create mode 100644 src/Requests/Template/DeleteTemplateFromTemplateId.php create mode 100644 src/Requests/Template/DownloadTemplateFromTemplateId.php create mode 100644 src/Requests/Template/UploadTemplate.php create mode 100644 src/Requests/Template/UploadTemplateAsBase64.php create mode 100644 src/Resource.php create mode 100644 src/Resource/Render.php create mode 100644 src/Resource/Status.php create mode 100644 src/Resource/Template.php create mode 100644 src/Responses/Template/UploadTemplateResponse.php create mode 100644 src/ServiceProvider.php create mode 100644 tests/ServiceProviderTest.php create mode 100644 tests/TestCase.php diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..2db1c28 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +# EditorConfig is awesome: https://EditorConfig.org + +# top-most EditorConfig file +root = true + +[*] +indent_style = space +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = false +insert_final_newline = true + +[*.{yml,xml}] +indent_size = 2 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..76371f6 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,8 @@ +* text=auto + +*.lock -diff +*.min.js -diff +*.min.css -diff + +/.github export-ignore +CHANGELOG.md export-ignore diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..423ac3a --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,36 @@ +name: phpstan + +on: [ push, pull_request ] + +jobs: + analyze: + runs-on: ubuntu-latest + + name: PHPStan + + steps: + - uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.3' + coverage: none + tools: phpstan + + - name: Get composer cache directory + id: composer-cache + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - name: Cache dependencies + uses: actions/cache@v3 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: phpstan-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: phpstan-composer- + + - name: Install dependencies + run: composer install --no-interaction --no-suggest + + - name: Run analysis + run: phpstan analyse diff --git a/.github/workflows/pint.yml b/.github/workflows/pint.yml new file mode 100644 index 0000000..7fcce08 --- /dev/null +++ b/.github/workflows/pint.yml @@ -0,0 +1,22 @@ +name: pint + +on: [ push, pull_request ] + +jobs: + analyze: + runs-on: ubuntu-latest + + name: Laravel Pint + + steps: + - uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: '8.3' + coverage: none + tools: laravel/pint + + - name: Run analysis + run: pint diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..b8d0dfc --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,26 @@ +name: Publish + +on: + push: + tags: + - '**' + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Get release info + id: query-release-info + uses: release-flow/keep-a-changelog-action@v2 + with: + command: query + version: latest + + - name: Publish to Github releases + uses: softprops/action-gh-release@v1 + with: + body: ${{ steps.query-release-info.outputs.release-notes }} + # prerelease: true + # files: '*.vsix' diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..0b80bf6 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,64 @@ +name: tests + +on: [ push, pull_request ] + +jobs: + test: + runs-on: ${{ matrix.os }} + + strategy: + fail-fast: true + matrix: + os: [ ubuntu-latest ] + php: [ 8.1, 8.2, 8.3 ] + laravel: [ 9.*, 10.* ] + dependency-version: [ prefer-stable ] + include: + - laravel: 9.* + testbench: 7.* + + - laravel: 10.* + testbench: 8.* + exclude: + - laravel: 10.* + php: 8.0 + + name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + extensions: dom, curl, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, intl, exif + coverage: pcov + + - name: Get composer cache directory + id: composer-cache + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - name: Cache dependencies + uses: actions/cache@v3 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: ${{ runner.os }}-composer-${{ matrix.dependency-version }}- + + - name: Install dependencies + run: | + composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update + composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest + + - name: Execute tests + run: vendor/bin/phpunit -c phpunit.coverage.dist.xml + + - name: Upload coverage reports to Codecov + uses: codecov/codecov-action@v3 + with: + token: ${{ secrets.CODECOV_TOKEN }} + files: ./clover.xml + fail_ci_if_error: true + verbose: true diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62dcc62 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/vendor/ +/node_modules +composer.lock +phpunit.xml +.DS_Store +coverage +clover.xml +.phpunit.cache diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..8c8f0e9 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "open-southeners.vscode-php-extension-pack" + ] +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..830a466 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 Open Southeners + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..fb0054f --- /dev/null +++ b/README.md @@ -0,0 +1,72 @@ +Carbone PHP [![required php version](https://img.shields.io/packagist/php-v/open-southeners/carbone-sdk)](https://www.php.net/supported-versions.php) [![codecov](https://codecov.io/gh/open-southeners/carbone-sdk/branch/main/graph/badge.svg?token=codecov_badge_token)](https://codecov.io/gh/open-southeners/carbone-sdk) [![Edit on VSCode online](https://img.shields.io/badge/vscode-edit%20online-blue?logo=visualstudiocode)](https://vscode.dev/github/open-southeners/carbone-sdk) +=== + +Unofficial port of the Carbone API SDK to Saloon v3 + +## Getting started + +``` +composer require open-southeners/carbone-sdk +``` + +### Laravel installation + +To make this work within a Laravel app you just need to add the following at the very bottom of your `.env` file: + +```ini +CARBONE_API_KEY='your-carbone-api-key' +``` + +### Usage + +Within Laravel you've it injected into your application's container: + +```php +$response = app('carbone')->template()->base64Upload($templateBase64); + +if ($response->failed()) { + throw new \Exception('Template upload failed!'); +} + +// This is extracted from official Carbone SDK: https://carbone.io/api-reference.html#upload-a-template-carbone-cloud-sdk-php +$templateId = $response->getTemplateId(); +``` + +### Any other framework (or not) + +If you are using another framework (or just pure PHP), you can still use this as a standalone library: + +```php +$carbone = new Carbone('your-carbone-api-key'); + +// Use this if you want to use a different API version: https://carbone.io/api-reference.html#api-version +// $carbone = new Carbone('your-carbone-api-key', '4'); + +$templateBase64 = base64_encode(file_get_contents('path_to_your_template_here')); + +$response = $carbone->template()->base64Upload($templateBase64); + +if ($response->failed()) { + throw new \Exception('Template upload failed!'); +} + +// This is extracted from official Carbone SDK: https://carbone.io/api-reference.html#upload-a-template-carbone-cloud-sdk-php +$templateId = $response->getTemplateId(); +``` + +### Differences between official SDK and this + +- Use of [Saloon v3](https://github.com/saloonphp/saloon/releases/tag/v3.0.0) not the v1 (which improves at everything typed for better IDE autocompletion support, etc) +- **Replaced the `template()->upload()` method with `template()->base64Upload()`** so the upload method can be used to send `multipart/form-data` POST request instead of a `application/json` with all the file contents base64 encoded +- Added `template()->exists()` to get if template with ID exists in Carbone (just a HEAD request so no content fetch at all) +- Added `status()->fetch()` to get status (not currently publicly documented, only on the OpenAPI, Postman, etc. [Can check them here](https://carbone.io/api-reference.html#api-integration)) + +[You can still check the official one here](https://github.com/carboneio/carbone-sdk-php). + +## Partners + +[![skore logo](https://github.com/open-southeners/partners/raw/main/logos/skore_logo.png)](https://getskore.com) + +## License + +This package is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT). diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..229fcc3 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,15 @@ +# Security Policy + +## Supported Versions + +We're also following the [Laravel's default support policy](https://laravel.com/docs/master/releases#support-policy) on our semantic versioning. + +| Version | Supported | +| ------- | ------------------ | +| 1.x | :white_check_mark: | + + + +## Reporting a Vulnerability + +Send us an email at security@opensoutheners.com diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..513786f --- /dev/null +++ b/composer.json @@ -0,0 +1,61 @@ +{ + "name": "open-southeners/carbone-sdk", + "description": "Unofficial port of the Carbone API SDK to Saloon v3", + "license": "MIT", + "keywords": [ + "open-southeners", + "carbone", + "carbone-sdk", + "carbone-client", + "carbone-api", + "word", + "excel", + "office", + "pdf", + "generator" + ], + "authors": [ + { + "name": "Ruben Robles", + "email": "me@d8vjork.com", + "homepage": "https://d8vjork.com" + } + ], + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/open-southeners" + } + ], + "require": { + "php": "^8.0", + "saloonphp/saloon": "^3.0" + }, + "require-dev": { + "larastan/larastan": "^2.0", + "orchestra/testbench": "^7.0 || ^8.0", + "phpstan/phpstan": "^1.0", + "phpunit/phpunit": "^9.0 || ^10.0" + }, + "minimum-stability": "dev", + "autoload": { + "psr-4": { + "OpenSoutheners\\CarboneSdk\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "OpenSoutheners\\CarboneSdk\\Tests\\": "tests" + } + }, + "config": { + "sort-packages": true + }, + "extra": { + "laravel": { + "providers": [ + "OpenSoutheners\\CarboneSdk\\ServiceProvider" + ] + } + } +} \ No newline at end of file diff --git a/dependabot.yml b/dependabot.yml new file mode 100644 index 0000000..df4d15b --- /dev/null +++ b/dependabot.yml @@ -0,0 +1,8 @@ +version: 2 +updates: + + - package-ecosystem: "github-actions" + directory: "/" + schedule: + # Check for updates to GitHub Actions every week + interval: "weekly" diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..69a07e7 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,11 @@ +# includes: +# - ./vendor/larastan/larastan/extension.neon + +parameters: + + paths: + - src + + level: "max" + + checkMissingIterableValueType: false diff --git a/phpunit.coverage.dist.xml b/phpunit.coverage.dist.xml new file mode 100644 index 0000000..b9e3d39 --- /dev/null +++ b/phpunit.coverage.dist.xml @@ -0,0 +1,29 @@ + + + + + src + + + + + + + + tests + + + + + + + + diff --git a/phpunit.dist.xml b/phpunit.dist.xml new file mode 100644 index 0000000..f8764ec --- /dev/null +++ b/phpunit.dist.xml @@ -0,0 +1,21 @@ + + + + + tests + + + + + + + + diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..93061b6 --- /dev/null +++ b/pint.json @@ -0,0 +1,3 @@ +{ + "preset": "laravel" +} diff --git a/src/Carbone.php b/src/Carbone.php new file mode 100644 index 0000000..9235aa4 --- /dev/null +++ b/src/Carbone.php @@ -0,0 +1,66 @@ + Copy the `production` or `testing` API key. + * - Carbone On-premise: Update the `Server URL` on the Open API specification. + * + * Useful links: + * - [API Flow](https://carbone.io/api-reference.html#quickstart-api-flow) + * - [Integration / SDKs](https://carbone.io/api-reference.html#api-integration) + * - [Generated document storage](https://carbone.io/api-reference.html#report-storage) + * - [Request timeout](https://carbone.io/api-reference.html#api-timeout) + */ +class Carbone extends Connector +{ + public function __construct( + protected readonly string $token, + protected readonly ?string $version = '4' + ) { + // + } + + protected function defaultAuth(): HeaderAuthenticator + { + return new HeaderAuthenticator("Bearer {$this->token}"); + } + + protected function defaultHeaders(): array + { + return [ + 'carbone-version' => $this->version, + ]; + } + + public function resolveBaseUrl(): string + { + return 'https://api.carbone.io'; + } + + public function render(): Render + { + return new Render($this); + } + + public function status(): Status + { + return new Status($this); + } + + public function template(): Template + { + return new Template($this); + } +} diff --git a/src/Requests/Render/GenerateDocumentFromTemplateIdAndJsonDataSet.php b/src/Requests/Render/GenerateDocumentFromTemplateIdAndJsonDataSet.php new file mode 100644 index 0000000..4e151ec --- /dev/null +++ b/src/Requests/Render/GenerateDocumentFromTemplateIdAndJsonDataSet.php @@ -0,0 +1,33 @@ +templateId}"; + } + + /** + * @param string $templateId Unique identifier of the template + */ + public function __construct( + protected string $templateId, + ) { + } +} diff --git a/src/Requests/Render/RetreiveGeneratedDocumentFromRenderId.php b/src/Requests/Render/RetreiveGeneratedDocumentFromRenderId.php new file mode 100644 index 0000000..b19a36c --- /dev/null +++ b/src/Requests/Render/RetreiveGeneratedDocumentFromRenderId.php @@ -0,0 +1,29 @@ +renderId}"; + } + + /** + * @param string $renderId Unique identifier of the report + */ + public function __construct( + protected string $renderId, + ) { + } +} diff --git a/src/Requests/Status/FetchTheApiStatusAndVersion.php b/src/Requests/Status/FetchTheApiStatusAndVersion.php new file mode 100644 index 0000000..5112a5c --- /dev/null +++ b/src/Requests/Status/FetchTheApiStatusAndVersion.php @@ -0,0 +1,23 @@ +templateId}"; + } + + /** + * @param string $templateId Unique identifier of the template + */ + public function __construct( + protected string $templateId, + ) { + } +} diff --git a/src/Requests/Template/DeleteTemplateFromTemplateId.php b/src/Requests/Template/DeleteTemplateFromTemplateId.php new file mode 100644 index 0000000..3444b3e --- /dev/null +++ b/src/Requests/Template/DeleteTemplateFromTemplateId.php @@ -0,0 +1,29 @@ +templateId}"; + } + + /** + * @param string $templateId Unique identifier of the template + */ + public function __construct( + protected string $templateId, + ) { + } +} diff --git a/src/Requests/Template/DownloadTemplateFromTemplateId.php b/src/Requests/Template/DownloadTemplateFromTemplateId.php new file mode 100644 index 0000000..56afbb9 --- /dev/null +++ b/src/Requests/Template/DownloadTemplateFromTemplateId.php @@ -0,0 +1,29 @@ +templateId}"; + } + + /** + * @param string $templateId Unique identifier of the template + */ + public function __construct( + protected string $templateId, + ) { + } +} diff --git a/src/Requests/Template/UploadTemplate.php b/src/Requests/Template/UploadTemplate.php new file mode 100644 index 0000000..5f86061 --- /dev/null +++ b/src/Requests/Template/UploadTemplate.php @@ -0,0 +1,50 @@ + + */ + protected function defaultBody(): array + { + return [ + 'template' => new MultipartValue(name: 'template', value: $this->template), + ]; + } + + /** + * @param resource|string|\Psr\Http\Message\StreamInterface $template + */ + public function __construct( + private mixed $template + ) { + // + } +} diff --git a/src/Requests/Template/UploadTemplateAsBase64.php b/src/Requests/Template/UploadTemplateAsBase64.php new file mode 100644 index 0000000..8573952 --- /dev/null +++ b/src/Requests/Template/UploadTemplateAsBase64.php @@ -0,0 +1,46 @@ + + */ + protected function defaultBody(): array + { + return [ + 'template' => $this->template, + ]; + } + + public function __construct( + private string $template + ) { + // + } +} diff --git a/src/Resource.php b/src/Resource.php new file mode 100644 index 0000000..cb3efb1 --- /dev/null +++ b/src/Resource.php @@ -0,0 +1,13 @@ +connector->send(new GenerateDocumentFromTemplateIdAndJsonDataSet($templateId)); + } + + /** + * @param string $renderId Unique identifier of the report + */ + public function retreive(string $renderId): Response + { + return $this->connector->send(new RetreiveGeneratedDocumentFromRenderId($renderId)); + } +} diff --git a/src/Resource/Status.php b/src/Resource/Status.php new file mode 100644 index 0000000..dc23ecd --- /dev/null +++ b/src/Resource/Status.php @@ -0,0 +1,15 @@ +connector->send(new FetchTheApiStatusAndVersion()); + } +} diff --git a/src/Resource/Template.php b/src/Resource/Template.php new file mode 100644 index 0000000..15c6825 --- /dev/null +++ b/src/Resource/Template.php @@ -0,0 +1,61 @@ +connector->send(new CheckTemplateExists($templateId)); + } + + /** + * @param resource|string|\Psr\Http\Message\StreamInterface $template Path or stream of the file to upload as template + */ + public function upload(mixed $template): UploadTemplateResponse + { + /** @var \OpenSoutheners\CarboneSdk\Responses\Template\UploadTemplateResponse $response */ + $response = $this->connector->send(new UploadTemplate($template)); + + return $response; + } + + /** + * @param string $template Base64 content of the file to upload as template + */ + public function base64Upload(string $template): UploadTemplateResponse + { + /** @var \OpenSoutheners\CarboneSdk\Responses\Template\UploadTemplateResponse $response */ + $response = $this->connector->send(new UploadTemplateAsBase64($template)); + + return $response; + } + + /** + * @param string $templateId Unique identifier of the template + */ + public function download(string $templateId): Response + { + return $this->connector->send(new DownloadTemplateFromTemplateId($templateId)); + } + + /** + * @param string $templateId Unique identifier of the template + */ + public function delete(string $templateId): Response + { + return $this->connector->send(new DeleteTemplateFromTemplateId($templateId)); + } +} diff --git a/src/Responses/Template/UploadTemplateResponse.php b/src/Responses/Template/UploadTemplateResponse.php new file mode 100644 index 0000000..ef4378a --- /dev/null +++ b/src/Responses/Template/UploadTemplateResponse.php @@ -0,0 +1,20 @@ +json('data.templateId'); + + if (! $templateId) { + return null; + } + + return $templateId; + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php new file mode 100644 index 0000000..b5e7c6d --- /dev/null +++ b/src/ServiceProvider.php @@ -0,0 +1,38 @@ +app->bind(Carbone::class, function () { + /** @var string $key */ + $key = config('services.carbone.key', env('CARBONE_API_KEY', '')); + /** @var string|null $version */ + $version = config('services.carbone.version'); + + return new Carbone($key, $version); + }); + + $this->app->alias(Carbone::class, 'carbone'); + } + + /** + * Register any application services. + * + * @return void + */ + public function register() + { + // + } +} diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php new file mode 100644 index 0000000..8c33e65 --- /dev/null +++ b/tests/ServiceProviderTest.php @@ -0,0 +1,14 @@ +assertTrue($this->app->bound(Carbone::class)); + $this->assertTrue($this->app->bound('carbone')); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..fc9598e --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,22 @@ + + */ + protected function getPackageProviders($app) + { + return [ + ServiceProvider::class, + ]; + } +}