<commit_before><?php

/*
 * Task runner, code generator and build tool for easier continuos integration
 *
 * @link      https://github.com/hiqdev/hidev
 * @package   hidev
 * @license   BSD-3-Clause
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
 */

namespace hidev\handlers;

use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;

/**
 * Base Handler.
 * Knows how to parse and render it's file type.
 */
class BaseHandler extends \yii\base\Object
{
    /**
     * @var Goal
     */
    public $goal;

    /**
     * @var string template file name to be used for rendering
     */
    public $template;

    /**
     * @var View object.
     */
    protected $_view;

    /**
     * Returns the view object that can be used to render views or view files.
     * If not set, it will default to the "view" application component.
     *
     * @return View|\yii\web\View the view object that can be used to render views or view files.
     */
    public function getView()
    {
        if ($this->_view === null) {
            $this->_view = Yii::$app->getView();
        }

        return $this->_view;
    }

    /**
     * Renders prepared data.
     * Must be redefined in child.
     *
     * @param array $data
     *
     * @throws InvalidConfigException
     *
     * @return string file content
     */
    public function renderPrepared(array $data)
    {
        throw new InvalidConfigException('Render not available');
    }

    /**
     * Renders raw data.
     * Prepares data with ArrayHelper::toArray.
     *
     * @param mixed $data
     *
     * @return string file content
     */
    public function render($data)
    {
        return $this->renderPrepared($this->prepareData($data));
    }

    public function prepareData($data)
    {
        return ArrayHelper::toArray($data, [], false);
        #return $data;
    }

    /**
     * Renders file content using given data.
     * Converts to array with ArrayHelper::toArray.
     *
     * @param mixed $data
     *
     * @return string file content
     */
    public function renderPath($path, $data)
    {
        return $this->write($path, $this->render($data));
    }

    public function parsePath($path, $minimal = null)
    {
        return $this->parse($this->read($path));
    }

    /**
     * Writes given content to the file.
     * TODO Creates intermediate directories when necessary.
     * @param string $path
     * @param string $content
     * @return bool true if file was changed
     */
    public function write($path, $content)
    {
        if (!is_file($path) || file_get_contents($path) !== $content) {
            Yii::warning('Written file: ' . $path, 'file');
            file_put_contents($path, $content);
            return true;
        }

        return false;
    }

    public function read($path, $asArray = false)
    {
        if (file_exists($path)) {
            Yii::info('Read file: ' . $path, 'file');

            return $asArray ? file($path) : file_get_contents($path);
        } else {
            Yii::error('Couldn\'t read file: ' . $path, 'file');

            return;
        }
    }

    public function readArray($path)
    {
        return $this->read($path, true);<commit_msg>* `BaseHandler`: + create intermediate directories when necessary before writing file<commit_after><?php

/*
 * Task runner, code generator and build tool for easier continuos integration
 *
 * @link      https://github.com/hiqdev/hidev
 * @package   hidev
 * @license   BSD-3-Clause
 * @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
 */

namespace hidev\handlers;

use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;

/**
 * Base Handler.
 * Knows how to parse and render it's file type.
 */
class BaseHandler extends \yii\base\Object
{
    /**
     * @var Goal
     */
    public $goal;

    /**
     * @var string template file name to be used for rendering
     */
    public $template;

    /**
     * @var View object.
     */
    protected $_view;

    /**
     * Returns the view object that can be used to render views or view files.
     * If not set, it will default to the "view" application component.
     *
     * @return View|\yii\web\View the view object that can be used to render views or view files.
     */
    public function getView()
    {
        if ($this->_view === null) {
            $this->_view = Yii::$app->getView();
        }

        return $this->_view;
    }

    /**
     * Renders prepared data.
     * Must be redefined in child.
     *
     * @param array $data
     *
     * @throws InvalidConfigException
     *
     * @return string file content
     */
    public function renderPrepared(array $data)
    {
        throw new InvalidConfigException('Render not available');
    }

    /**
     * Renders raw data.
     * Prepares data with ArrayHelper::toArray.
     *
     * @param mixed $data
     *
     * @return string file content
     */
    public function render($data)
    {
        return $this->renderPrepared($this->prepareData($data));
    }

    public function prepareData($data)
    {
        return ArrayHelper::toArray($data, [], false);
        #return $data;
    }

    /**
     * Renders file content using given data.
     * Converts to array with ArrayHelper::toArray.
     *
     * @param mixed $data
     *
     * @return string file content
     */
    public function renderPath($path, $data)
    {
        return $this->write($path, $this->render($data));
    }

    public function parsePath($path, $minimal = null)
    {
        return $this->parse($this->read($path));
    }

    /**
     * Writes given content to the file.
     * Doesn't touch file if it has exactly same content.
     * Creates intermediate directories when necessary.
     * @param string $path
     * @param string $content
     * @return bool true if file was changed
     */
    public function write($path, $content)
    {
        if (!is_file($path) || file_get_contents($path) !== $content) {
            $this->mkdir(dirname($path));
            file_put_contents($path, $content);
            Yii::warning('Written file: ' . $path, 'file');

            return true;
        }

        return false;
    }

    /**
     * Creates directory if not exists.
     * @param string $path
     * @return bool true if directory did not exist and was created
     */
    public function mkdir($path)
    {
        $path = trim(trim($path), '/');
        if (!file_exists($path)) {
            mkdir($path, 0777, true);
            Yii::warning('Created dir:  ' . $path . '/', 'file');

            return true;
        }

        return false;
    }

    public function read($path, $asArray = false)
    {
        if (file_exists($path)) {
            Yii::info('Read file: ' . $path, 'file');

            return $asArray ? file($path) : file_get_contents($path);
        } else {
            Yii::error('Couldn\'t read file: ' . $path, 'file');

            return;
        }
    }

    public function readArray($path)
    {
        return $this->read($path, true);
{'fraction_non_alphanumeric': 0.1267764176164345, 'fraction_numerical': 0.0030955396088363585, 'mean_word_length': 2.0637931034482757, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, 'https://': 2, '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': '2240777', 'n_tokens_mistral': 2113, 'n_tokens_neox': 2049, 'n_words': 602}