Skip to content

Commit 4a05444

Browse files
Add skill for AI Agent (#99)
* Add skill * Add AI agent skill section to README * Rename PHP skill to generate-and-scan-barcode --------- Co-authored-by: Denis Averin <59285247+Denis-Averin@users.noreply.github.com>
1 parent d12d32d commit 4a05444

5 files changed

Lines changed: 288 additions & 1 deletion

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ To use these SDKs, you will need Client Id and Client Secret which can be looked
3333

3434
You can either directly use it in your project via source code or get [Packagist distribution](https://packagist.org/packages/aspose/barcode-cloud-php) (recommended).
3535

36+
## AI Agent Skills
37+
38+
This repository includes an AI-agent skill in [`skills/generate-and-scan-barcode-php/SKILL.md`](skills/generate-and-scan-barcode-php/SKILL.md). Point your coding agent to it when working with this SDK so it follows the repo workflow and SDK-specific API patterns.
39+
3640
## Installation
3741

3842
### Via Composer
@@ -136,4 +140,3 @@ Class | Method | HTTP request | Description
136140
- [RegionPoint](docs/Model/RegionPoint.md)
137141
- [ScanBase64Request](docs/Model/ScanBase64Request.md)
138142

139-
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
---
2+
name: generate-and-scan-barcode-php
3+
description: Write or update PHP code that uses the Aspose.BarCode Cloud SDK (`Aspose\BarCode\...`; Composer package `aspose/barcode-cloud-php`) to generate, recognize, or scan barcodes through Aspose's cloud REST API. Use this skill whenever the user wants barcode work in PHP, touches files under `submodules/php`, or mentions `GenerateApi`, `RecognizeApi`, `ScanApi`, `Configuration`, `GenerateRequestWrapper`, `GenerateBodyRequestWrapper`, `RecognizeBase64Request`, or `ScanMultipartRequestWrapper`. The PHP SDK has several easy-to-miss idioms, including constructing APIs as `new ...Api(null, $config)`, wrapping every call in a request-wrapper object, treating generate results as `SplFileObject` streams, using public `file_url` values for GET recognize and scan methods, and base64-encoding body payloads yourself.
4+
---
5+
6+
# Generate and scan barcode in PHP
7+
8+
Use this skill to write PHP code against the generated Aspose.BarCode Cloud SDK or to maintain the PHP SDK inside `submodules/php`. Most tasks boil down to choosing the right API class, choosing the right request wrapper, and using the correct transport shape for the data you have.
9+
10+
The Composer package name and PHP namespace differ. Install `aspose/barcode-cloud-php`, then import classes from `Aspose\BarCode\...`.
11+
12+
## Quick start
13+
14+
Use these imports in most PHP examples:
15+
16+
```php
17+
use Aspose\BarCode\Configuration;
18+
use Aspose\BarCode\GenerateApi;
19+
use Aspose\BarCode\RecognizeApi;
20+
use Aspose\BarCode\ScanApi;
21+
```
22+
23+
Prefer creating one `Configuration` instance and passing it into each API class:
24+
25+
```php
26+
$config = new Configuration();
27+
$config->setClientId($clientId);
28+
$config->setClientSecret($clientSecret);
29+
30+
$generateApi = new GenerateApi(null, $config);
31+
$recognizeApi = new RecognizeApi(null, $config);
32+
$scanApi = new ScanApi(null, $config);
33+
```
34+
35+
If the task edits SDK source, tests, snippets, or generated files in `submodules/php`, read `references/repo-workflow.md`. If the task needs the closest existing example or snippet, read `references/snippet-map.md`.
36+
37+
## Authenticate correctly
38+
39+
Use one of these two patterns:
40+
41+
1. Let the SDK fetch an access token lazily from client credentials.
42+
43+
```php
44+
$config = new Configuration();
45+
$config->setClientId($clientId);
46+
$config->setClientSecret($clientSecret);
47+
```
48+
49+
2. Inject a pre-fetched bearer token.
50+
51+
```php
52+
$config = new Configuration();
53+
$config->setAccessToken($token);
54+
```
55+
56+
The runtime defaults are:
57+
58+
- `Host`: `https://api.aspose.cloud`
59+
- `BasePath`: `/v4.0`
60+
- `AuthUrl`: `https://id.aspose.cloud/connect/token`
61+
62+
Inside this repo, tests load `tests/Configuration.json` first and then fall back to `TEST_CONFIGURATION_*` environment variables. Snippets usually check `TEST_CONFIGURATION_ACCESS_TOKEN` first and otherwise rely on client credentials. Mirror the surrounding file when editing existing repo code.
63+
64+
## Choose the right API shape
65+
66+
Pick the operation first:
67+
68+
- `GenerateApi`: create a barcode image.
69+
- `RecognizeApi`: decode one or more known barcode types and optionally tune recognition.
70+
- `ScanApi`: auto-detect barcode types with the smallest API surface.
71+
72+
Then pick the transport variant based on what the caller has:
73+
74+
- Public internet URL to an image: use `recognize()` or `scan()`. `file_url` must be publicly reachable, not a local path.
75+
- Local file on disk: use `recognizeMultipart()` or `scanMultipart()` with `new SplFileObject(..., 'rb')`.
76+
- Raw bytes already in memory: call `base64_encode()` yourself and use `recognizeBase64()` or `scanBase64()`.
77+
- Simple text plus query parameters for generation: use `generate()`.
78+
- Structured generate payload: use `generateBody()`.
79+
- Multipart-form generation: use `generateMultipart()` when the caller explicitly needs that shape.
80+
81+
Key method names:
82+
83+
- `generate`
84+
- `generateBody`
85+
- `generateMultipart`
86+
- `recognize`
87+
- `recognizeBase64`
88+
- `recognizeMultipart`
89+
- `scan`
90+
- `scanBase64`
91+
- `scanMultipart`
92+
93+
## Follow the PHP-specific SDK rules
94+
95+
1. Instantiate APIs as `new GenerateApi(null, $config)` and the equivalent `RecognizeApi` or `ScanApi` constructors. The first constructor argument is the optional Guzzle client, not the `Configuration`.
96+
2. Pass a request-wrapper object for every endpoint. Examples include `GenerateRequestWrapper`, `GenerateBodyRequestWrapper`, `RecognizeMultipartRequestWrapper`, and `ScanBase64RequestWrapper`.
97+
3. Treat `generate()`, `generateBody()`, and `generateMultipart()` results as `\SplFileObject`. Read them with `getSize()` and `fread()`, save them to disk, or pass them directly into `ScanMultipartRequestWrapper`.
98+
4. Build base64-body requests in two layers: create `RecognizeBase64Request` or `ScanBase64Request`, then wrap that model in the matching `...RequestWrapper`.
99+
5. Call `base64_encode()` yourself before `recognizeBase64()` or `scanBase64()`. The SDK does not encode raw bytes for you.
100+
6. Use one `DecodeBarcodeType` for `RecognizeRequestWrapper` and `RecognizeMultipartRequestWrapper`. Use a list in `RecognizeBase64Request(['barcode_types' => [...]])` when the request body should search multiple types.
101+
7. Use GET-based recognize and scan methods only for public URLs. For local files, use multipart or base64 instead.
102+
8. Expect `BarcodeResponseList` from recognize and scan methods. Iterate `getBarcodes()` and read `getBarcodeValue()`, `getType()`, `getRegion()`, and `getChecksum()`.
103+
9. Let the SDK fetch a token automatically when `AccessToken` is empty but `ClientId` and `ClientSecret` are present. On `401`, the generated clients refresh the token and retry once.
104+
10. Turn on `setDebug(true)` when request and response logging would help, and use `setUserAgent()` when a custom user agent is required.
105+
11. Catch `Aspose\BarCode\ApiException` for API failures. For many `4xx` failures, `getResponseObject()` contains a parsed `ApiErrorResponse`.
106+
107+
## Reuse the common patterns
108+
109+
Generate and save a QR code:
110+
111+
```php
112+
use Aspose\BarCode\Model\BarcodeImageFormat;
113+
use Aspose\BarCode\Model\CodeLocation;
114+
use Aspose\BarCode\Model\EncodeBarcodeType;
115+
use Aspose\BarCode\Requests\GenerateRequestWrapper;
116+
117+
$request = new GenerateRequestWrapper(EncodeBarcodeType::QR, 'hello from PHP');
118+
$request->image_format = BarcodeImageFormat::Png;
119+
$request->text_location = CodeLocation::None;
120+
121+
$file = $generateApi->generate($request);
122+
$file->rewind();
123+
file_put_contents('qr.png', $file->fread($file->getSize()));
124+
```
125+
126+
Recognize a local file stream:
127+
128+
```php
129+
use Aspose\BarCode\Model\DecodeBarcodeType;
130+
use Aspose\BarCode\Requests\RecognizeMultipartRequestWrapper;
131+
132+
$file = new SplFileObject('qr.png', 'rb');
133+
$result = $recognizeApi->recognizeMultipart(
134+
new RecognizeMultipartRequestWrapper(DecodeBarcodeType::QR, $file)
135+
);
136+
137+
foreach ($result->getBarcodes() as $barcode) {
138+
echo $barcode->getBarcodeValue() . PHP_EOL;
139+
}
140+
```
141+
142+
Auto-scan bytes already in memory:
143+
144+
```php
145+
use Aspose\BarCode\Model\ScanBase64Request;
146+
use Aspose\BarCode\Requests\ScanBase64RequestWrapper;
147+
148+
$payload = base64_encode(file_get_contents('unknown.png'));
149+
$result = $scanApi->scanBase64(
150+
new ScanBase64RequestWrapper(
151+
new ScanBase64Request(['file_base64' => $payload])
152+
)
153+
);
154+
```
155+
156+
## Work inside this repo
157+
158+
Read `references/repo-workflow.md` when the task changes SDK source, tests, snippets, package metadata, or generated code in `submodules/php`.
159+
160+
Read `references/snippet-map.md` when the task needs example code, README-aligned snippets, or the closest existing pattern for a generate, recognize, or scan scenario.
161+
162+
## Final checklist
163+
164+
1. Use the correct package and namespace pair: Composer `aspose/barcode-cloud-php`, namespace `Aspose\BarCode\...`.
165+
2. Construct API classes as `new ...Api(null, $config)`.
166+
3. Pass the matching request-wrapper object into every SDK call.
167+
4. Use GET only for public URLs, multipart for local files, and base64-body requests for bytes already in memory.
168+
5. Base64-encode request payloads yourself before `recognizeBase64()` or `scanBase64()`.
169+
6. Treat generate responses as `SplFileObject` streams and recognize or scan responses as `BarcodeResponseList`.
170+
7. When changing the repo, validate with the submodule workflow in `references/repo-workflow.md`.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
interface:
2+
display_name: "Generate and scan barcode in PHP"
3+
short_description: "Use Aspose.BarCode Cloud from PHP"
4+
default_prompt: "Use $generate-and-scan-barcode-php to write or update PHP code that generates, recognizes, or scans barcodes with Aspose.BarCode Cloud."
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# PHP submodule workflow
2+
3+
Use this reference when the task edits SDK source, tests, snippets, package metadata, or generated files inside `submodules/php`.
4+
5+
## Layout
6+
7+
- `src/Aspose/BarCode`: generated API clients, `Configuration`, `ApiException`, serializer, and transport helpers.
8+
- `src/Aspose/BarCode/Model`: generated request and response models plus enums.
9+
- `src/Aspose/BarCode/Requests`: request-wrapper objects for every SDK endpoint.
10+
- `tests`: PHPUnit coverage for configuration, generate, recognize, scan, exceptions, end-to-end flows, and serialization.
11+
- `snippets`: runnable documentation snippets grouped by `generate/*` and `read/*` scenarios.
12+
- `scripts`: snippet runners and post-generation helpers such as credential insertion, example insertion, and deprecation-warning injection.
13+
- `README.md` and `index.php`: user-facing usage examples.
14+
- `composer.json` and `Makefile`: local validation and automation entry points.
15+
16+
## Validation
17+
18+
On Windows, run repo scripts and Make targets through WSL.
19+
20+
From `submodules/php`:
21+
22+
- `make init`
23+
- `make format`
24+
- `make lint`
25+
- `make test`
26+
- `make after-gen`
27+
28+
What these targets do:
29+
30+
- `make init`: run `composer install`.
31+
- `make format`: run `php-cs-fixer fix index.php src/Aspose/ tests/ snippets/ --config=php-cs-fixer.conf`.
32+
- `make lint`: run `phpstan analyse index.php src/ tests/ --level=3`.
33+
- `make test`: run `composer test` (`phpunit`) and then `./scripts/run_snippets.sh`.
34+
- `make after-gen`: run `format`, `insert-example`, and `./scripts/add-deprecation-warnings.bash`.
35+
36+
`run_snippets.sh` does more than execute samples:
37+
38+
- creates `snippets_test`
39+
- symlinks the local `src` tree into that temp folder
40+
- copies each snippet through `scripts/insert-credentials.py`
41+
- executes each generated temp snippet with `php`
42+
43+
Treat snippet failures as consumer-facing regressions, not just sample breakage.
44+
45+
## Test configuration
46+
47+
- Tests load `tests/Configuration.json` first and then fall back to `TEST_CONFIGURATION_*` environment variables via `TestConfiguration::fromFileOrEnv()`.
48+
- Useful names include `TEST_CONFIGURATION_CLIENT_ID`, `TEST_CONFIGURATION_CLIENT_SECRET`, `TEST_CONFIGURATION_ACCESS_TOKEN`, `TEST_CONFIGURATION_HOST`, `TEST_CONFIGURATION_AUTH_URL`, and `TEST_CONFIGURATION_DEBUG`.
49+
- `tests/Configuration.example.json` only contains placeholder client credentials. The runtime defaults still use `https://api.aspose.cloud` plus `/v4.0` and `https://id.aspose.cloud/connect/token`.
50+
- Snippets embed placeholder dashboard credentials and optionally prefer `TEST_CONFIGURATION_ACCESS_TOKEN`. Mirror the surrounding file when editing repo code.
51+
52+
## Regenerated code workflow
53+
54+
If you change generated SDK code in this mono-repo:
55+
56+
1. Make the intended SDK edit in `submodules/php` so the target behavior is clear.
57+
2. Mirror the change in the matching template under `codegen/Templates/php` when the file is generated.
58+
3. Stage the PHP submodule changes.
59+
4. From the repo root, run `make php` through WSL.
60+
5. Ensure `submodules/php` has no new unstaged diffs after regeneration.
61+
6. If regeneration reintroduces old code, keep fixing templates or post-generation helpers until the generated output matches the intended SDK change.
62+
63+
## Useful anchors
64+
65+
- `src/Aspose/BarCode/Configuration.php`: auth defaults, debug flags, and user-agent behavior.
66+
- `src/Aspose/BarCode/Requests/*.php`: the wrapper shapes every endpoint expects.
67+
- `tests/TestConfiguration.php`: how tests discover config from JSON and environment variables.
68+
- `tests/EndToEndTest.php`: generate a barcode stream, then pass it directly into `ScanMultipartRequestWrapper`.
69+
- `scripts/run_snippets.sh` and `scripts/run_snippet.sh`: snippet execution harness.
70+
- `Makefile`: the local validation and post-generation entry points.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Snippet and example map
2+
3+
Use this reference when you want the closest existing PHP pattern before writing new SDK code or when updating docs, snippets, and examples.
4+
5+
## Small end-user examples
6+
7+
- `index.php`: compact generate-and-stream example suitable for quick starts.
8+
- `snippets/ManualFetchToken.php`: manual OAuth client-credentials token fetch without using the SDK.
9+
10+
## Generate patterns
11+
12+
- `snippets/generate/save/GenerateGet.php`: simple `generate()` and save-to-file flow.
13+
- `snippets/generate/save/GenerateBody.php`: `generateBody()` with `GenerateParams`.
14+
- `snippets/generate/save/GenerateMultipart.php`: multipart generation flow.
15+
- `snippets/generate/set-text/*`: `EncodeData` and `EncodeDataType` examples.
16+
- `snippets/generate/set-size/*`: width, height, resolution, and units examples.
17+
- `snippets/generate/set-colorscheme/*`: foreground and background color examples.
18+
- `snippets/generate/appearance/*`: richer `BarcodeImageParams` examples.
19+
20+
## Recognize and scan patterns
21+
22+
- `snippets/read/set-source/RecognizeGet.php`: recognize from a public URL.
23+
- `snippets/read/set-source/RecognizeMultipart.php`: recognize from a local `SplFileObject`.
24+
- `snippets/read/set-source/RecognizeBody.php`: recognize from base64 bytes.
25+
- `snippets/read/set-source/ScanGet.php`: auto-scan from a public URL.
26+
- `snippets/read/set-source/ScanMultipart.php`: auto-scan from a local `SplFileObject`.
27+
- `snippets/read/set-source/ScanBody.php`: auto-scan from base64 bytes.
28+
- `snippets/read/set-target-types/*`: choosing a single `DecodeBarcodeType` or a list for `RecognizeBase64Request`.
29+
- `snippets/read/set-quality/*`: `RecognitionMode` examples.
30+
- `snippets/read/set-image-kind/*`: `RecognitionImageKind` examples.
31+
32+
## Tests worth copying
33+
34+
- `tests/EndToEndTest.php`: generate a barcode stream, then scan that same stream end to end.
35+
- `tests/GenerateApiTest.php`: generate via GET, body, and multipart variants.
36+
- `tests/RecognizeApiTest.php`: recognize via base64 body, multipart, and public URL.
37+
- `tests/ScanApiTest.php`: scan via base64 body, multipart, and public URL.
38+
- `tests/ConfigurationTest.php`: configuration defaults, JSON serialization, and environment-variable loading.
39+
- `tests/ExceptionTest.php`: expected API failures and parsed error behavior.
40+
- `tests/ObjectSerializerTests.php`: serializer behavior if model or date formatting changes.

0 commit comments

Comments
 (0)