squirrelphp/entities

View on GitHub
bin/squirrel_repositories_generate

Summary

Maintainability
Test Coverage
#!/usr/bin/env php
<?php

use Composer\Autoload\ClassLoader;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;

foreach ([
    __DIR__ . '/../../autoload.php',
    __DIR__ . '/../../../autoload.php',
    __DIR__ . '/../vendor/autoload.php',
] as $file) {
    if (file_exists($file)) {
        require $file;

        break;
    }
}

// Define the necessary command line options and defaults
$inputDefinition = new InputDefinition();
$inputDefinition->addOption(new InputOption(
    'verbose',
    'v',
    InputOption::VALUE_NONE,
    'Verbose mode, showing all generated repositories and possible file conflicts' // Description
));
$inputDefinition->addOption(new InputOption(
    'force',
    'f',
    InputOption::VALUE_NONE,
    'Force creating repositories and gitignore files, even when there are file conflicts' // Description
));
$inputDefinition->addOption(new InputOption(
    'source-dir',
    null,
    InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
    'Source directories (relative to current directory) where entities will be searched recursively' // Description
));

$input = new ArgvInput(null, $inputDefinition);
$srcDirectories = $input->getOption('source-dir');
$isVerbose = $input->getOption('verbose');
$isForce = $input->getOption('force');

// Execute command to generate repositories
$cmd = new \Squirrel\Entities\Generate\RepositoriesGenerateCommand(
    $srcDirectories,
    $isForce,
    new \Squirrel\Entities\Generate\PHPFilesInDirectoryGetContents()
);
[$logRepositories, $logConflicts] = $cmd();

// Show detailed log
if ($isVerbose === true) {
    echo implode("\n", $logRepositories);
}

// Show summary
if (\count($logConflicts) > 0 && $isForce === false) {
    echo "\n" . count($logConflicts) . ' file conflicts found, where existing files (which seem to not have been generated by this command) would be overwritten. Re-run with --force if you want to overwrite these files, or with --verbose to see which files have conflicts.' . "\n";
} else {
    echo "\n" . count($logRepositories) . ' entities found for which repositories were generated.' . "\n";

    if (\count($logConflicts) > 0) {
        echo count($logConflicts) . ' file conflicts found, those files were overwritten because of --force flag.' . "\n";
    }
}