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

api-platform / core / 13175309672

06 Feb 2025 09:04AM UTC coverage: 7.663% (-0.2%) from 7.841%
13175309672

push

github

web-flow
fix: ensure template files have a tpl file extension (#6826) (#6829)

2 of 5 new or added lines in 4 files covered. (40.0%)

3676 existing lines in 122 files now uncovered.

13073 of 170593 relevant lines covered (7.66%)

27.3 hits per line

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

0.0
/docs/src/Kernel.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Playground;
15

16
use ApiPlatform\Playground\DependencyInjection\Compiler\AttributeFilterPass;
17
use ApiPlatform\Playground\DependencyInjection\Compiler\FilterPass;
18
use ApiPlatform\Playground\Doctrine\StaticMappingDriver;
19
use ApiPlatform\Playground\Metadata\Resource\Factory\ClassResourceNameCollectionFactory;
20
use Doctrine\Migrations\Configuration\Configuration;
21
use Doctrine\Migrations\Configuration\EntityManager\ExistingEntityManager;
22
use Doctrine\Migrations\Configuration\Migration\ExistingConfiguration;
23
use Doctrine\Migrations\DependencyFactory;
24
use Doctrine\Migrations\Metadata\Storage\TableMetadataStorageConfiguration;
25
use Doctrine\Migrations\Version\Direction;
26
use Doctrine\Migrations\Version\Version;
27
use Doctrine\ORM\EntityManagerInterface;
28
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
29
use Symfony\Component\Config\Loader\LoaderInterface;
30
use Symfony\Component\Config\Resource\ReflectionClassResource;
31
use Symfony\Component\Console\Input\ArrayInput;
32
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
33
use Symfony\Component\DependencyInjection\ContainerBuilder;
34
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
35
use Symfony\Component\HttpFoundation\Request;
36
use Symfony\Component\HttpFoundation\Response;
37
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
38

39
use function App\DependencyInjection\configure; // @phpstan-ignore-line
40
use function App\Playground\request;
41

42
class Kernel extends BaseKernel
43
{
44
    use MicroKernelTrait;
45
    private $declaredClasses = [];
46
    private string $guide;
47

48
    public function __construct(string $environment, bool $debug, ?string $guide = null)
49
    {
50
        parent::__construct($environment, $debug);
×
51

52
        if (!$guide) {
×
53
            $guide = $_SERVER['APP_GUIDE'] ?? $_ENV['APP_GUIDE'] ?? null;
×
54

55
            if (!$guide) {
×
56
                throw new \RuntimeException('No guide.');
×
57
            }
58
        }
59

60
        $this->guide = $guide;
×
61
        require_once "{$this->getProjectDir()}/guides/{$this->guide}.php";
×
62
    }
63

64
    private function configureContainer(ContainerConfigurator $container, LoaderInterface $loader, ContainerBuilder $builder): void
65
    {
66
        $configDir = $this->getConfigDir();
×
67

68
        $container->import($configDir.'/{packages}/*.{php,yaml}');
×
69

70
        $services = $container->services()
×
71
            ->defaults()
×
72
            ->autowire()
×
73
            ->autoconfigure();
×
74

75
        $resources = [];
×
76
        $entities = [];
×
77

78
        foreach ($this->getDeclaredClasses() as $class) {
×
79
            $refl = new \ReflectionClass($class);
×
80
            $ns = $refl->getNamespaceName();
×
81
            if (!str_starts_with($ns, 'App')) {
×
82
                continue;
×
83
            }
84

85
            $builder->addResource(new ReflectionClassResource($refl));
×
86

87
            $hasMetadataAttribute = false;
×
88
            foreach ($refl->getAttributes() as $a) {
×
89
                if (str_starts_with($a->getName(), 'ApiPlatform\\Metadata')) {
×
90
                    $hasMetadataAttribute = true;
×
91
                    break;
×
92
                }
93
            }
94

95
            if (str_starts_with($ns, 'App\\Entity')) {
×
96
                $entities[] = $class;
×
97
            }
98

99
            if ($hasMetadataAttribute) {
×
100
                $resources[] = $class;
×
101
                continue;
×
102
            }
103

104
            $services->set($class);
×
105
        }
106

107
        $services->set(ClassResourceNameCollectionFactory::class)->args(['$classes' => $resources]);
×
108

109
        $builder->addCompilerPass(new AttributeFilterPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 101);
×
110
        $builder->addCompilerPass(new FilterPass());
×
111

112
        $container->parameters()->set('guide', $this->guide);
×
113

114
        $services->set('doctrine.orm.default_metadata_driver', StaticMappingDriver::class)->args(['$classes' => $entities]);
×
115

116
        if (\function_exists('App\DependencyInjection\configure')) {
×
117
            configure($container);
×
118
        }
119
    }
120

121
    public function request(?Request $request = null): Response
122
    {
123
        if (null === $request && \function_exists('App\Playground\request')) {
×
124
            $request = request();
×
125
        }
126

127
        $request = $request ?? Request::create('/docs.json');
×
128
        $response = $this->handle($request);
×
129
        $response->send();
×
130
        $this->terminate($request, $response);
×
131

132
        return $response;
×
133
    }
134

135
    public function getCacheDir(): string
136
    {
137
        return parent::getCacheDir().\DIRECTORY_SEPARATOR.$this->guide;
×
138
    }
139

140
    public function executeMigrations(string $direction = Direction::UP): void
141
    {
142
        $migrationClasses = $this->getDeclaredClassesForNamespace('DoctrineMigrations');
×
143

144
        if (!$migrationClasses) {
×
145
            return;
×
146
        }
147

148
        $this->boot();
×
149

150
        foreach ($migrationClasses as $migrationClass) {
×
151
            if ("Doctrine\Migrations\AbstractMigration" !== (new \ReflectionClass($migrationClass))->getParentClass()->getName()) {
×
152
                continue;
×
153
            }
154
            $conf = new Configuration();
×
155
            $conf->addMigrationClass($migrationClass);
×
156
            $conf->setTransactional(true);
×
157
            $conf->setCheckDatabasePlatform(true);
×
158
            $meta = new TableMetadataStorageConfiguration();
×
159
            $meta->setTableName('doctrine_migration_versions');
×
160
            $conf->setMetadataStorageConfiguration($meta);
×
161

162
            $confLoader = new ExistingConfiguration($conf);
×
163
            /** @var EntityManagerInterface $em */
164
            $em = $this->getContainer()->get('doctrine.orm.entity_manager');
×
165
            $loader = new ExistingEntityManager($em);
×
166
            $dependencyFactory = DependencyFactory::fromEntityManager($confLoader, $loader);
×
UNCOV
167
            $metadataStorage = $dependencyFactory->getMetadataStorage();
×
168

169
            try {
UNCOV
170
                $metadataStorage->ensureInitialized();
×
171
            } catch (\Exception) {
×
172
                // table exists
173
            }
174

175
            $executed = $metadataStorage->getExecutedMigrations();
×
176

177
            if ($executed->hasMigration(new Version($migrationClass)) && Direction::DOWN !== $direction) {
×
178
                continue;
×
179
            }
180

181
            $planCalculator = $dependencyFactory->getMigrationPlanCalculator();
×
UNCOV
182
            $plan = $planCalculator->getPlanForVersions([new Version($migrationClass)], $direction);
×
UNCOV
183
            $migrator = $dependencyFactory->getMigrator();
×
UNCOV
184
            $migratorConfigurationFactory = $dependencyFactory->getConsoleInputMigratorConfigurationFactory();
×
UNCOV
185
            $migratorConfiguration = $migratorConfigurationFactory->getMigratorConfiguration(new ArrayInput([]));
×
186

187
            $migrator->migrate($plan, $migratorConfiguration);
×
188
        }
189
    }
190

191
    public function loadFixtures(): void
192
    {
193
        $fixtureClasses = $this->getDeclaredClassesForNamespace('App\Fixtures');
×
194
        if (!$fixtureClasses) {
×
195
            return;
×
196
        }
197
        $this->boot();
×
UNCOV
198
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
×
UNCOV
199
        foreach ($fixtureClasses as $class) {
×
UNCOV
200
            if ("Doctrine\Bundle\FixturesBundle\Fixture" !== (new \ReflectionClass($class))->getParentClass()->getName()) {
×
UNCOV
201
                continue;
×
202
            }
203
            (new $class())->load($em);
×
204
        }
205
    }
206

207
    private function getDeclaredClassesForNamespace(string $namespace): array
208
    {
UNCOV
209
        return array_filter($this->getDeclaredClasses(), static function (string $class) use ($namespace): bool {
×
UNCOV
210
            return str_starts_with($class, $namespace);
×
UNCOV
211
        });
×
212
    }
213

214
    /**
215
     * @return class-string[]
216
     */
217
    public function getDeclaredClasses(): array
218
    {
UNCOV
219
        if (!$this->declaredClasses) {
×
UNCOV
220
            $this->declaredClasses = get_declared_classes();
×
221
        }
222

UNCOV
223
        return $this->declaredClasses;
×
224
    }
225
}
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