-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathApp.php
1295 lines (1092 loc) · 42.5 KB
/
App.php
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
declare(strict_types=1);
namespace Atk4\Ui;
use Atk4\Core\AppScopeTrait;
use Atk4\Core\DiContainerTrait;
use Atk4\Core\DynamicMethodTrait;
use Atk4\Core\ExceptionRenderer;
use Atk4\Core\Factory;
use Atk4\Core\HookTrait;
use Atk4\Core\TraitUtil;
use Atk4\Core\WarnDynamicPropertyTrait;
use Atk4\Data\Persistence;
use Atk4\Ui\Exception\ExitApplicationError;
use Atk4\Ui\Exception\LateOutputError;
use Atk4\Ui\Exception\UnhandledCallbackExceptionError;
use Atk4\Ui\Js\Jquery;
use Atk4\Ui\Js\JsExpression;
use Atk4\Ui\Js\JsExpressionable;
use Atk4\Ui\Js\JsFunction;
use Atk4\Ui\Persistence\Ui as UiPersistence;
use Atk4\Ui\UserAction\ExecutorFactory;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Response;
use Nyholm\Psr7Server\ServerRequestCreator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation;
class App
{
use AppScopeTrait;
use DiContainerTrait;
use DynamicMethodTrait;
use HookTrait;
private const UNHANDLEABLE_ERROR_LEVELS = \E_ERROR | \E_PARSE | \E_CORE_ERROR | \E_CORE_WARNING | \E_COMPILE_ERROR | \E_COMPILE_WARNING;
private const UNSUPPRESSIBLE_ERROR_LEVELS = \PHP_MAJOR_VERSION >= 8 ? (\E_ERROR | \E_PARSE | \E_CORE_ERROR | \E_COMPILE_ERROR | \E_USER_ERROR | \E_RECOVERABLE_ERROR) : 0;
public const HOOK_BEFORE_EXIT = self::class . '@beforeExit';
public const HOOK_BEFORE_RENDER = self::class . '@beforeRender';
private static ?string $shutdownReservedMemory; // @phpstan-ignore property.onlyRead
private static ?int $errorReportingLevel = null;
/** @var array<string, string> Location where to load JS/CSS files */
public array $cdn = [
'atk' => '/public',
'jquery' => '/public/external/jquery/dist',
'fomantic-ui' => '/public/external/fomantic-ui/dist',
'flatpickr' => '/public/external/flatpickr/dist',
'highlight.js' => '/public/external/@highlightjs/cdn-assets',
'chart.js' => '/public/external/chart.js/dist', // for atk4/chart
];
/** @var ExecutorFactory App wide executor factory object for Model user action. */
protected $executorFactory;
/**
* @var string Version of Agile UI
*
* @TODO remove, no longer needed for CDN versioning as we bundle all resources
*/
public $version = '6.0-dev';
/** @var string Name of application */
public $title = 'Agile UI - Untitled Application';
/** @var Layout the top-most view object */
public $layout;
/** @var string|list<string> Set one or more directories where templates should reside. */
public $templateDir;
/** @var bool Will replace an exception handler with our own, that will output errors nicely. */
public $catchExceptions = true;
/** Will display error if callback wasn't triggered. */
protected bool $catchRunawayCallbacks = true;
/** Will always run application even if developer didn't explicitly executed run();. */
protected bool $alwaysRun = true;
/** Will be set to true after app->run() is called, which may be done automatically on exit. */
public bool $runCalled = false;
/** Will be set to true after exit is called. */
private bool $exitCalled = false;
public bool $isRendering = false;
/** @var UiPersistence */
public $uiPersistence;
/** @var View|null For internal use */
public $html;
/** @var LoggerInterface|null Target for objects with DebugTrait */
public $logger;
/** @var Persistence|Persistence\Sql */
public $db;
/** @var App\SessionManager */
public $session;
private ServerRequestInterface $request;
private ResponseInterface $response;
/**
* If filename path part is missing during building of URL, this page will be used.
* Set to empty string when when your webserver supports index.php autoindex or you use mod_rewrite with routing.
*
* @internal only for self::url() method
*/
protected string $urlBuildingIndexPage = 'index';
/**
* Remove and re-add the extension of the file during parsing requests and building URL.
*
* @internal only for self::url() method
*/
protected string $urlBuildingExt = '.php';
/** @var bool Call exit in place of throw Exception when Application need to exit. */
public $callExit = true;
/** @var array<string, bool> global sticky arguments */
protected array $stickyGetArguments = [
'__atk_json' => false,
];
/** @var class-string */
public $templateClass = HtmlTemplate::class;
/**
* @param array<string, mixed> $defaults
*/
public function __construct(array $defaults = [])
{
if (isset($defaults['request'])) {
$this->request = $defaults['request'];
unset($defaults['request']);
} else {
$requestFactory = new Psr17Factory();
$requestCreator = new ServerRequestCreator($requestFactory, $requestFactory, $requestFactory, $requestFactory);
$noGlobals = [];
foreach (['_GET', '_COOKIE', '_FILES'] as $k) {
if (!array_key_exists($k, $GLOBALS)) {
$noGlobals[] = $k;
$GLOBALS[$k] = [];
}
}
try {
$this->request = $requestCreator->fromGlobals();
} finally {
foreach ($noGlobals as $k) {
unset($GLOBALS[$k]);
}
}
}
if (isset($defaults['response'])) {
$this->response = $defaults['response'];
unset($defaults['response']);
} else {
$this->response = new Response();
}
// disable caching by default
$this->setResponseHeader('Cache-Control', 'no-store');
$this->setApp($this);
$this->setDefaults($defaults);
$this->setupTemplateDirs();
foreach ($this->cdn as $k => $v) {
if (str_starts_with($v, '/') && !str_starts_with($v, '//')) {
$this->cdn[$k] = $this->createRequestPathFromLocalPath(__DIR__ . '/..' . $v);
}
}
// set our exception handler
if ($this->catchExceptions) {
set_exception_handler(\Closure::fromCallable([$this, 'caughtException']));
$createErrorExceptionArgsFx = static function (int $severity, string $msg, string $file, int $line) {
return [$msg, 0, $severity, $file, $line];
};
set_error_handler(function (int $severity, string $msg, string $file, int $line) use ($createErrorExceptionArgsFx): bool {
if ((error_reporting() & ~self::UNSUPPRESSIBLE_ERROR_LEVELS) === 0) {
$isFirstFrame = true;
foreach (array_slice(debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 10), 1) as $frame) {
// allow to suppress any warning outside Atk4
if ($isFirstFrame) {
$isFirstFrame = false;
if (!isset($frame['class']) || !str_starts_with($frame['class'], 'Atk4\\')) {
return false;
}
}
// allow to suppress undefined property warning
if (isset($frame['class']) && TraitUtil::hasTrait($frame['class'], WarnDynamicPropertyTrait::class)
&& $frame['function'] === 'warnPropertyDoesNotExist') {
return false;
}
}
}
$this->throwOrCaughtExceptionIfInShutdown(new \ErrorException(...$createErrorExceptionArgsFx($severity, $msg, $file, $line)));
});
register_shutdown_function(function () use ($createErrorExceptionArgsFx): void {
$error = error_get_last();
if ($error !== null && ($error['type'] & self::UNHANDLEABLE_ERROR_LEVELS) !== 0) {
$this->throwOrCaughtExceptionIfInShutdown(new \ErrorException(...$createErrorExceptionArgsFx($error['type'], $error['message'], $error['file'], $error['line'])));
}
});
self::$errorReportingLevel = error_reporting();
error_reporting(self::$errorReportingLevel & ~self::UNHANDLEABLE_ERROR_LEVELS);
http_response_code(500);
header('Content-Type: text/plain');
header('Cache-Control: no-store');
}
// always run app on shutdown
if ($this->alwaysRun) {
$this->setupAlwaysRun();
}
if ($this->uiPersistence === null) {
$this->uiPersistence = new UiPersistence();
}
if (!str_starts_with($this->getRequest()->getUri()->getPath(), '/')) {
throw (new Exception('Request URL path must always start with \'/\''))
->addMoreInfo('url', (string) $this->getRequest()->getUri());
}
if ($this->session === null) {
$this->session = new App\SessionManager();
}
// setting up default executor factory
$this->executorFactory = Factory::factory([ExecutorFactory::class]);
}
public function setExecutorFactory(ExecutorFactory $factory): void
{
$this->executorFactory = $factory;
}
public function getExecutorFactory(): ExecutorFactory
{
return $this->executorFactory;
}
protected function setupTemplateDirs(): void
{
if ($this->templateDir === null) {
$this->templateDir = [];
} elseif (!is_array($this->templateDir)) {
$this->templateDir = [$this->templateDir];
}
$this->templateDir[] = dirname(__DIR__) . '/template';
}
/**
* @return ($calledFromShutdownHandler is true ? void : never)
*/
public function callExit(bool $calledFromShutdownHandler = false): void
{
if (!$this->exitCalled) {
$this->exitCalled = true;
$this->hook(self::HOOK_BEFORE_EXIT);
}
if (!$calledFromShutdownHandler) {
if (!$this->callExit) {
// case process is not in shutdown mode
// App as already done everything
// App need to stop output
// set_handler to catch/trap any exception
set_exception_handler(static function (\Throwable $t): void {});
// raise exception to be trapped and stop execution
throw new ExitApplicationError();
}
exit;
}
}
/**
* @return never
*/
private function throwOrCaughtExceptionIfInShutdown(\Throwable $exception): void
{
if (\PHP_VERSION_ID < 80300 && self::$shutdownReservedMemory === null) {
// remove method once PHP 8.2 support is dropped
// https://github.com/php/php-src/issues/10695
$this->caughtException($exception);
exit(1);
}
throw $exception;
}
protected function caughtException(\Throwable $exception): void
{
if ($exception instanceof LateOutputError) {
$this->outputLateOutputError($exception);
}
while ($exception instanceof UnhandledCallbackExceptionError) {
$exception = $exception->getPrevious();
}
$this->catchRunawayCallbacks = false;
// just replace layout to avoid any extended App->_construct problems
// it will maintain everything as in the original app StickyGet, logger, Events
$this->html = null;
$this->initLayout([Layout\Centered::class]);
$this->layout->template->dangerouslySetHtml('Content', $this->renderExceptionHtml($exception));
$this->layout->template->tryDel('Header');
$this->setResponseHeader('Cache-Control', 'no-store');
if ($this->isJsUrlRequest() || $this->getRequest()->getHeaderLine('X-Requested-With') === 'XMLHttpRequest') {
$this->outputResponseJson([
'success' => false,
'message' => $this->layout->getHtml(),
]);
} else {
$this->setResponseStatusCode(500);
$this->run();
}
// process is already in shutdown because of uncaught exception
$this->callExit(true);
}
public function getRequest(): ServerRequestInterface
{
return $this->request;
}
/**
* Check if a specific GET parameter exists in the HTTP request.
*/
public function hasRequestQueryParam(string $key): bool
{
return $this->tryGetRequestQueryParam($key) !== null;
}
/**
* Try to get the value of a specific GET parameter from the HTTP request.
*/
public function tryGetRequestQueryParam(string $key): ?string
{
return $this->getRequest()->getQueryParams()[$key] ?? null;
}
/**
* Get the value of a specific GET parameter from the HTTP request.
*/
public function getRequestQueryParam(string $key): string
{
$res = $this->tryGetRequestQueryParam($key);
if ($res === null) {
throw (new Exception('GET param does not exist'))
->addMoreInfo('key', $key);
}
return $res;
}
/**
* Check if a specific POST parameter exists in the HTTP request.
*/
public function hasRequestPostParam(string $key): bool
{
return $this->tryGetRequestPostParam($key) !== null;
}
/**
* Try to get the value of a specific POST parameter from the HTTP request.
*/
public function tryGetRequestPostParam(string $key): ?string
{
return $this->getRequest()->getParsedBody()[$key] ?? null;
}
/**
* Get the value of a specific POST parameter from the HTTP request.
*/
public function getRequestPostParam(string $key): string
{
$res = $this->tryGetRequestPostParam($key);
if ($res === null) {
throw (new Exception('POST param does not exist'))
->addMoreInfo('key', $key);
}
return $res;
}
/**
* Check if a specific uploaded file exists in the HTTP request.
*/
public function hasRequestUploadedFile(string $key): bool
{
return $this->tryGetRequestUploadedFile($key) !== null;
}
/**
* Try to get a specific uploaded file from the HTTP request.
*/
public function tryGetRequestUploadedFile(string $key): ?UploadedFileInterface
{
return $this->getRequest()->getUploadedFiles()[$key] ?? null;
}
/**
* Get a specific uploaded file from the HTTP request.
*/
public function getRequestUploadedFile(string $key): UploadedFileInterface
{
$res = $this->tryGetRequestUploadedFile($key);
if ($res === null) {
throw (new Exception('FILES upload does not exist'))
->addMoreInfo('key', $key);
}
return $res;
}
public function getResponse(): ResponseInterface
{
return $this->response;
}
protected function assertHeadersNotSent(): void
{
if (headers_sent()
&& \PHP_SAPI !== 'cli' // for phpunit
&& $this->response->getHeaderLine('Content-Type') !== 'text/event-stream' // for SSE
) {
$lateError = new LateOutputError('Headers already sent, more headers cannot be set at this stage');
if ($this->catchExceptions) {
$this->caughtException($lateError);
$this->outputLateOutputError($lateError);
}
throw $lateError;
}
}
/**
* @return $this
*/
public function setResponseStatusCode(int $statusCode): self
{
$this->assertHeadersNotSent();
$this->response = $this->response->withStatus($statusCode);
return $this;
}
/**
* @return $this
*/
public function setResponseHeader(string $name, string $value): self
{
$this->assertHeadersNotSent();
if ($value === '') {
$this->response = $this->response->withoutHeader($name);
} else {
$name = preg_replace_callback('~(?<![a-zA-Z])[a-z]~', static function ($matches) {
return strtoupper($matches[0]);
}, strtolower($name));
$this->response = $this->response->withHeader($name, $value);
}
return $this;
}
/**
* Will perform a preemptive output and terminate. Do not use this
* directly, instead call it from Callback, JsCallback or similar
* other classes.
*
* @param string|StreamInterface|array<mixed> $output Array type is supported only for JSON response
*
* @return never
*/
public function terminate($output = ''): void
{
$type = preg_replace('~;.*~', '', strtolower($this->response->getHeaderLine('Content-Type'))); // in LC without charset
if ($type === '') {
throw new Exception('Content type must be always set');
}
if ($output instanceof StreamInterface) {
$this->response = $this->response->withBody($output);
$this->outputResponse('');
} elseif ($type === 'application/json') {
if (is_string($output)) {
$output = $this->decodeJson($output);
}
$this->outputResponseJson($output);
} elseif ($type === 'text/html') {
$this->outputResponseHtml($output);
} else {
$this->outputResponse($output);
}
$this->runCalled = true; // prevent shutdown function from triggering
$this->callExit();
}
/**
* @param string|array<mixed>|View|HtmlTemplate $output
*
* @return never
*/
public function terminateHtml($output): void
{
if ($output instanceof View) {
$output = $output->renderToHtml();
} elseif ($output instanceof HtmlTemplate) {
$output = $output->renderToHtml();
}
$this->setResponseHeader('Content-Type', 'text/html');
$this->terminate($output);
}
/**
* @param string|array<mixed>|View $output
*
* @return never
*/
public function terminateJson($output): void
{
if ($output instanceof View) {
$output = $output->renderToJsonArr();
}
$this->setResponseHeader('Content-Type', 'application/json');
$this->terminate($output);
}
/**
* Initializes layout.
*
* @param Layout|array<mixed> $seed
*
* @return $this
*/
public function initLayout($seed)
{
$layout = Layout::fromSeed($seed);
$layout->setApp($this);
if ($this->html === null) {
$this->html = new View(['defaultTemplate' => 'html.html']);
$this->html->setApp($this);
$this->html->invokeInit();
}
$this->layout = $this->html->add($layout); // @phpstan-ignore assign.propertyType
$this->initIncludes();
return $this;
}
/**
* Initialize JS and CSS includes.
*/
public function initIncludes(): void
{
$minified = !file_exists(__DIR__ . '/../.git');
// jQuery
$this->requireJs($this->cdn['jquery'] . '/jquery' . ($minified ? '.min' : '') . '.js');
// Fomantic-UI
$this->requireJs($this->cdn['fomantic-ui'] . '/semantic' . ($minified ? '.min' : '') . '.js');
$this->requireCss($this->cdn['fomantic-ui'] . '/semantic' . ($minified ? '.min' : '') . '.css');
// flatpickr - TODO should be load only when needed
// needs https://github.com/atk4/ui/issues/1875
$this->requireJs($this->cdn['flatpickr'] . '/flatpickr' . ($minified ? '.min' : '') . '.js');
$this->requireCss($this->cdn['flatpickr'] . '/flatpickr' . ($minified ? '.min' : '') . '.css');
if ($this->uiPersistence->locale !== 'en') {
$this->requireJs($this->cdn['flatpickr'] . '/l10n/' . $this->uiPersistence->locale . '.js');
$this->html->js(true, new JsExpression('flatpickr.localize(window.flatpickr.l10ns.' . $this->uiPersistence->locale . ')'));
}
// Agile UI
$this->requireJs($this->cdn['atk'] . '/js/atkjs-ui' . ($minified ? '.min' : '') . '.js');
$this->requireCss($this->cdn['atk'] . '/css/agileui.min.css');
// set JS bundle dynamic loading path
$this->html->template->dangerouslySetHtml(
'InitJsBundle',
(new JsExpression('window.__atkBundlePublicPath = [];', [$this->cdn['atk']]))->jsRender()
);
}
/**
* Adds a <style> block to the HTML Header. Not escaped. Try to avoid
* and use file include instead.
*
* @param string $style CSS rules, like ".foo { background: red }".
*/
public function addStyle($style): void
{
$this->html->template->dangerouslyAppendHtml('Head', $this->getTag('style', [], $style));
}
/**
* Add a new object into the app. You will need to have Layout first.
*
* @param AbstractView $object
* @param string|array<mixed>|null $region
*
* @return ($object is View ? View : AbstractView)
*/
public function add($object, $region = null): AbstractView
{
if (!$this->layout) { // @phpstan-ignore booleanNot.alwaysFalse
throw (new Exception('App layout is missing'))
->addSolution('$app->initLayout() must be called first');
}
return $this->layout->add($object, $region);
}
/**
* Runs app and echo rendered template.
*/
public function run(): void
{
$isExitException = false;
try {
$this->runCalled = true;
$this->hook(self::HOOK_BEFORE_RENDER);
$this->isRendering = true;
$this->html->template->set('title', $this->title);
$this->html->renderAll();
$this->html->template->dangerouslyAppendHtml(
'Head',
$this->getTag('script', [], (new Jquery(new JsFunction([], $this->html->getJs())))->jsRender() . ';')
);
$this->isRendering = false;
if ($this->hasRequestQueryParam(Callback::URL_QUERY_TARGET) && $this->catchRunawayCallbacks) {
throw (new Exception('Callback requested, but never reached. You may be missing some arguments in request URL.'))
->addMoreInfo('callback', $this->getRequestQueryParam(Callback::URL_QUERY_TARGET));
}
$output = $this->html->template->renderToHtml();
} catch (ExitApplicationError $e) {
$output = '';
$isExitException = true;
}
if (!$this->exitCalled) { // output already sent by terminate()
if ($this->isJsUrlRequest()) {
$this->outputResponseJson($output);
} else {
$this->outputResponseHtml($output);
}
}
if ($isExitException) {
$this->callExit();
}
}
/**
* Load template by template file name.
*
* @return HtmlTemplate
*/
public function loadTemplate(string $filename)
{
$template = new $this->templateClass();
if ((['.' => true, '/' => true, '\\' => true][substr($filename, 0, 1)] ?? false) || str_contains($filename, ':\\')) {
return $template->loadFromFile($filename);
}
$dirs = is_array($this->templateDir) ? $this->templateDir : [$this->templateDir];
foreach ($dirs as $dir) {
$t = $template->tryLoadFromFile($dir . '/' . $filename);
if ($t !== false) {
return $t;
}
}
throw (new Exception('Cannot find template file'))
->addMoreInfo('filename', $filename)
->addMoreInfo('templateDir', $this->templateDir);
}
protected function createRequestPathFromLocalPath(string $localPath): string
{
// $localPath does not need realpath() as the path is expected to be built using __DIR__
// which has symlinks resolved
static $requestUrlPath = null;
static $requestLocalPath = null;
if ($requestUrlPath === null) {
if (\PHP_SAPI === 'cli') { // for phpunit
$requestUrlPath = '/';
$requestLocalPath = \Closure::bind(static function () {
return (new ExceptionRenderer\Html(new \Exception()))->getVendorDirectory();
}, null, ExceptionRenderer\Html::class)();
} else {
$request = new HttpFoundation\Request([], [], [], [], [], $_SERVER);
$requestUrlPath = $request->getBasePath();
$requestLocalPath = realpath($request->server->get('SCRIPT_FILENAME'));
}
}
$fs = new Filesystem();
$localPathRelative = $fs->makePathRelative($localPath, dirname($requestLocalPath));
$res = '/' . $fs->makePathRelative($requestUrlPath . '/' . $localPathRelative, '/');
// fix https://github.com/symfony/symfony/pull/40051
if (str_ends_with($res, '/') && !str_ends_with($localPath, '/')) {
$res = substr($res, 0, -1);
}
return $res;
}
/**
* Make current get argument with specified name automatically appended to all generated URLs.
*/
public function stickyGet(string $name, bool $isDeleting = false): ?string
{
$this->stickyGetArguments[$name] = !$isDeleting;
return $this->tryGetRequestQueryParam($name);
}
/**
* Remove sticky GET which was set by stickyGet.
*/
public function stickyForget(string $name): void
{
unset($this->stickyGetArguments[$name]);
}
/**
* @return array<0|string, string>
*/
private function explodeUrlPage(string $page): array
{
$pageExploded = explode('?', $page, 2);
$pagePath = $pageExploded[0] !== ''
? $pageExploded[0]
: null;
parse_str($pageExploded[1] ?? '', $pageArgs);
return [$pagePath] + $pageArgs;
}
/**
* Build a URL that application can use for loading HTML data.
*
* @param string|array<0|string, string|int|false> $page URL as string or array with page path as first element and other GET arguments
* @param array<string, string> $extraRequestUrlArgs additional URL arguments, deleting sticky can delete them
*/
public function url($page = [], array $extraRequestUrlArgs = []): string
{
if (is_string($page)) {
$page = $this->explodeUrlPage($page);
}
$pagePath = $page[0] ?? null;
unset($page[0]);
$request = $this->getRequest();
if ($pagePath === null) {
$pagePath = $request->getUri()->getPath();
}
if (str_ends_with($pagePath, '/')) {
$pagePath .= $this->urlBuildingIndexPage;
}
if (!str_ends_with($pagePath, '/') && !str_contains(basename($pagePath), '.')) {
$pagePath .= $this->urlBuildingExt;
}
$args = $extraRequestUrlArgs;
// add sticky arguments
$requestQueryParams = $request->getQueryParams();
foreach ($this->stickyGetArguments as $k => $v) {
if ($v && isset($requestQueryParams[$k])) {
$args[$k] = $requestQueryParams[$k];
} else {
unset($args[$k]);
}
}
// add arguments
foreach ($page as $k => $v) {
if ($v === false) {
unset($args[$k]);
} else {
$args[$k] = $v;
}
}
$pageQuery = http_build_query($args, '', '&', \PHP_QUERY_RFC3986);
return $pagePath . ($pageQuery !== '' ? '?' . $pageQuery : '');
}
/**
* Build a URL that application can use for JS callbacks. Some framework integration will use a different routing
* mechanism for non-HTML response.
*
* @param string|array<0|string, string|int|false> $page URL as string or array with page path as first element and other GET arguments
* @param array<string, string> $extraRequestUrlArgs additional URL arguments, deleting sticky can delete them
*/
public function jsUrl($page = [], array $extraRequestUrlArgs = []): string
{
if (is_string($page)) {
$page = $this->explodeUrlPage($page);
}
if (($page['__atk_json'] ?? null) !== false) {
$page['__atk_json'] = 1;
}
return $this->url($page, $extraRequestUrlArgs);
}
/**
* Request was made using App::jsUrl().
*/
public function isJsUrlRequest(): bool
{
return $this->hasRequestQueryParam('__atk_json') && $this->getRequestQueryParam('__atk_json') !== '0';
}
/**
* Adds additional JS script include in application template.
*
* @param string $url
* @param bool $isAsync whether or not you want Async loading
* @param bool $isDefer whether or not you want Defer loading
*
* @return $this
*/
public function requireJs($url, $isAsync = false, $isDefer = false)
{
$this->html->template->dangerouslyAppendHtml('Head', $this->getTag('script', ['src' => $url, 'defer' => $isDefer, 'async' => $isAsync], '') . "\n");
return $this;
}
/**
* Adds additional CSS stylesheet include in application template.
*
* @param string $url
*
* @return $this
*/
public function requireCss($url)
{
$this->html->template->dangerouslyAppendHtml('Head', $this->getTag('link/', ['rel' => 'stylesheet', 'type' => 'text/css', 'href' => $url]) . "\n");
return $this;
}
/**
* A convenient wrapper for sending user to another page.
*
* @param string|array<0|string, string|int|false> $page
*/
public function redirect($page, bool $permanent = false): void
{
$this->setResponseStatusCode($permanent ? 301 : 302);
$this->setResponseHeader('location', $this->url($page));
$this->terminateHtml('');
}
/**
* Generate action for redirecting user to another page.
*
* @param string|array<0|string, string|int|false> $page
*/
public function jsRedirect($page, bool $newWindow = false): JsExpressionable
{
return new JsExpression('window.open([], [])', [$this->url($page), $newWindow ? '_blank' : '_top']);
}
public function isVoidTag(string $tag): bool
{
return [
'area' => true, 'base' => true, 'br' => true, 'col' => true, 'embed' => true,
'hr' => true, 'img' => true, 'input' => true, 'link' => true, 'meta' => true,
'param' => true, 'source' => true, 'track' => true, 'wbr' => true,
][strtolower($tag)] ?? false;
}
/**
* Construct HTML tag with supplied attributes.
*
* $html = getTag('img/', ['src' => 'foo.gif', 'border' => 0])
* --> "<img src="foo.gif" border="0">"
*
*
* The following rules are respected:
*
* 1. all array key => val elements appear as attributes with value escaped.
* getTag('input/', ['value' => 'he"llo'])
* --> <input value="he"llo">
*
* 2. true value will add attribute without value
* getTag('td', ['nowrap' => true])
* --> <td nowrap="nowrap">
*
* 3. false value will ignore the attribute
* getTag('img/', ['src' => false])
* --> <img>
*
* 4. passing key 0 => "val" will re-define the element itself
* getTag('div', ['a', 'href' => 'picture'])
* --> <a href="picture">
*
* 5. use '/' at end of tag to self-close it (self closing slash is not rendered because of HTML5 void tag)
* getTag('img/', ['src' => 'foo.gif'])
* --> <img src="foo.gif">
*
* 6. if main tag is self-closing, overriding it keeps it self-closing
* getTag('img/', ['input', 'type' => 'picture'])
* --> <input type="picture">
*
* 7. simple way to close tag. Any attributes to closing tags are ignored
* getTag('/td')
* --> </td>
*
* 7b. except for 0 => 'newtag'
* getTag('/td', ['th', 'align' => 'left'])
* --> </th>
*
* 8. using $value will add value inside tag. It will also encode value.
* getTag('a', ['href' => 'foo.html'], 'click here >>')
* --> <a href="foo.html">click here >></a>
*
* 9. pass array as 3rd parameter to nest tags. Each element can be either string (inserted as-is) or
* array (passed to getTag recursively)
* getTag('a', ['href' => 'foo.html'], [['b', 'click here'], ' for fun'])
* --> <a href="foo.html"><b>click here</b> for fun</a>
*
* 10. extended example:
* getTag('a', ['href' => 'hello'], [
* ['b', 'class' => 'red', [
* ['i', 'class' => 'blue', 'welcome']
* ]]
* ])
* --> <a href="hello"><b class="red"><i class="blue">welcome</i></b></a>'
*
* @param array<0|string, string|bool> $attr
* @param string|list<array{0: string, 1?: array<0|string, string|bool>, 2?: string|list<mixed>|null}|string>|null $value
*/
public function getTag(string $tag, array $attr = [], $value = null): string
{