<commit_before><?php

namespace CakeOrm\Action;

use App\Action\AppAction;
use Cake\Database\Schema\Table;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
use DOMDocument;
use DOMElement;
use DOMXPath;
use League\CLImate\CLImate;
use Rad\Core\Bundles;
use Rad\Utility\Inflection;

/**
 * Build Action
 *
 * @package RadBundle\CakeOrm\Action
 */
class BuildAction extends AppAction
{
    protected $tableRegistryCode = [];
    protected $tablesModel = [];

    /**
     * Cli method
     *
     * @param CLImate $climate
     *
     * @throws \Rad\Core\Exception\MissingBundleException
     */
    public function cliMethod(CLImate $climate)
    {
        $sqlDir = VAR_DIR . DS . 'cake_orm' . DS . 'sql';
        if (!is_dir($sqlDir)) {
            mkdir($sqlDir, 0777, true);
        }

        $sql = [];
        foreach (Bundles::getLoaded() as $bundle) {
            $climate->info(sprintf('Bundle %s ...', $bundle));
            $path = Bundles::getPath($bundle);
            $sqlFilename = Inflection::underscore($bundle) . '.sql';
            $schemaPath = $path . DS . 'Resource' . DS . 'config' . DS . 'schema.xml';

            if (!is_file($schemaPath)) {
                $climate->lightRed(sprintf('File "%s" does not exists.', $schemaPath));
                continue;
            }

            $domDocument = new DOMDocument();
            $domDocument->load($schemaPath);

            $xpath = new DOMXPath($domDocument);
            $tables = $xpath->query('/database/table');

            /** @var DOMElement $table */
            foreach ($tables as $table) {
                $schemaTable = new Table($table->getAttribute('name'));

                $this->prepareTableRegistry($table, $bundle);
                $this->prepareColumn($table, $xpath, $schemaTable);
                $this->prepareForeignConstraint($table, $xpath, $schemaTable);
                $this->prepareUniqueConstraint($table, $xpath, $schemaTable);
                $this->preparePrimaryConstraint($table, $xpath, $schemaTable);
                $this->prepareIndex($table, $xpath, $schemaTable);

                $sql[$sqlDir . DS . $sqlFilename][] = $schemaTable->createSql(ConnectionManager::get('default'));
            }

            foreach ($sql as $file => $tablesSql) {
                $tmpSql = '';
                foreach ($tablesSql as $tableSql) {
                    $tmpSql .= implode(";\n", $tableSql);
                    $tmpSql .= "\n\n";
                }

                file_put_contents($file, $tmpSql);
                $climate->lightGray(sprintf('Create SQL file "%s".', $file));
            }

            $climate->info('Dump table registry config ...');
            $this->dumpTableRegistry($climate, $bundle);

            $climate->info('Dump model classes ...');
            $this->dumpModelClasses($climate, $bundle);
            $climate->br(2);
        }
    }

    /**
     * Prepare table registry
     *
     * @param DOMElement $table
     * @param string     $bundle Bundle name
     */
    protected function prepareTableRegistry(DOMElement $table, $bundle)
    {
        if (($tableName = $table->getAttribute('name')) !== '') {
            $tmpTableClass = null;
            if (!empty($tableClass = $table->getAttribute('tableClass'))) {<commit_msg>Fix some bugs<commit_after><?php

namespace CakeOrm\Action;

use App\Action\AppAction;
use Cake\Database\Schema\Table;
use Cake\Datasource\ConnectionManager;
use DOMDocument;
use DOMElement;
use DOMXPath;
use League\CLImate\CLImate;
use Rad\Core\Bundles;
use Rad\Utility\Inflection;

/**
 * Build Action
 *
 * @package RadBundle\CakeOrm\Action
 */
class BuildAction extends AppAction
{
    protected $tableRegistryCode = [];
    protected $tablesModel = [];

    /**
     * Cli method
     *
     * @param CLImate $climate
     *
     * @throws \Rad\Core\Exception\MissingBundleException
     */
    public function cliMethod(CLImate $climate)
    {
        $sqlDir = VAR_DIR . DS . 'cake_orm' . DS . 'sql';
        if (!is_dir($sqlDir)) {
            mkdir($sqlDir, 0777, true);
        }

        $sql = [];
        foreach (Bundles::getLoaded() as $bundle) {
            $climate->backgroundLightGray()->info(sprintf('Bundle %s ...', $bundle));
            $path = Bundles::getPath($bundle);
            $sqlFilename = Inflection::underscore($bundle) . '.sql';
            $schemaPath = $path . DS . 'Resource' . DS . 'config' . DS . 'schema.xml';

            if (!is_file($schemaPath)) {
                $climate->lightRed(sprintf('File "%s" does not exists.', $schemaPath));
                $climate->br(2);
                continue;
            }

            $domDocument = new DOMDocument();
            $domDocument->load($schemaPath);

            $xpath = new DOMXPath($domDocument);
            $tables = $xpath->query('/database/table');

            /** @var DOMElement $table */
            foreach ($tables as $table) {
                $schemaTable = new Table($table->getAttribute('name'));

                $this->prepareTableRegistry($table, $bundle);
                $this->prepareColumn($table, $xpath, $schemaTable);
                $this->prepareForeignConstraint($table, $xpath, $schemaTable);
                $this->prepareUniqueConstraint($table, $xpath, $schemaTable);
                $this->preparePrimaryConstraint($table, $xpath, $schemaTable);
                $this->prepareIndex($table, $xpath, $schemaTable);

                $sql[$sqlDir . DS . $sqlFilename][] = $schemaTable->createSql(ConnectionManager::get('default'));
            }

            foreach ($sql as $file => $tablesSql) {
                $tmpSql = '';
                foreach ($tablesSql as $tableSql) {
                    $tmpSql .= implode(";\n", $tableSql);
                    $tmpSql .= "\n\n";
                }

                file_put_contents($file, $tmpSql);
                $climate->lightGray(sprintf('Create SQL file "%s".', $file));
            }
            $sql = [];

            $climate->info('Dump table registry config ...');
            $this->dumpTableRegistry($climate, $bundle);

            $climate->info('Dump model classes ...');
            $this->dumpModelClasses($climate, $bundle);
            $climate->br(2);
        }
    }

    /**
     * Prepare table registry
     *
     * @param DOMElement $table
     * @param string     $bundle Bundle name
     */
    protected function prepareTableRegistry(DOMElement $table, $bundle)
    {
        if (($tableName = $table->getAttribute('name')) !== '') {
            $tmpTableClass = null;
            if (!empty($tableClass = $table->getAttribute('tableClass'))) {
{'fraction_non_alphanumeric': 0.15141530627527333, 'fraction_numerical': 0.0016474464579901153, 'mean_word_length': 2.059092991296381, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '7282061', 'n_tokens_mistral': 1937, 'n_tokens_neox': 1858, 'n_words': 374}