-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentry.php
More file actions
87 lines (74 loc) · 2.89 KB
/
Copy pathentry.php
File metadata and controls
87 lines (74 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
use PHPModules\Demo\Todo::TaskList;
use PHPModules\Demo\Todo::Task;
use PHPModules\Demo\Todo::ListRepository;
use PHPModules\Demo\Todo::Renderable;
use PHPModules\Demo\Todo::ListError;
require __DIR__ . "/vendor/autoload.php";
echo "=== Todo Module Demo ===\n\n";
// ---- 1. Public API - create lists, tasks, and persist ----
echo "1. Public API — create and save a todo list:\n";
$repo = new ListRepository();
$list = new TaskList('Shopping');
$list->addTask(new Task('Buy milk'));
$list->addTask(new Task('Buy eggs'));
$list->addTask(new Task('Buy bread'));
$repo->save($list);
$found = $repo->findById($list->id());
printf(" List: %s (%d tasks)\n", $found->title(), count($found->tasks()));
printf(" Created at: %d\n\n", $found->createdAt());
// ---- 2. internal(set) - read allowed, write denied ----
echo "2. internal(set) on Task::\$completed:\n";
$task = $found->tasks()[0];
$task->complete();
printf(" Read completed: %s\n", $task->completed ? 'true' : 'false');
try {
$task->completed = false;
echo " Write: OK (unexpected)\n";
} catch (\Error $e) {
printf(" Write: DENIED — %s\n\n", $e->getMessage());
}
// ---- 3. internal method - denied from outside ----
echo "3. TaskList::reorderTasks() — internal method:\n";
try {
$found->reorderTasks(['x', 'y']);
} catch (\Error $e) {
printf(" DENIED — %s\n\n", $e->getMessage());
}
// ---- 4. Extend a module class from outside ----
echo "4. Extend Todo::Task from outside:\n";
class PriorityTask extends Task {
public function __construct(
string $title,
readonly int $priority = 0,
) {
parent::__construct($title);
}
}
$urgent = new PriorityTask('Urgent', 5);
printf(" PriorityTask: \"%s\" (priority %d)\n\n", $urgent->title(), $urgent->priority);
// ---- 5. Implement a module interface from outside ----
echo "5. Implement Todo::Renderable from outside:\n";
class JsonExport implements Renderable {
public function __construct(private array $data) {}
public function render(): string {
return json_encode($this->data, JSON_PRETTY_PRINT);
}
}
$export = new JsonExport(['task' => $urgent->title(), 'done' => $urgent->completed]);
printf(" %s\n\n", $export->render());
// ---- 6. Public const accessible from outside ----
echo "6. Public module const:\n";
printf(" MAX_ITEMS_PER_LIST = %d\n\n", \PHPModules\Demo\Todo::MAX_ITEMS_PER_LIST);
// ---- 7. Nested Storage module — internal classes denied ----
echo "7. Todo::Storage — internal nested module class:\n";
try {
new \PHPModules\Demo\Todo::Storage\ListMapper();
} catch (\Error $e) {
printf(" DENIED — %s\n\n", $e->getMessage());
}
// ---- 8. instanceof observability — catch by type ----
echo "8. instanceof — observability across boundary:\n";
$error = ListError::NotFound;
printf(" \$error instanceof ListError: %s\n", $error instanceof ListError ? 'true' : 'false');
echo "\n=== Demo Complete ===\n";