<commit_before><?php
/**
 * This is a part of Maketok Site. Licensed under GPL 3.0
 * Please do not use for your own profit.
 * @project site
 * @developer Slayer <EMAIL> maketok.com
 */

namespace Maketok\Installer\Ddl;

use Maketok\App\Site;
use Maketok\Installer\AbstractManager;
use Maketok\Installer\Ddl\Resource\Model\DdlClient;
use Maketok\Installer\Ddl\Resource\Model\DdlClientType;
use Maketok\Installer\Exception;
use Maketok\Installer\ManagerInterface;
use Maketok\Installer\ClientInterface as BaseClientInterface;
use Maketok\Installer\Ddl\ClientInterface as DdlClientInterface;
use Maketok\Util\StreamHandlerInterface;
use Monolog\Logger;

class Manager extends AbstractManager implements ManagerInterface
{
    /**
     * @var Logger
     */
    private $_logger;

    /**
     * Constructor
     * @param ConfigReaderInterface $reader
     * @param ResourceInterface $resource
     * @param Directives $directives
     * @param StreamHandlerInterface|null $handler
     * @param Logger $logger
     */
    public function __construct(ConfigReaderInterface $reader,
                                ResourceInterface $resource,
                                Directives $directives,
                                StreamHandlerInterface $handler = null,
                                Logger $logger)
    {
        $this->_reader = $reader;
        $this->_streamHandler = $handler;
        $this->_directives = $directives;
        if ($handler) {
            $this->_resource = $resource;
        }
        $this->_logger = $logger;
    }

    /**
     * {@inheritdoc}
     */
    public function addClient(BaseClientInterface $client)
    {
        if (!($client instanceof ClientInterface)) {
            throw new Exception("Wrong client type.");
        }
        if (is_null($this->_clients)) {
            $this->_clients = [];
        }
        $this->_logger->debug(sprintf("Client added: %s", $client->getDdlCode()));
        $this->_clients[$client->getDdlCode()] = $this->getClientModel($client);
    }

    /**
     * @param DdlClientInterface $client
     * @return DdlClient
     */
    public function getClientModel(DdlClientInterface $client)
    {
        try {
            /** @var DdlClientType $type */
            $type = Site::getServiceContainer()->get('ddl_client_table');
            $model = $type->getClientByCode($client->getDdlCode());
        } catch (\Exception $e) {
            // this is kind of a way to catch non existing table request
            // TODO need to find a better way in the future
            $model = new DdlClient();
        }
        $model->code = $client->getDdlCode();
        $model->version = $client->getDdlVersion();
        $model->config = $client->getDdlConfig($model->version);
        $model->dependencies = $client->getDependencies();
        return $model;
    }

    /**
     * This is where all clients are processed
     * @return void
     */
    public function process()
    {
        // lock process
        $this->_streamHandler->lock();
        try {
            // build tree
            $this->_reader->buildDependencyTree($this->_clients);
            $this->_logger->debug("Dependency Tree: %s", array(
                'tree' => $this->_reader->getDependencyTree(),
            ));
            // create directives
            $this->createDirectives();
            $this->_logger->debug("Directives: %s", array(
                'directives' => $this->_directives->asArray(),
            ));
            // create db procedures
            $this->_resource->createProcedures($this->_directives);

            $this->_logger->debug("Procedures: %s", array(
                'procedures' => $this->_resource->getProcedures(),
            ));
            // run
            $this->_resource->runProcedures();
            $this->_logger->debug("All procedures have been completed.");
        } catch (\Exception $e) {
            $this->_logger->err(sprintf("Exception while running DDL Installer process: %s", $e->__toString()));
        }
        $this->_streamHandler->unLock();
    }

    /**
     * @throws \LogicException
     * @return void
     */
    public function createDirectives()
    {
        $config = $this->_reader->getMergedConfig();
        $this->processValidateMergedConfig($config);
        $this->_logger->debug("Merged Config: %s", array(
            'config' => $config,
        ));
        foreach ($config as $table => $definition) {
            if (!isset($definition['columns']) || !is_array($definition['columns'])) {
                throw new \LogicException(sprintf('Can not have a table `%s` without columns definition.'
                    , $table));
            }
            $dbConfig = $this->_resource->getTable($table);
            // compare def with db
            if (empty($dbConfig)) {
                // add table
                $this->_directives->addProp('addTables', [$table, $definition]);
            } else {
                $_newColumns = $definition['columns'];
                $_oldColumns = $dbConfig['columns'];<commit_msg>add one more log level<commit_after><?php
/**
 * This is a part of Maketok Site. Licensed under GPL 3.0
 * Please do not use for your own profit.
 * @project site
 * @developer Slayer <EMAIL> maketok.com
 */

namespace Maketok\Installer\Ddl;

use Maketok\App\Site;
use Maketok\Installer\AbstractManager;
use Maketok\Installer\Ddl\Resource\Model\DdlClient;
use Maketok\Installer\Ddl\Resource\Model\DdlClientType;
use Maketok\Installer\Exception;
use Maketok\Installer\ManagerInterface;
use Maketok\Installer\ClientInterface as BaseClientInterface;
use Maketok\Installer\Ddl\ClientInterface as DdlClientInterface;
use Maketok\Util\StreamHandlerInterface;
use Monolog\Logger;

class Manager extends AbstractManager implements ManagerInterface
{
    /**
     * @var Logger
     */
    private $_logger;

    /**
     * Constructor
     * @param ConfigReaderInterface $reader
     * @param ResourceInterface $resource
     * @param Directives $directives
     * @param StreamHandlerInterface|null $handler
     * @param Logger $logger
     */
    public function __construct(ConfigReaderInterface $reader,
                                ResourceInterface $resource,
                                Directives $directives,
                                StreamHandlerInterface $handler = null,
                                Logger $logger)
    {
        $this->_reader = $reader;
        $this->_streamHandler = $handler;
        $this->_directives = $directives;
        if ($handler) {
            $this->_resource = $resource;
        }
        $this->_logger = $logger;
    }

    /**
     * {@inheritdoc}
     */
    public function addClient(BaseClientInterface $client)
    {
        if (!($client instanceof ClientInterface)) {
            throw new Exception("Wrong client type.");
        }
        if (is_null($this->_clients)) {
            $this->_clients = [];
        }
        $this->_logger->debug(sprintf("Client added: %s", $client->getDdlCode()));
        $this->_clients[$client->getDdlCode()] = $this->getClientModel($client);
    }

    /**
     * @param DdlClientInterface $client
     * @return DdlClient
     */
    public function getClientModel(DdlClientInterface $client)
    {
        try {
            /** @var DdlClientType $type */
            $type = Site::getServiceContainer()->get('ddl_client_table');
            $model = $type->getClientByCode($client->getDdlCode());
        } catch (\Exception $e) {
            // this is kind of a way to catch non existing table request
            // TODO need to find a better way in the future
            $model = new DdlClient();
        }
        $model->code = $client->getDdlCode();
        $model->version = $client->getDdlVersion();
        $model->config = $client->getDdlConfig($model->version);
        $model->dependencies = $client->getDependencies();
        return $model;
    }

    /**
     * This is where all clients are processed
     * @return void
     */
    public function process()
    {
        // lock process
        $this->_streamHandler->lock();
        try {
            // build tree
            $this->_reader->buildDependencyTree($this->_clients);
            $this->_logger->debug("Dependency Tree: %s", array(
                'tree' => $this->_reader->getDependencyTree(),
            ));
            // create directives
            $this->createDirectives();
            $this->_logger->debug("Directives: %s", array(
                'directives' => $this->_directives->asArray(),
            ));
            // create db procedures
            $this->_resource->createProcedures($this->_directives);

            $this->_logger->debug("Procedures: %s", array(
                'procedures' => $this->_resource->getProcedures(),
            ));
            // run
            $this->_resource->runProcedures();
            $this->_logger->debug("All procedures have been completed.");
        } catch (\Exception $e) {
            $this->_logger->err(sprintf("Exception while running DDL Installer process: %s", $e->__toString()));
        }
        $this->_streamHandler->unLock();
    }

    /**
     * @throws \LogicException
     * @return void
     */
    public function createDirectives()
    {
        $config = $this->_reader->getMergedConfig();
        $this->_logger->debug("Merged Config: %s", array(
            'config' => $config,
        ));
        $this->processValidateMergedConfig($config);
        $this->_logger->debug("Processed Merged Config: %s", array(
            'config' => $config,
        ));
        foreach ($config as $table => $definition) {
            if (!isset($definition['columns']) || !is_array($definition['columns'])) {
                throw new \LogicException(sprintf('Can not have a table `%s` without columns definition.'
                    , $table));
            }
            $dbConfig = $this->_resource->getTable($table);
            // compare def with db
            if (empty($dbConfig)) {
                // add table
                $this->_directives->addProp('addTables', [$table, $definition]);
            } else {
                $_newColumns = $definition['columns'];
                $_oldColumns = $dbConfig['columns'];
{'fraction_non_alphanumeric': 0.1339754816112084, 'fraction_numerical': 0.0003891807744697412, 'mean_word_length': 2.088641826923077, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 146, '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': '2349126', 'n_tokens_mistral': 2743, 'n_tokens_neox': 2707, 'n_words': 602}