setName('models:clear')
->setDescription('Removes all generated models, leaves only empty directory behind.')
->setHelp('Use this command if you want to remove existing model cache.');
}
/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$target = Utils::getModelsCacheDirectory();
if (!is_dir($target)) {
$output->writeln('The target directory does not exist.');
$output->writeln('You can safely use "bin/console models:generate" to generate models.');
return -1;
}
$output->writeln(sprintf('Cleaning up "%s">...', $target));
if (self::isVerbose($output)) {
$output->writeln('');
}
$checksumFile = implode(DIRECTORY_SEPARATOR, [$target, 'checksum.json']);
$models = new PhpFilesIterator($target);
foreach ($models as $model) {
if (file_exists($model['file'])) {
unlink($model['file']);
if (self::isVerbose($output)) {
$output->writeln(sprintf('- Removed "%s">', $model['file']));
}
}
}
if (file_exists($checksumFile)) {
unlink($checksumFile);
}
if (self::isVerbose($output)) {
$output->writeln('');
}
$output->writeln(' ✓ Done!>');
return 0;
}
}