<commit_before><?php

/**
 * MIT License. This file is part of the Propel package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Propel\Generator\Command;

use Propel\Generator\Config\GeneratorConfig;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

/**
 * @author <NAME> <<EMAIL>>
 */
abstract class AbstractCommand extends Command
{
    /**
     * @var string
     */
    public const DEFAULT_CONFIG_DIRECTORY = '.';

    /**
     * @var int
     */
    public const CODE_SUCCESS = 0;

    /**
     * @var int
     */
    public const CODE_ERROR = 1;

    /**
     * @var \Symfony\Component\Filesystem\Filesystem|null
     */
    protected $filesystem;

    /**
     * {@inheritDoc}
     *
     * @return void
     */
    protected function configure()
    {
        $this
            ->addOption('platform', null, InputOption::VALUE_REQUIRED, 'The platform to use. Define a full qualified class name or mysql|pgsql|sqlite|mssql|oracle.')
            ->addOption('config-dir', null, InputOption::VALUE_REQUIRED, 'The directory where the configuration file is placed.', self::DEFAULT_CONFIG_DIRECTORY)
            ->addOption('recursive', null, InputOption::VALUE_NONE, 'Search recursive for *schema.xml inside the input directory');
    }

    /**
     * Returns a new `GeneratorConfig` object with your `$properties` merged with
     * the configuration properties in the `config-dir` folder.
     *
     * @param array|null $properties Properties to add to the configuration. They usually come from command line.
     * @param \Symfony\Component\Console\Input\InputInterface|null $input
     *
     * @return \Propel\Generator\Config\GeneratorConfig
     */
    protected function getGeneratorConfig(?array $properties = null, ?InputInterface $input = null)
    {
        if ($input === null) {
            return new GeneratorConfig(null, $properties);
        }

        if ($this->hasInputOption('platform', $input)) {
            $properties['propel']['generator']['platformClass'] = $input->getOption('platform');
        }

        if ($input->hasParameterOption('--recursive')) {
            $properties['propel']['generator']['recursive'] = $input->getOption('recursive');
        }

        return new GeneratorConfig($input->getOption('config-dir'), $properties);
    }

    /**
     * Find every schema files.
     *
     * @param array<string>|string $directory Path to the input directory
     * @param bool $recursive Search for file inside the input directory and all subdirectories
     *
     * @return array List of schema files
     */
    protected function getSchemas($directory, $recursive = false)
    {
        $finder = new Finder();
        $finder
            ->name('*schema.xml')
            ->sortByName()
            ->in($directory);
        if (!$recursive) {
            $finder->depth(0);
        }

        return iterator_to_array($finder->files());
    }

    /**
     * Returns a Filesystem instance.
     *
     * @return \Symfony\Component\Filesystem\Filesystem
     */
    protected function getFilesystem()
    {
        if ($this->filesystem === null) {
            $this->filesystem = new Filesystem();
        }

        return $this->filesystem;
    }

    /**
     * @param string $directory
     *
     * @throws \RuntimeException
     *
     * @return void
     */
    protected function createDirectory($directory)
    {
        $filesystem = $this->getFilesystem();

        try {
            $filesystem->mkdir($directory);
        } catch (IOException $e) {
            throw new RuntimeException(sprintf('Unable to write the "%s" directory', $directory), 0, $e);
        }
    }

    /**
     * Parses a connection string and returns an array with name, DNS and extra information.
     *
     * @param string $connection The connection string
     *
     * @return array
     */
    protected function parseConnection($connection)
    {
        $pos = strpos($connection, '=');
        $name = substr($connection, 0, $pos);
        $dsn = substr($connection, $pos + 1, strlen($connection));

        $pos = strpos($dsn, ':');
        $adapter = substr($dsn, 0, $pos);

        $extras = [];<commit_msg>Fix up acronym.<commit_after><?php

/**
 * MIT License. This file is part of the Propel package.
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Propel\Generator\Command;

use Propel\Generator\Config\GeneratorConfig;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

/**
 * @author <NAME> <<EMAIL>>
 */
abstract class AbstractCommand extends Command
{
    /**
     * @var string
     */
    public const DEFAULT_CONFIG_DIRECTORY = '.';

    /**
     * @var int
     */
    public const CODE_SUCCESS = 0;

    /**
     * @var int
     */
    public const CODE_ERROR = 1;

    /**
     * @var \Symfony\Component\Filesystem\Filesystem|null
     */
    protected $filesystem;

    /**
     * {@inheritDoc}
     *
     * @return void
     */
    protected function configure()
    {
        $this
            ->addOption('platform', null, InputOption::VALUE_REQUIRED, 'The platform to use. Define a full qualified class name or mysql|pgsql|sqlite|mssql|oracle.')
            ->addOption('config-dir', null, InputOption::VALUE_REQUIRED, 'The directory where the configuration file is placed.', self::DEFAULT_CONFIG_DIRECTORY)
            ->addOption('recursive', null, InputOption::VALUE_NONE, 'Search recursive for *schema.xml inside the input directory');
    }

    /**
     * Returns a new `GeneratorConfig` object with your `$properties` merged with
     * the configuration properties in the `config-dir` folder.
     *
     * @param array|null $properties Properties to add to the configuration. They usually come from command line.
     * @param \Symfony\Component\Console\Input\InputInterface|null $input
     *
     * @return \Propel\Generator\Config\GeneratorConfig
     */
    protected function getGeneratorConfig(?array $properties = null, ?InputInterface $input = null)
    {
        if ($input === null) {
            return new GeneratorConfig(null, $properties);
        }

        if ($this->hasInputOption('platform', $input)) {
            $properties['propel']['generator']['platformClass'] = $input->getOption('platform');
        }

        if ($input->hasParameterOption('--recursive')) {
            $properties['propel']['generator']['recursive'] = $input->getOption('recursive');
        }

        return new GeneratorConfig($input->getOption('config-dir'), $properties);
    }

    /**
     * Find every schema files.
     *
     * @param array<string>|string $directory Path to the input directory
     * @param bool $recursive Search for file inside the input directory and all subdirectories
     *
     * @return array List of schema files
     */
    protected function getSchemas($directory, $recursive = false)
    {
        $finder = new Finder();
        $finder
            ->name('*schema.xml')
            ->sortByName()
            ->in($directory);
        if (!$recursive) {
            $finder->depth(0);
        }

        return iterator_to_array($finder->files());
    }

    /**
     * Returns a Filesystem instance.
     *
     * @return \Symfony\Component\Filesystem\Filesystem
     */
    protected function getFilesystem()
    {
        if ($this->filesystem === null) {
            $this->filesystem = new Filesystem();
        }

        return $this->filesystem;
    }

    /**
     * @param string $directory
     *
     * @throws \RuntimeException
     *
     * @return void
     */
    protected function createDirectory($directory)
    {
        $filesystem = $this->getFilesystem();

        try {
            $filesystem->mkdir($directory);
        } catch (IOException $e) {
            throw new RuntimeException(sprintf('Unable to write the "%s" directory', $directory), 0, $e);
        }
    }

    /**
     * Parses a connection string and returns an array with name, DSN and extra information.
     *
     * @param string $connection The connection string
     *
     * @return array
     */
    protected function parseConnection($connection)
    {
        $pos = strpos($connection, '=');
        $name = substr($connection, 0, $pos);
        $dsn = substr($connection, $pos + 1, strlen($connection));

        $pos = strpos($dsn, ':');
        $adapter = substr($dsn, 0, $pos);

        $extras = [];
{'fraction_non_alphanumeric': 0.12627268702965913, 'fraction_numerical': 0.0015493581230633024, 'mean_word_length': 2.5522798742138364, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3219146', 'n_tokens_mistral': 2487, 'n_tokens_neox': 2429, 'n_words': 648}