PHPackages                             abishekrsrikaanth/saloon-sdk-generator - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. abishekrsrikaanth/saloon-sdk-generator

ActiveProject[Utility &amp; Helpers](/categories/utility)

abishekrsrikaanth/saloon-sdk-generator
======================================

Simplified SDK Scaffolding for Saloon

00PHP

Since Jan 3Pushed 2y agoCompare

[ Source](https://github.com/abishekrsrikaanth/saloon-sdk-generator)[ Packagist](https://packagist.org/packages/abishekrsrikaanth/saloon-sdk-generator)[ RSS](/packages/abishekrsrikaanth-saloon-sdk-generator/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![](.github/header.png)](.github/header.png)

Saloon SDK Generator - Simplified SDK Scaffolding 🚀
===================================================

[](#saloon-sdk-generator---simplified-sdk-scaffolding-)

[![Latest Version on Packagist](https://camo.githubusercontent.com/5b024ad2056cd6f371edd19daadcfe4a5af41c8ea41d21fcfc711c00a774375c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f637265736361742d696f2f73616c6f6f6e2d73646b2d67656e657261746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/crescat-io/saloon-sdk-generator)[![Total Downloads](https://camo.githubusercontent.com/c0291ebeb621330c3477572d9eba051d70612883760ed8a296ca73d59dc3953b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f637265736361742d696f2f73616c6f6f6e2d73646b2d67656e657261746f722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/crescat-io/saloon-sdk-generator)

Introducing the Saloon SDK Generator – your tool for quickly creating the basic structure of PHP SDKs using the powerful [Saloon](https://docs.saloon.dev/) package.

Please note: This tool helps you set up the foundation for your SDK, but it might not create a complete, ready-to-use solution. 🛠️

Whether you're using Postman Collection JSON files (v2.1) or OpenAPI specifications, the Saloon SDK Generator simplifies the process of generating PHP SDKs. It provides you with a starting point to build the initial framework for your API interactions.

Keep in mind that the generated code might not be perfect for every situation. Think of it as a speedy way to scaffold your SDK structure. While you might need to customize it for specific cases, the Saloon SDK Generator saves you time by eliminating the need to create boilerplate code from scratch.

Your journey to crafting a tailored SDK starts here – with the Saloon SDK Generator. 🌟

Installation
------------

[](#installation)

You can install this package using Composer:

```
composer global require crescat-io/saloon-sdk-generator
```

Usage
-----

[](#usage)

To generate the PHP SDK from an API specification file, run the following command:

```
sdkgenerator generate:sdk API_SPEC_FILE.{json|yaml|yml}
     --type={postman|openapi}
    [--name=SDK_NAME]
    [--output=OUTPUT_PATH]
    [--namespace=Company\\Integration]
    [--force]
    [--dry]
    [--zip]
```

Replace the placeholders with the appropriate values:

- `API_SPEC_FILE`: Path to the API specification file (JSON or YAML format).
- `--type`: Specify the type of API specification (`postman` or `openapi`).
- `--name`: (Optional) Specify the name of the generated SDK (default: Unnamed).
- `--namespace`: (Optional) Specify the root namespace for the SDK (default: `App\\Sdk`).
- `--output`: (Optional) Specify the output path where the generated code will be created (default: ./Generated).
- `--force`: (Optional) Force overwriting existing files.
- `--dry`: (Optional) Perform a dry run. It will not save generated files, only show a list of them.
- `--zip`: (Optional) Use this flag to generate a zip archive containing all the generated files.

**Note:** Due to PHP using Backslashes `\`, when specifying the `--namespace`, you need to escape any backslashes like so:

```
sdkgenerator generate:sdk ./tests/Samples/paddle.json
  --force
  --type=postman
  --name=Paddle
  --output ./paddle-sdk/src
  --namespace=Your\\Sdk\\Namespace # run(Factory::parse($type, $inputPath));
```

3. **Use Generated Results:**

You can access the generated classes and perform actions with them:

```
// Generated Connector Class
echo "Generated Connector Class: " . Utils::formatNamespaceAndClass($result->connectorClass) . "\n";

// Generated Base Resource Class
echo "Generated Base Resource Class: " . Utils::formatNamespaceAndClass($result->resourceBaseClass) . "\n";

// Generated Resource Classes
foreach ($result->resourceClasses as $resourceClass) {
   echo "Generated Resource Class: " . Utils::formatNamespaceAndClass($resourceClass) . "\n";
}

// Generated Request Classes
foreach ($result->requestClasses as $requestClass) {
   echo "Generated Request Class: " . Utils::formatNamespaceAndClass($requestClass) . "\n";
}
```

---

How It Works
------------

[](#how-it-works)

The Saloon SDK Generator automates the creation of PHP SDKs by following these steps:

### 1. Parsing API Specifications

[](#1-parsing-api-specifications)

The parser reads and understands different API specification formats, including Postman Collection JSON (v2.1), OpenAPI specifications, and custom formats. When executed, it:

- Takes an input file.
- Converts the contents into an internal format called `ApiSpecification`.
- This format provides a comprehensive view of the API, which the SDK generator uses.

### 2. Auto-generating SDK Components

[](#2-auto-generating-sdk-components)

This component automatically generates essential SDK components based on parsed specifications:

- Request classes
- Resource classes
- Data Transfer Objects (DTOs)
- Connectors
- Foundational resource class

### 3. Configurable Generators

[](#3-configurable-generators)

The Saloon SDK Generator is modular, allowing you to replace default generators with custom ones to tailor the SDK generation to your needs. The following generators can be customized:

- `BaseResourceGenerator`
- `ConnectorGenerator`
- `DtoGenerator`
- `RequestGenerator`
- `ResourceGenerator`

Core Data Structures
--------------------

[](#core-data-structures)

These foundational structures form the basis of the generated SDK output:

### 1. ApiSpecification

[](#1-apispecification)

- Represents the entire API specification.
- Contains metadata like name, description, base URL, and endpoints catalog.

### 2. Config

[](#2-config)

- Central repository for SDK generator configurations.
- Defines namespaces, component suffixes, and other parameters.
- Customizable for specific requirements.

### 3. Endpoint

[](#3-endpoint)

- Describes individual API endpoints.
- Includes method types, path segments, and related parameters.

### 4. GeneratedCode

[](#4-generatedcode)

- Captures the complete generated SDK code.
- Encompasses PHP files for request classes, resource classes, DTOs, connectors, and foundational resource class.

### 5. Parameter

[](#5-parameter)

- Describes parameters associated with API endpoints.
- Includes attributes like type, name, and nullability.

Building a Custom Parser
------------------------

[](#building-a-custom-parser)

If you're working with an API specification format that isn't natively supported by the Saloon SDK Generator, you can build a custom parser to integrate it. Here's how you can do it:

1. **Create Your Custom Parser:**

Start by creating a custom parser class that implements the `Crescat\SaloonSdkGenerator\Contracts\Parser` interface.

There are two ways to initialize a Parser. The first one is through the constructor, which will receive the filePath specified when running `sdkgenerator generate:sdk {FILE_PATH}`.

Example:

```
