Skip to content

Commit 1d30318

Browse files
committed
Merge branch 'review-container'
2 parents 6cbfdaa + 944fc6f commit 1d30318

File tree

8 files changed

+175
-37
lines changed

8 files changed

+175
-37
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,24 @@ The format is based on [Keep a Changelog][], and this project adheres to [Semant
1010
[Keep a Changelog]: https://keepachangelog.com/en/1.1.0/
1111
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html
1212

13+
## [v0.99.79] - 2025-03-31
14+
15+
### Added
16+
17+
#### `Container`
18+
19+
- Add and implement `ApplicationInterface` methods:
20+
- `hasOutputLog()`
21+
- `hasHarRecorder()`
22+
- `hasCache()`
23+
- `hasSync()`
24+
25+
### Changed
26+
27+
#### `Container`
28+
29+
- Rename `ApplicationInterface` method `exportHar()` to `recordHar()`
30+
1331
## [v0.99.78] - 2025-03-28
1432

1533
### Added
@@ -4917,6 +4935,7 @@ This is the final release of `lkrms/util`. It is moving to [Salient](https://git
49174935

49184936
- Allow `CliOption` value names to contain arbitrary characters
49194937

4938+
[v0.99.79]: https://github.com/salient-labs/toolkit/compare/v0.99.78...v0.99.79
49204939
[v0.99.78]: https://github.com/salient-labs/toolkit/compare/v0.99.77...v0.99.78
49214940
[v0.99.77]: https://github.com/salient-labs/toolkit/compare/v0.99.76...v0.99.77
49224941
[v0.99.76]: https://github.com/salient-labs/toolkit/compare/v0.99.75...v0.99.76

src/Toolkit/Container/Application.php

Lines changed: 65 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,14 @@ public function isRunningInProduction(): bool
394394
));
395395
}
396396

397+
/**
398+
* @inheritDoc
399+
*/
400+
public function hasOutputLog(): bool
401+
{
402+
return $this->OutputLogIsRegistered;
403+
}
404+
397405
/**
398406
* @inheritDoc
399407
*/
@@ -416,7 +424,15 @@ public function logOutput(?string $name = null, ?bool $debug = null)
416424
/**
417425
* @inheritDoc
418426
*/
419-
public function exportHar(
427+
public function hasHarRecorder(): bool
428+
{
429+
return $this->HarListenerId !== null || $this->HarRecorder;
430+
}
431+
432+
/**
433+
* @inheritDoc
434+
*/
435+
public function recordHar(
420436
?string $name = null,
421437
?string $creatorName = null,
422438
?string $creatorVersion = null,
@@ -487,6 +503,16 @@ public function getHarFilename(): ?string
487503
: null;
488504
}
489505

506+
/**
507+
* @inheritDoc
508+
*/
509+
public function hasCache(): bool
510+
{
511+
return $this->CacheStore || (
512+
Cache::isLoaded() && $this->globalCacheIsValid()
513+
);
514+
}
515+
490516
/**
491517
* @inheritDoc
492518
*/
@@ -496,16 +522,12 @@ public function startCache()
496522
throw new LogicException('Cache already started');
497523
}
498524
if (Cache::isLoaded()) {
499-
$cache = Cache::getInstance();
500-
if (
501-
$cache instanceof CacheStore
502-
&& File::same($this->getCacheDb(false), $file = $cache->getFilename())
503-
) {
525+
if ($this->globalCacheIsValid($name)) {
504526
return $this;
505527
}
506528
throw new LogicException(sprintf(
507529
'Global cache already started: %s',
508-
$file ?? get_class($cache),
530+
$name,
509531
));
510532
}
511533
$this->CacheStore = new CacheStore($this->getCacheDb());
@@ -540,11 +562,33 @@ public function stopCache()
540562
return $this;
541563
}
542564

565+
/**
566+
* @param-out string $name
567+
*/
568+
private function globalCacheIsValid(?string &$name = null): bool
569+
{
570+
$cache = Cache::getInstance();
571+
$isValid = $cache instanceof CacheStore
572+
&& File::same($this->getCacheDb(false), $file = $cache->getFilename());
573+
$name = $file ?? get_class($cache);
574+
return $isValid;
575+
}
576+
543577
private function getCacheDb(bool $create = true): string
544578
{
545579
return $this->getCachePath($create) . '/cache.db';
546580
}
547581

582+
/**
583+
* @inheritDoc
584+
*/
585+
public function hasSync(): bool
586+
{
587+
return $this->SyncStore || (
588+
Sync::isLoaded() && $this->globalSyncIsValid()
589+
);
590+
}
591+
548592
/**
549593
* @param string[]|null $arguments
550594
*/
@@ -554,16 +598,12 @@ public function startSync(?string $command = null, ?array $arguments = null)
554598
throw new LogicException('Sync entity store already started');
555599
}
556600
if (Sync::isLoaded()) {
557-
$store = Sync::getInstance();
558-
if (
559-
$store instanceof SyncStore
560-
&& File::same($this->getSyncDb(false), $file = $store->getFilename())
561-
) {
601+
if ($this->globalSyncIsValid($name)) {
562602
return $this;
563603
}
564604
throw new LogicException(sprintf(
565605
'Global sync entity store already started: %s',
566-
$file ?? get_class($store),
606+
$name,
567607
));
568608
}
569609
if ($arguments === null && $command === null) {
@@ -615,6 +655,18 @@ public function sync(
615655
return $this;
616656
}
617657

658+
/**
659+
* @param-out string $name
660+
*/
661+
private function globalSyncIsValid(?string &$name = null): bool
662+
{
663+
$store = Sync::getInstance();
664+
$isValid = $store instanceof SyncStore
665+
&& File::same($this->getSyncDb(false), $file = $store->getFilename());
666+
$name = $file ?? get_class($store);
667+
return $isValid;
668+
}
669+
618670
private function getSyncDb(bool $create = true): string
619671
{
620672
return $this->getDataPath($create) . '/sync.db';

src/Toolkit/Contract/Container/ApplicationInterface.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ public function getTempPath(): string;
6666
*/
6767
public function isRunningInProduction(): bool;
6868

69+
/**
70+
* Check if console output is being logged to the application's log
71+
* directory
72+
*/
73+
public function hasOutputLog(): bool;
74+
6975
/**
7076
* Log console output to the application's log directory
7177
*
@@ -83,7 +89,15 @@ public function isRunningInProduction(): bool;
8389
public function logOutput(?string $name = null, ?bool $debug = null);
8490

8591
/**
86-
* Export the application's HTTP requests to an HTTP Archive (HAR) file in
92+
* Check if the application's HTTP requests are being recorded
93+
*
94+
* Returns `true` if {@see recordHar()} has been called, even if no
95+
* subsequent HTTP requests have been made.
96+
*/
97+
public function hasHarRecorder(): bool;
98+
99+
/**
100+
* Record the application's HTTP requests to an HTTP Archive (HAR) file in
87101
* its log directory
88102
*
89103
* If any HTTP requests are made via {@see CurlerInterface} implementations,
@@ -94,7 +108,7 @@ public function logOutput(?string $name = null, ?bool $debug = null);
94108
* @return $this
95109
* @throws LogicException if HTTP requests are already being recorded.
96110
*/
97-
public function exportHar(
111+
public function recordHar(
98112
?string $name = null,
99113
?string $creatorName = null,
100114
?string $creatorVersion = null,
@@ -104,20 +118,27 @@ public function exportHar(
104118
/**
105119
* Get the name of the application's HTTP Archive (HAR) file if it exists
106120
*
107-
* Returns `null` if {@see exportHar()} has been called but no HTTP requests
121+
* Returns `null` if {@see recordHar()} has been called but no HTTP requests
108122
* have been made.
109123
*
110124
* @throws LogicException if HTTP requests are not being recorded.
111125
*/
112126
public function getHarFilename(): ?string;
113127

128+
/**
129+
* Check if a cache has been started for the application
130+
*/
131+
public function hasCache(): bool;
132+
114133
/**
115134
* Start a cache for the application and make it the global cache
116135
*
117136
* If the cache is filesystem-backed, it is started in the application's
118137
* cache directory.
119138
*
120139
* @return $this
140+
* @throws LogicException if a cache has already been started for the
141+
* application.
121142
*/
122143
public function startCache();
123144

@@ -128,6 +149,11 @@ public function startCache();
128149
*/
129150
public function stopCache();
130151

152+
/**
153+
* Check if a sync entity store has been started for the application
154+
*/
155+
public function hasSync(): bool;
156+
131157
/**
132158
* Start a sync entity store for the application and make it the global sync
133159
* entity store
@@ -136,6 +162,8 @@ public function stopCache();
136162
* application's data directory.
137163
*
138164
* @return $this
165+
* @throws LogicException if a sync entity store has already been started
166+
* for the application.
139167
*/
140168
public function startSync();
141169

src/Toolkit/Sync/Command/AbstractSyncCommand.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
*/
2626
abstract class AbstractSyncCommand extends CliCommand
2727
{
28-
protected bool $ExportHar = false;
28+
protected bool $RecordHar = false;
2929

3030
// --
3131

@@ -147,17 +147,17 @@ protected function getGlobalOptionList(): iterable
147147
CliOption::build()
148148
->long('har')
149149
->short('H')
150-
->description('Export HTTP requests to an HTTP Archive file in the log directory')
151-
->bindTo($this->ExportHar),
150+
->description('Record HTTP requests to an HTTP Archive file in the log directory')
151+
->bindTo($this->RecordHar),
152152
];
153153
}
154154

155155
protected function startRun(): void
156156
{
157157
Console::registerStderrTarget();
158158

159-
if ($this->ExportHar) {
160-
$this->App->exportHar();
159+
if ($this->RecordHar) {
160+
$this->App->recordHar();
161161
}
162162
}
163163

0 commit comments

Comments
 (0)