<commit_before><?php

namespace Nails\Cron\Console\Command\Controller;

use Nails\Cron\Exception\Console\ControllerExistsException;
use Nails\Console\Command\BaseMaker;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Create extends BaseMaker
{
    const RESOURCE_PATH   = NAILS_PATH . 'module-cron/resources/console/';
    const CONTROLLER_PATH = FCPATH . 'application/modules/cron/controllers/';

    // --------------------------------------------------------------------------

    /**
     * Configure the command
     */
    protected function configure()
    {
        $this->setName('make:controller:cron');
        $this->setDescription('Creates a new Cron controller');
        $this->addArgument(
            'className',
            InputArgument::OPTIONAL,
            'Define the name of the model on which to base the controller'
        );
    }

    // --------------------------------------------------------------------------

    /**
     * Executes the app
     *
     * @param  InputInterface $oInput The Input Interface provided by Symfony
     * @param  OutputInterface $oOutput The Output Interface provided by Symfony
     * @return int
     */
    protected function execute(InputInterface $oInput, OutputInterface $oOutput)
    {
        parent::execute($oInput, $oOutput);

        // --------------------------------------------------------------------------

        try {
            //  Ensure the paths exist
            $this->createPath(self::CONTROLLER_PATH);
            //  Create the controller
            $this->createController();
        } catch (\Exception $e) {
            return $this->abort(
                self::EXIT_CODE_FAILURE,
                $e->getMessage()
            );
        }

        // --------------------------------------------------------------------------

        //  Cleaning up
        $oOutput->writeln('');
        $oOutput->writeln('<comment>Cleaning up...</comment>');

        // --------------------------------------------------------------------------

        //  And we're done
        $oOutput->writeln('');
        $oOutput->writeln('Complete!');

        return self::EXIT_CODE_SUCCESS;
    }

    // --------------------------------------------------------------------------

    /**
     * Create the Model
     *
     * @throws \Exception
     * @return void
     */
    private function createController()
    {
        $aFields = $this->getArguments();

        try {

            //  Check for existing controller
            $sPath  = static::CONTROLLER_PATH . $aFields['CLASS_NAME'] . '.php';
            if (file_exists($sPath)) {
                throw new ControllerExistsException(
                    'Controller "' . $aFields['CLASS_NAME'] . '" exists already at path "' . $sPath . '"'
                );
            }

            $this->createFile($sPath, $this->getResource('template/controller.php', $aFields));

        } catch (ControllerExistsException $e) {
            //  Do not clean up (delete existing controller)!
            throw new \Exception($e->getMessage(), $e->getCode());
        } catch (\Exception $e) {
            //  Clean up
            if (!empty($sPath)) {
                @unlink($sPath);
            }
            throw new \Exception($e->getMessage());
        }
    }
}<commit_msg>Allowing multiple controllers to be made at once by specifying the names as CSVs<commit_after><?php

namespace Nails\Cron\Console\Command\Controller;

use Nails\Console\Command\BaseMaker;
use Nails\Cron\Exception\Console\ControllerExistsException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Create extends BaseMaker
{
    const RESOURCE_PATH   = NAILS_PATH . 'module-cron/resources/console/';
    const CONTROLLER_PATH = FCPATH . 'application/modules/cron/controllers/';

    // --------------------------------------------------------------------------

    /**
     * Configure the command
     */
    protected function configure()
    {
        $this->setName('make:controller:cron');
        $this->setDescription('Creates a new Cron controller');
        $this->addArgument(
            'className',
            InputArgument::OPTIONAL,
            'Define the name of the model on which to base the controller'
        );
    }

    // --------------------------------------------------------------------------

    /**
     * Executes the app
     *
     * @param  InputInterface $oInput The Input Interface provided by Symfony
     * @param  OutputInterface $oOutput The Output Interface provided by Symfony
     * @return int
     */
    protected function execute(InputInterface $oInput, OutputInterface $oOutput)
    {
        parent::execute($oInput, $oOutput);

        // --------------------------------------------------------------------------

        try {
            //  Ensure the paths exist
            $this->createPath(self::CONTROLLER_PATH);
            //  Create the controller
            $this->createController();
        } catch (\Exception $e) {
            return $this->abort(
                self::EXIT_CODE_FAILURE,
                $e->getMessage()
            );
        }

        // --------------------------------------------------------------------------

        //  Cleaning up
        $oOutput->writeln('');
        $oOutput->writeln('<comment>Cleaning up...</comment>');

        // --------------------------------------------------------------------------

        //  And we're done
        $oOutput->writeln('');
        $oOutput->writeln('Complete!');

        return self::EXIT_CODE_SUCCESS;
    }

    // --------------------------------------------------------------------------

    /**
     * Create the Model
     *
     * @throws \Exception
     * @return void
     */
    private function createController()
    {
        $aFields  = $this->getArguments();
        $aCreated = [];

        try {

            $aClasses = array_filter(explode(',', $aFields['CLASS_NAME']));

            foreach ($aClasses as $sClass) {

                $aFields['CLASS_NAME'] = $sClass;
                $this->oOutput->write('Creating controller <comment>' . $sClass . '</comment>... ');

                //  Check for existing controller
                $sPath = static::CONTROLLER_PATH . $sClass . '.php';
                if (file_exists($sPath)) {
                    throw new ControllerExistsException(
                        'Controller "' . $sClass . '" exists already at path "' . $sPath . '"'
                    );
                }

                $this->createFile($sPath, $this->getResource('template/controller.php', $aFields));
                $aCreated[] = $sPath;
                $this->oOutput->writeln('<info>done!</info>');
            }

        } catch (\Exception $e) {
            $this->oOutput->writeln('<error>failed!</error>');
            //  Clean up created models
            if (!empty($aCreated)) {
                $this->oOutput->writeln('<error>Cleaning up - removing newly created controllers</error>');
                foreach ($aCreated as $sPath) {
                    @unlink($sPath);
                }
            }
            throw new \Exception($e->getMessage());
        }
    }
}
{'fraction_non_alphanumeric': 0.2317532992189604, 'fraction_numerical': 0.0, 'mean_word_length': 1.3282131661442007, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '964638', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1769, 'n_words': 376}