Skip to content

Commit 38433b9

Browse files
committed
feat(example): Add example script for SoarPHP usage
- Introduce a new example.php file demonstrating the usage of SoarPHP. - Include basic and advanced scoring examples with queries. - Provide examples of running various Soar commands with explanations. Signed-off-by: guanguans <[email protected]>
1 parent 5329bcd commit 38433b9

File tree

3 files changed

+111
-7
lines changed

3 files changed

+111
-7
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ composer require guanguans/soar-php --ansi -v
3939
```php
4040
<?php
4141

42-
/** @noinspection SqlResolve */
43-
4442
declare(strict_types=1);
4543

4644
require __DIR__.'/vendor/autoload.php';
@@ -70,6 +68,7 @@ $queries = [
7068
* Examples of scoring.
7169
*/
7270
$scores = Soar::make()->arrayScores($queries); // Basic scoring
71+
dump($scores);
7372

7473
$scores = Soar::make() // Advanced scoring
7574
->withTestDsn([
@@ -98,16 +97,17 @@ $scores = Soar::make() // Advanced scoring
9897
* Examples of running any soar command.
9998
*/
10099
// Final run: '/.../bin/soar.darwin-arm64' '-report-type=fingerprint' '-query=SELECT * FROM `foo`;'
101-
$fingerprint = Soar::make()->withReportType('fingerprint')->withQuery($queries[1])->dump()->run();
100+
$fingerprint = Soar::make()->withReportType('fingerprint')->withQuery($queries[1])->run();
102101

103102
// Final run: '/.../bin/soar.darwin-arm64' '-report-type=pretty' '-query=SELECT * FROM `foo`;'
104-
$pretty = Soar::make()->withReportType('pretty')->withQuery($queries[1])->dump()->run();
103+
$pretty = Soar::make()->withReportType('pretty')->withQuery($queries[1])->run();
105104

106105
// Final run: '/.../bin/soar.darwin-arm64' '-version=true'
107-
$version = Soar::make()->withHelp(true)->setVersion(true)->dump()->run();
106+
$version = Soar::make()->withHelp(true)->setVersion(true)->run();
108107

109-
// Final run: '/.../bin/soar.darwin-arm64' '-only-syntax-check=true' '-query=SELECT * FRO `foo`;'
110-
$syntaxCheck = Soar::make()->withOnlySyntaxCheck(true)->withQuery('SELECT * FRO `foo`;')->dump()->run();
108+
// Final run: '/.../bin/soar.darwin-arm64' '-only-syntax-check=true' '-query=SELECT * FROM `foo`;'
109+
// $syntaxCheck = Soar::make()->withOnlySyntaxCheck(true)->withQuery('SELECT * FRO `foo`;')->run();
110+
$syntaxCheck = Soar::make()->withOnlySyntaxCheck(true)->withQuery('SELECT * FROM `foo`;')->run();
111111
```
112112
</details>
113113

@@ -534,6 +534,8 @@ guanguans ALL=(ALL) NOPASSWD: /Users/guanguans/Documents/develop/soar-php/bin/so
534534
```shell
535535
composer benchmark
536536
composer checks:required
537+
composer soar:example-run
538+
composer soar:example-serve
537539
composer test
538540
```
539541

composer.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,14 @@
428428
],
429429
"soar:dump-php-config-prototype": "Guanguans\\SoarPHP\\Support\\ComposerScripts::dumpSoarPHPConfig",
430430
"soar:dump-yaml-config": "Guanguans\\SoarPHP\\Support\\ComposerScripts::dumpSoarYamlConfig",
431+
"soar:example-run": [
432+
"echo 'examples/example.php:\n'",
433+
"@php examples/example.php"
434+
],
435+
"soar:example-serve": [
436+
"@composer-config:disable-process-timeout",
437+
"@php -S localhost:8123 examples/example.php"
438+
],
431439
"testbench": "@php vendor/bin/testbench --ansi",
432440
"testbench:build": "@testbench workbench:build",
433441
"testbench:clear": "@testbench package:purge-skeleton",

examples/example.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/** @noinspection DebugFunctionUsageInspection */
4+
/** @noinspection ForgottenDebugOutputInspection */
5+
/** @noinspection PhpUnhandledExceptionInspection */
6+
/** @noinspection SqlResolve */
7+
/** @noinspection SuspiciousAssignmentsInspection */
8+
9+
declare(strict_types=1);
10+
11+
/**
12+
* Copyright (c) 2019-2025 guanguans<[email protected]>
13+
*
14+
* For the full copyright and license information, please view
15+
* the LICENSE file that was distributed with this source code.
16+
*
17+
* @see https://github.com/guanguans/soar-php
18+
*/
19+
20+
require __DIR__.'/../vendor/autoload.php';
21+
22+
use Guanguans\SoarPHP\Soar;
23+
24+
$queries = [
25+
<<<'QUERY'
26+
SELECT
27+
DATE_FORMAT (t.last_update, '%Y-%m-%d'),
28+
COUNT(DISTINCT (t.city))
29+
FROM
30+
city t
31+
WHERE
32+
t.last_update > '2018-10-22 00:00:00'
33+
AND t.city LIKE '%Chrome%'
34+
AND t.city = 'eip'
35+
GROUP BY
36+
DATE_FORMAT(t.last_update, '%Y-%m-%d')
37+
ORDER BY
38+
DATE_FORMAT(t.last_update, '%Y-%m-%d');
39+
QUERY,
40+
'SELECT * FROM `foo`;',
41+
];
42+
43+
/**
44+
* Examples of scoring.
45+
*/
46+
$scores = Soar::make()->arrayScores($queries); // Basic scoring
47+
dump($scores);
48+
49+
$scores = Soar::make() // Advanced scoring
50+
->withTestDsn([
51+
'user' => 'you_user',
52+
'password' => 'you_password',
53+
'addr' => 'you_host:you_port',
54+
// 'host' => 'you_host',
55+
// 'port' => 'you_port',
56+
'schema' => 'you_dbname',
57+
// 'disable' => false,
58+
])
59+
->withOnlineDsn([
60+
'user' => 'you_user',
61+
'password' => 'you_password',
62+
// 'addr' => 'you_host:you_port',
63+
'host' => 'you_host',
64+
'port' => 'you_port',
65+
'schema' => 'you_dbname',
66+
'disable' => true,
67+
])
68+
->withExplain(true) // Enable EXPLAIN
69+
->withAllowOnlineAsTest(true) // Enable index suggestions
70+
->arrayScores($queries);
71+
72+
/**
73+
* Examples of running any soar command.
74+
*/
75+
// Final run: '/.../bin/soar.darwin-arm64' '-report-type=fingerprint' '-query=SELECT * FROM `foo`;'
76+
$fingerprint = Soar::make()->withReportType('fingerprint')->withQuery($queries[1])->run();
77+
78+
// Final run: '/.../bin/soar.darwin-arm64' '-report-type=pretty' '-query=SELECT * FROM `foo`;'
79+
$pretty = Soar::make()->withReportType('pretty')->withQuery($queries[1])->run();
80+
81+
// Final run: '/.../bin/soar.darwin-arm64' '-version=true'
82+
$version = Soar::make()->withHelp(true)->setVersion(true)->run();
83+
84+
// Final run: '/.../bin/soar.darwin-arm64' '-only-syntax-check=true' '-query=SELECT * FROM `foo`;'
85+
// $syntaxCheck = Soar::make()->withOnlySyntaxCheck(true)->withQuery('SELECT * FRO `foo`;')->run();
86+
$syntaxCheck = Soar::make()->withOnlySyntaxCheck(true)->withQuery('SELECT * FROM `foo`;')->run();
87+
88+
dump(
89+
// $scores,
90+
// $fingerprint,
91+
// $pretty,
92+
// $version,
93+
// $syntaxCheck
94+
);

0 commit comments

Comments
 (0)