Your IP : 216.73.217.79


Current Path : /var/www/v3.cesa.co.za/scripts/
Upload File :
Current File : /var/www/v3.cesa.co.za/scripts/remove-orphan-api-platform-config.php

<?php

/**
 * Removes Api Platform leftovers when the package is not fully installed.
 *
 * Typical server state after a failed or partial install:
 * - config/packages/api_platform.yaml
 * - config/routes/api_platform.yaml (type: api_platform → Loader error)
 * - ApiPlatformBundle still listed in config/bundles.php (Twig paths → missing vendor views)
 * - Broken vendor/api-platform/ directory
 */

$projectDir = dirname(__DIR__);

if (isApiPlatformInstalled($projectDir)) {
    return;
}

$orphanFiles = [
    $projectDir . '/config/packages/api_platform.yaml',
    $projectDir . '/config/packages/dev/api_platform.yaml',
    $projectDir . '/config/packages/prod/api_platform.yaml',
    $projectDir . '/config/packages/test/api_platform.yaml',
    $projectDir . '/config/routes/api_platform.yaml',
    $projectDir . '/config/routes/dev/api_platform.yaml',
    $projectDir . '/config/routes/prod/api_platform.yaml',
];

foreach ($orphanFiles as $file) {
    removeFileIfExists($file);
}

removeApiPlatformFromBundlesPhp($projectDir . '/config/bundles.php');
stripApiPlatformImportsFromRoutesYaml($projectDir . '/config/routes.yaml');

$brokenVendor = $projectDir . '/vendor/api-platform';
if (is_dir($brokenVendor) && !isApiPlatformInstalled($projectDir)) {
    removeDirectory($brokenVendor);
    fwrite(STDERR, sprintf("Removed broken vendor tree: %s\n", $brokenVendor));
}

function isApiPlatformInstalled(string $projectDir): bool
{
    return is_file($projectDir . '/vendor/api-platform/symfony/Bundle/ApiPlatformBundle.php')
        && is_dir($projectDir . '/vendor/api-platform/symfony/Bundle/Resources/views');
}

function removeFileIfExists(string $file): void
{
    if (!is_file($file)) {
        return;
    }

    unlink($file);
    fwrite(STDERR, sprintf("Removed orphan Api Platform config: %s\n", $file));
}

function removeApiPlatformFromBundlesPhp(string $bundlesFile): void
{
    if (!is_file($bundlesFile)) {
        return;
    }

    $lines = file($bundlesFile, FILE_IGNORE_NEW_LINES);
    if ($lines === false) {
        return;
    }

    $filtered = array_values(array_filter(
        $lines,
        static fn (string $line): bool => !preg_match('/ApiPlatform\\\\/i', $line)
    ));

    if (count($filtered) === count($lines)) {
        return;
    }

    file_put_contents($bundlesFile, implode("\n", $filtered) . "\n");
    fwrite(STDERR, "Removed Api Platform bundle entry from config/bundles.php\n");
}

function stripApiPlatformImportsFromRoutesYaml(string $routesFile): void
{
    if (!is_file($routesFile)) {
        return;
    }

    $content = file_get_contents($routesFile);
    $updated = preg_replace('/^[ \t-]*.*api_platform.*\n/m', '', $content);

    if ($updated === null || $updated === $content) {
        return;
    }

    file_put_contents($routesFile, $updated);
    fwrite(STDERR, "Removed Api Platform imports from config/routes.yaml\n");
}

function removeDirectory(string $dir): void
{
    if (!is_dir($dir)) {
        return;
    }

    $iterator = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS),
        RecursiveIteratorIterator::CHILD_FIRST
    );

    foreach ($iterator as $item) {
        if ($item->isDir()) {
            rmdir($item->getPathname());
        } else {
            unlink($item->getPathname());
        }
    }

    rmdir($dir);
}