|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of Ymir PHP Runtime. |
| 7 | + * |
| 8 | + * (c) Carl Alexander <support@ymirapp.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Ymir\Runtime\Application; |
| 15 | + |
| 16 | +use Symfony\Component\Process\Process; |
| 17 | +use Ymir\Runtime\Exception\ApplicationInitializationException; |
| 18 | +use Ymir\Runtime\Lambda\Handler\Http\LaravelHttpEventHandler; |
| 19 | +use Ymir\Runtime\Lambda\Handler\LambdaEventHandlerCollection; |
| 20 | +use Ymir\Runtime\Lambda\Handler\Sqs\LaravelSqsHandler; |
| 21 | + |
| 22 | +/** |
| 23 | + * Laravel runtime application. |
| 24 | + */ |
| 25 | +class LaravelApplication extends AbstractApplication |
| 26 | +{ |
| 27 | + use CreatesLaravelStorageDirectoriesTrait; |
| 28 | + |
| 29 | + /** |
| 30 | + * {@inheritDoc} |
| 31 | + */ |
| 32 | + public static function present(string $directory): bool |
| 33 | + { |
| 34 | + return file_exists($directory.'/public/index.php') |
| 35 | + && file_exists($directory.'/artisan'); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * {@inheritDoc} |
| 40 | + */ |
| 41 | + public function getQueueHandlers(): LambdaEventHandlerCollection |
| 42 | + { |
| 43 | + return $this->getEventHandlerCollection([ |
| 44 | + new LaravelSqsHandler($this->context->getLogger(), $this->context->getRootDirectory()), |
| 45 | + ]); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * {@inheritDoc} |
| 50 | + */ |
| 51 | + public function getWebsiteHandlers(): LambdaEventHandlerCollection |
| 52 | + { |
| 53 | + return $this->getEventHandlerCollection([ |
| 54 | + new LaravelHttpEventHandler($this->context->getLogger(), $this->context->getPhpFpmProcess(), $this->context->getRootDirectory()), |
| 55 | + ]); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritDoc} |
| 60 | + */ |
| 61 | + public function initialize(): void |
| 62 | + { |
| 63 | + $logger = $this->context->getLogger(); |
| 64 | + |
| 65 | + $this->createStorageDirectories($logger); |
| 66 | + |
| 67 | + $logger->debug('Creating Laravel cache'); |
| 68 | + |
| 69 | + $process = new Process(['/opt/bin/php', $this->context->getRootDirectory().'/artisan', 'config:cache', '--no-ansi']); |
| 70 | + $process->run(); |
| 71 | + |
| 72 | + if (!$process->isSuccessful()) { |
| 73 | + throw new ApplicationInitializationException(sprintf('Failed to create Laravel cache: %s', $process->getOutput())); |
| 74 | + } |
| 75 | + |
| 76 | + $logger->debug('Laravel cache created'); |
| 77 | + } |
| 78 | +} |
0 commit comments