-
-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathApplication.php
More file actions
86 lines (77 loc) · 3.24 KB
/
Copy pathApplication.php
File metadata and controls
86 lines (77 loc) · 3.24 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
<?php declare(strict_types=1);
namespace Bref\Extra;
use AsyncAws\Core\HttpClient\AwsRetryStrategy;
use AsyncAws\Lambda\LambdaClient;
use Bref\Extra\Aws\LayerProvider;
use Bref\Extra\Aws\LayerPublisher;
use Bref\Extra\Command\ListCommand;
use Bref\Extra\Command\PublishCommand;
use Bref\Extra\Service\RegionProvider;
use Bref\Logger\StderrLogger;
use DI\Container;
use DI\ContainerBuilder;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\RetryableHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class Application extends \Silly\Edition\PhpDi\Application
{
protected function createContainer(): Container
{
$builder = new ContainerBuilder;
$projectDir = dirname(__DIR__);
$localLayers = $this->getLayers($projectDir);
$builder->addDefinitions([
'project_dir' => $projectDir,
'layer_names' => $localLayers,
LoggerInterface::class => function (ContainerInterface $c) {
return new StderrLogger(LogLevel::INFO);
},
LayerProvider::class => function (ContainerInterface $c) {
return new LayerProvider($c->get(LambdaClient::class), $c->get('layer_names'));
},
ListCommand::class => function (ContainerInterface $c) {
return new ListCommand($c->get(LayerProvider::class), $c->get(RegionProvider::class), $c->get('project_dir'));
},
PublishCommand::class => function (ContainerInterface $c) {
return new PublishCommand($c->get(LayerPublisher::class), $c->get(RegionProvider::class), $c->get('project_dir'));
},
HttpClientInterface::class => function (ContainerInterface $c) {
$strategy = new AwsRetryStrategy(AwsRetryStrategy::DEFAULT_RETRY_STATUS_CODES, 5000, 2, 600000);
return new RetryableHttpClient(HttpClient::create(), $strategy, 12, $c->get(LoggerInterface::class));
},
LambdaClient::class => function (ContainerInterface $c) {
return new LambdaClient([], null, $c->get(HttpClientInterface::class), $c->get(LoggerInterface::class));
},
LayerPublisher::class => function (ContainerInterface $c) {
return new LayerPublisher($c->get(LambdaClient::class));
},
]);
return $builder->build();
}
/**
* Search in the local filesystem for all layer versions.
*/
protected function getLayers(string $projectDir): array
{
$finder = new Finder;
$finder->in($projectDir . '/layers')
->depth(0)
->directories();
$layers = [];
foreach ($finder as $dir) {
$layerName = $dir->getBasename();
$configFile = $dir->getPathname() . '/config.json';
if (file_exists($configFile)) {
$config = json_decode(file_get_contents($configFile), true);
foreach ($config['php'] ?? [] as $version) {
$layers[] = sprintf('%s-php-%s', $layerName, $version);
}
}
}
return $layers;
}
}