• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

trejjam / ares / 6449658980

08 Oct 2023 07:54PM UTC coverage: 91.736% (-1.5%) from 93.22%
6449658980

push

github

web-flow
Add support for dynamic arguments (#65)

5 of 5 new or added lines in 1 file covered. (100.0%)

111 of 121 relevant lines covered (91.74%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.24
/src/DI/AresExtension.php
1
<?php
2
declare(strict_types=1);
3

4
namespace Trejjam\Ares\DI;
5

6
use Composer;
7
use GuzzleHttp;
8
use Nette;
9
use Nette\DI\CompilerExtension;
10
use Nette\DI\Definitions\ServiceDefinition;
11
use Nette\DI\Definitions\Statement;
12
use Nette\PhpGenerator\Literal;
13
use Nette\Schema\Expect;
14
use stdClass;
15
use Trejjam\Ares;
16
use Trejjam\Ares\Mapper;
17

18
/**
19
 * @property-read stdClass $config
20
 */
21
class AresExtension extends CompilerExtension
22
{
23
    public function getConfigSchema() : Nette\Schema\Schema
24
    {
25
        return Expect::structure([
1✔
26
            'http' => Expect::structure([
1✔
27
                'clientFactory' => Expect::anyOf(Expect::string(), Expect::array(), Expect::type(Statement::class))->nullable(),
1✔
28
                'caChain' => Expect::anyOf(Expect::string(), Expect::type(Statement::class))->nullable(),
1✔
29
                'client' => Expect::array()->dynamic()->default([]),
1✔
30
            ]),
31
            'mapper' => Expect::anyOf(Expect::string(), Expect::array(), Expect::type(Statement::class))->default(Mapper::class),
1✔
32
        ]);
33
    }
34

35
    public function loadConfiguration() : void
36
    {
37
        $http = $this->config->http;
1✔
38
        if ($http->caChain === null && method_exists(
1✔
39
            'Composer\CaBundle\CaBundle',
1✔
40
            'getSystemCaRootBundlePath'
1✔
41
        )) {
42
            $http->caChain = Composer\CaBundle\CaBundle::getSystemCaRootBundlePath();
1✔
43
        }
44
    }
1✔
45

46
    public function beforeCompile() : void
47
    {
48
        parent::beforeCompile();
1✔
49

50
        $builder = $this->getContainerBuilder();
1✔
51

52
        $mapper = $this->config->mapper;
1✔
53
        $http = $this->config->http;
1✔
54

55
        $this->registerFactory('mapper', Ares\IMapper::class, $mapper);
1✔
56

57
        if ($http->clientFactory !== null) {
1✔
58
            $httpClient = $this->registerFactory(
1✔
59
                'http.client',
1✔
60
                GuzzleHttp\Client::class,
1✔
61
                $http->clientFactory
1✔
62
            );
63
        }
64
        else {
65
            $httpClient = $builder->addDefinition($this->prefix('http.client'))->setType(GuzzleHttp\Client::class);
1✔
66
        }
67

68
        if ($http->client instanceof Literal) {
1✔
69
            $http->client = new Literal(
×
70
                "array_merge(['verify' => '{$http->caChain}'], {$http->client})"
×
71
            );
72
        }
73
        elseif ($http->caChain !== null && !array_key_exists('verify', $http->client)) {
1✔
74
            $http->client['verify'] = $http->caChain;
1✔
75
        }
76

77
        $httpClient
78
            ->setArguments(['config' => $http->client])
1✔
79
            ->setAutowired(false);
1✔
80

81
        $builder
82
            ->addDefinition($this->prefix('request'))
1✔
83
            ->setFactory(Ares\Request::class)->setArguments(['httpClient' => $httpClient]);
1✔
84
    }
1✔
85

86
    private function registerFactory(string $name, string $type, string|array|Statement $factory) : ServiceDefinition
1✔
87
    {
88
        $builder = $this->getContainerBuilder();
1✔
89

90
        if (is_string($factory) && str_starts_with($factory, '@')) {
1✔
91
            $factoryDefinition = $builder->addDefinition($this->prefix($name));
1✔
92

93
            $factoryDefinition->setFactory($factory);
1✔
94
        }
95
        else {
96
            $this->loadDefinitionsFromConfig([$name => $factory]);
1✔
97

98
            $factoryDefinition = $builder->getDefinition($this->prefix($name));
1✔
99
        }
100

101
        assert($factoryDefinition instanceof ServiceDefinition);
102

103
        $factoryDefinition->setType($type);
1✔
104

105
        return $factoryDefinition;
1✔
106
    }
107
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc