Skip to content

Make config file optional #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions e2e/no-config-file/expected-output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@


[ERROR] No configuration file specified, cannot run Fractor.

15 changes: 13 additions & 2 deletions e2e/run-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ cd $TESTS_BASE_DIR
rm -r composer.lock vendor || true
composer install

for TEST_DIR in typo3-typoscript typo3-xml typo3-yaml
ALL_TESTS="no-config-file typo3-typoscript typo3-xml typo3-yaml"
TESTS_TO_RUN=${1:-$ALL_TESTS}

echo "Running tests: " $TESTS_TO_RUN

for TEST_DIR in $TESTS_TO_RUN
do
set +x
echo
Expand All @@ -28,7 +33,13 @@ do
# copy over our fixture to the path that Fractor will run in
cp -r $TEST_DIR/fixtures/ $TEST_DIR/result/

./vendor/bin/fractor process -c $TESTS_BASE_DIR/$TEST_DIR/fractor.php > $TEST_DIR/output.txt
CONFIG_FILE=""
if [ -f $TESTS_BASE_DIR/$TEST_DIR/fractor.php ]
then
CONFIG_FILE="-c $TESTS_BASE_DIR/$TEST_DIR/fractor.php"
fi

./vendor/bin/fractor process $CONFIG_FILE > $TEST_DIR/output.txt

set +x
echo
Expand Down
11 changes: 11 additions & 0 deletions packages/fractor/src/Configuration/ConfigurationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function createFromInput(InputInterface $input): Configuration
$paths = (array) $this->parameterBag->get(Option::PATHS);

return new Configuration(
$this->getConfigurationFile(),
$this->allowedFileExtensionsResolver->resolve(),
$paths,
(array) $this->parameterBag->get(Option::SKIP),
Expand All @@ -40,11 +41,21 @@ public function createForTests(array $paths): Configuration
Assert::allStringNotEmpty($paths, 'No directories given');

return new Configuration(
$this->getConfigurationFile(),
$this->allowedFileExtensionsResolver->resolve(),
$paths,
(array) $this->parameterBag->get(Option::SKIP),
false,
false
);
}

private function getConfigurationFile(): ?string
{
$configurationFile = $this->parameterBag->get('fractor_config_file');
if ($configurationFile !== null) {
Assert::string($configurationFile);
}
return $configurationFile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* @param string[] $skip
*/
public function __construct(
private ?string $configurationFile,
private array $fileExtensions,
private array $paths,
private array $skip,
Expand All @@ -26,6 +27,11 @@ public function __construct(
Assert::allStringNotEmpty($this->paths, 'No directories given');
}

public function getConfigurationFile(): ?string
{
return $this->configurationFile;
}

/**
* @return string[]
*/
Expand Down
9 changes: 8 additions & 1 deletion packages/fractor/src/Console/Command/ProcessCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,19 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$configuration = $this->configurationFactory->createFromInput($input);

if ($configuration->getConfigurationFile() === null) {
$io->error('No configuration file specified, cannot run Fractor.');
return Command::FAILURE;
}

$processResult = $this->runner->run(new SymfonyConsoleOutput($output), $configuration);

$outputFormat = 'console';
$outputFormatter = $this->outputFormatterCollector->getByName($outputFormat);
$outputFormatter->setSymfonyStyle(new SymfonyStyle($input, $output));
$outputFormatter->setSymfonyStyle($io);
$outputFormatter->report($processResult, $configuration);

return $this->resolveReturnCode($processResult, $configuration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace a9f\Fractor\DependencyInjection;

use a9f\Fractor\Configuration\FractorConfiguration;
use a9f\Fractor\Exception\ShouldNotHappenException;
use a9f\FractorExtensionInstaller\Generated\InstalledPackages;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -78,12 +79,17 @@ private function collectConfigFilesFromExtensions(): array

private function loadFractorConfigFile(string $fractorConfigFile, ContainerBuilder $containerBuilder): void
{
Assert::fileExists($fractorConfigFile);
if (! file_exists($fractorConfigFile)) {
$callable = (FractorConfiguration::configure());
$containerBuilder->setParameter('fractor_config_file', null);
} else {
$containerBuilder->setParameter('fractor_config_file', $fractorConfigFile);

$self = $this;
$callable = (require $fractorConfigFile);
$self = $this;
$callable = (require $fractorConfigFile);

Assert::isCallable($callable);
Assert::isCallable($callable);
}
$instanceOf = [];
/** @var callable(ContainerConfigurator $container): void $callable */
$callable(new ContainerConfigurator($containerBuilder, new PhpFileLoader($containerBuilder, new FileLocator(
Expand Down