Skip to content

Commit 1b2d84b

Browse files
authored
Merge pull request #13 from micilini/hotfix-1.0.1-example-autoload
fix examples autoload when installed through composer
2 parents 2eb7180 + 781b49e commit 1b2d84b

6 files changed

Lines changed: 108 additions & 3 deletions

File tree

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ ws://127.0.0.1:8080
145145

146146
## Running the Examples
147147

148+
> The example servers can be executed both from a cloned repository and from a project where the package was installed through Composer. The examples use `examples/bootstrap.php` to locate the correct Composer autoload file automatically.
149+
148150
Each example has two parts:
149151

150152
1. a WebSocket server process;
@@ -164,6 +166,13 @@ Open another terminal and serve the UI:
164166
php -S 127.0.0.1:8000 -t examples/easy-chat/public
165167
```
166168

169+
If you installed the package inside another project with Composer, run:
170+
171+
```bash
172+
php vendor/micilini/php-websockets/examples/easy-chat/server.php
173+
php -S 127.0.0.1:8000 -t vendor/micilini/php-websockets/examples/easy-chat/public
174+
```
175+
167176
Open:
168177

169178
```txt
@@ -196,6 +205,13 @@ Open another terminal and serve the UI:
196205
php -S 127.0.0.1:8001 -t examples/medium-chat/public
197206
```
198207

208+
If you installed the package inside another project with Composer, run:
209+
210+
```bash
211+
php vendor/micilini/php-websockets/examples/medium-chat/server.php
212+
php -S 127.0.0.1:8001 -t vendor/micilini/php-websockets/examples/medium-chat/public
213+
```
214+
199215
Open:
200216

201217
```txt
@@ -226,6 +242,13 @@ Open another terminal and serve the UI:
226242
php -S 127.0.0.1:8002 -t examples/private-chat/public
227243
```
228244

245+
If you installed the package inside another project with Composer, run:
246+
247+
```bash
248+
php vendor/micilini/php-websockets/examples/private-chat/server.php
249+
php -S 127.0.0.1:8002 -t vendor/micilini/php-websockets/examples/private-chat/public
250+
```
251+
229252
Open:
230253

231254
```txt

examples/bootstrap.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
$autoloadCandidates = [
6+
// Repository clone:
7+
// php-websockets/vendor/autoload.php
8+
__DIR__ . '/../vendor/autoload.php',
9+
10+
// Composer package install:
11+
// project/vendor/micilini/php-websockets/examples/../../../autoload.php
12+
__DIR__ . '/../../../autoload.php',
13+
14+
// Fallback when executed from the consumer project root:
15+
// project/vendor/autoload.php
16+
getcwd() . '/vendor/autoload.php',
17+
];
18+
19+
foreach ($autoloadCandidates as $autoload) {
20+
if (is_file($autoload)) {
21+
require_once $autoload;
22+
23+
return;
24+
}
25+
}
26+
27+
throw new RuntimeException(
28+
'Composer autoload file was not found. '
29+
. 'Run "composer install" in the repository root, '
30+
. 'or install micilini/php-websockets through Composer before running the examples.'
31+
);

examples/easy-chat/server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
require __DIR__ . '/../../vendor/autoload.php';
5+
require __DIR__ . '/../bootstrap.php';
66

77
use Micilini\PhpSockets\Chat\ChatServer;
88
use Micilini\PhpSockets\Config\ChatConfig;

examples/medium-chat/server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
require __DIR__ . '/../../vendor/autoload.php';
5+
require __DIR__ . '/../bootstrap.php';
66

77
use Micilini\PhpSockets\Chat\ChatMessage;
88
use Micilini\PhpSockets\Chat\ChatServer;

examples/private-chat/server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
require __DIR__ . '/../../vendor/autoload.php';
5+
require __DIR__ . '/../bootstrap.php';
66
require __DIR__ . '/bots/EchoBot.php';
77
require __DIR__ . '/bots/HelpBot.php';
88

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Micilini\PhpSockets\Tests\Unit\Examples;
6+
7+
use PHPUnit\Framework\TestCase;
8+
9+
final class ExampleServerAutoloadTest extends TestCase
10+
{
11+
public function testExamplesHaveSharedBootstrapFile(): void
12+
{
13+
self::assertFileExists(__DIR__ . '/../../../examples/bootstrap.php');
14+
}
15+
16+
public function testExampleServersUseSharedBootstrapInsteadOfLocalVendorAutoload(): void
17+
{
18+
$serverFiles = [
19+
__DIR__ . '/../../../examples/easy-chat/server.php',
20+
__DIR__ . '/../../../examples/medium-chat/server.php',
21+
__DIR__ . '/../../../examples/private-chat/server.php',
22+
];
23+
24+
foreach ($serverFiles as $serverFile) {
25+
self::assertFileExists($serverFile);
26+
27+
$contents = (string) file_get_contents($serverFile);
28+
29+
self::assertStringContainsString(
30+
"require __DIR__ . '/../bootstrap.php';",
31+
$contents,
32+
"{$serverFile} should use the shared examples bootstrap.",
33+
);
34+
35+
self::assertStringNotContainsString(
36+
'/../../vendor/autoload.php',
37+
$contents,
38+
"{$serverFile} should not assume a local package vendor directory.",
39+
);
40+
}
41+
}
42+
43+
public function testSharedBootstrapSupportsRepositoryAndComposerInstallPaths(): void
44+
{
45+
$bootstrap = (string) file_get_contents(__DIR__ . '/../../../examples/bootstrap.php');
46+
47+
self::assertStringContainsString("__DIR__ . '/../vendor/autoload.php'", $bootstrap);
48+
self::assertStringContainsString("__DIR__ . '/../../../autoload.php'", $bootstrap);
49+
self::assertStringContainsString("getcwd() . '/vendor/autoload.php'", $bootstrap);
50+
}
51+
}

0 commit comments

Comments
 (0)