1: <?php declare(strict_types=1);
2:
3: namespace Salient\Contract\Container;
4:
5: use Salient\Contract\Core\MessageLevel as Level;
6: use Salient\Contract\Curler\CurlerInterface;
7: use Salient\Contract\Sync\SyncNamespaceHelperInterface;
8: use Salient\Contract\Sync\SyncStoreInterface;
9: use Closure;
10: use LogicException;
11:
12: /**
13: * A service container for applications
14: *
15: * @api
16: */
17: interface ApplicationInterface extends ContainerInterface
18: {
19: /**
20: * Get the name of the application
21: */
22: public function getAppName(): string;
23:
24: /**
25: * Check if the application is running in a production environment
26: *
27: * Returns `true` if:
28: *
29: * - the name of the current environment is `"production"`
30: * - the application is running from a Phar archive, or
31: * - the application was installed with `composer --no-dev`
32: */
33: public function isProduction(): bool;
34:
35: /**
36: * Get the application's base path
37: */
38: public function getBasePath(): string;
39:
40: /**
41: * Get a writable cache directory for the application
42: *
43: * Appropriate for replaceable data that should persist between runs to
44: * improve performance.
45: */
46: public function getCachePath(): string;
47:
48: /**
49: * Get a writable directory for configuration files created by the
50: * application
51: */
52: public function getConfigPath(): string;
53:
54: /**
55: * Get a writable data directory for the application
56: *
57: * Appropriate for critical data that should persist indefinitely.
58: */
59: public function getDataPath(): string;
60:
61: /**
62: * Get a writable directory for the application's log files
63: */
64: public function getLogPath(): string;
65:
66: /**
67: * Get a writable directory for the application's ephemeral data
68: */
69: public function getTempPath(): string;
70:
71: /**
72: * Log console output to the application's log directory
73: *
74: * Messages with levels between {@see Level::EMERGENCY} and
75: * {@see Level::INFO} are written to `<name>.log`.
76: *
77: * If `$debug` is `true`, or `$debug` is `null` and debug mode is enabled in
78: * the environment, messages with levels between {@see Level::EMERGENCY} and
79: * {@see Level::DEBUG} are simultaneously written to `<name>.debug.log`.
80: *
81: * @param string|null $name If `null`, the name of the application is used.
82: * @return $this
83: */
84: public function logOutput(?string $name = null, ?bool $debug = null);
85:
86: /**
87: * Export the application's HTTP requests to an HTTP Archive (HAR) file in
88: * its log directory
89: *
90: * If any requests are made via {@see CurlerInterface} objects,
91: * `<name>-<timestamp>-<uuid>.har` is created to record them.
92: *
93: * @param string|null $name If `null`, the name of the application is used.
94: * @param (Closure(): string)|string|null $uuid
95: * @return $this
96: * @throws LogicException if HTTP requests are already being recorded.
97: */
98: public function exportHar(
99: ?string $name = null,
100: ?string $creatorName = null,
101: ?string $creatorVersion = null,
102: $uuid = null
103: );
104:
105: /**
106: * Get the name of the HTTP Archive (HAR) file created via exportHar() if it
107: * has been created
108: *
109: * @throws LogicException if HTTP requests are not being recorded.
110: */
111: public function getHarFilename(): ?string;
112:
113: /**
114: * Start a cache store and make it the global cache
115: *
116: * If the cache store is filesystem-backed, the application's cache
117: * directory is used.
118: *
119: * @return $this
120: */
121: public function startCache();
122:
123: /**
124: * Start a cache store and make it the global cache if a previously started
125: * cache store exists, otherwise do nothing
126: *
127: * @return $this
128: */
129: public function resumeCache();
130:
131: /**
132: * Stop a cache store started by startCache() or resumeCache()
133: *
134: * @return $this
135: */
136: public function stopCache();
137:
138: /**
139: * Start an entity store and make it the global sync entity store
140: *
141: * If the entity store is filesystem-backed, the application's data
142: * directory is used.
143: *
144: * @param mixed[]|null $arguments
145: * @return $this
146: */
147: public function startSync(?string $command = null, ?array $arguments = null);
148:
149: /**
150: * Stop an entity store started by startSync()
151: *
152: * @return $this
153: */
154: public function stopSync();
155:
156: /**
157: * Register a namespace for sync entities and their provider interfaces with
158: * the global sync entity store
159: *
160: * @see SyncStoreInterface::registerNamespace()
161: *
162: * @return $this
163: * @throws LogicException if the global sync entity store is not loaded.
164: */
165: public function registerSyncNamespace(
166: string $prefix,
167: string $uri,
168: string $namespace,
169: ?SyncNamespaceHelperInterface $helper = null
170: );
171:
172: /**
173: * Get the application's working directory
174: *
175: * The application's working directory is either the directory it was
176: * started in, or the directory most recently set via
177: * {@see ApplicationInterface::setWorkingDirectory()}.
178: */
179: public function getWorkingDirectory(): string;
180:
181: /**
182: * Change to the application's working directory
183: *
184: * @return $this
185: */
186: public function restoreWorkingDirectory();
187:
188: /**
189: * Set the application's working directory
190: *
191: * @param string|null $directory If `null`, the current working directory is
192: * used.
193: * @return $this
194: */
195: public function setWorkingDirectory(?string $directory = null);
196:
197: /**
198: * Print a summary of the application's runtime performance metrics and
199: * system resource usage when it terminates
200: *
201: * @param Level::* $level
202: * @param string[]|string|null $groups If `null` or `["*"]`, all metrics are
203: * reported, otherwise only metrics in the given groups are reported.
204: * @return $this
205: */
206: public function registerShutdownReport(
207: int $level = Level::INFO,
208: bool $includeResourceUsage = true,
209: bool $includeRunningTimers = true,
210: $groups = null,
211: ?int $limit = 10
212: );
213:
214: /**
215: * Print a summary of the application's system resource usage
216: *
217: * @param Level::* $level
218: * @return $this
219: */
220: public function reportResourceUsage(int $level = Level::INFO);
221:
222: /**
223: * Print a summary of the application's runtime performance metrics
224: *
225: * @param Level::* $level
226: * @param string[]|string|null $groups If `null` or `["*"]`, all metrics are
227: * reported, otherwise only metrics in the given groups are reported.
228: * @return $this
229: */
230: public function reportMetrics(
231: int $level = Level::INFO,
232: bool $includeRunningTimers = true,
233: $groups = null,
234: ?int $limit = 10
235: );
236: }
237: