<commit_before><?php

namespace luya\components;

use Yii;
use Exception;

/**
 * Ability to register small html elements via closure function and run those
 * parts an every part of the page.
 * 
 * ```php
 * Yii::$app->element->addElement('foo', function($param) {
 *     return '<button>' . $param . '</button>';
 * });
 * ```
 * 
 * The above example can be execute like this:
 * 
 * ```php
 * echo Yii::$app->element->foo('Hello World');
 * ```
 * 
 * or in Twig
 * 
 * ```twig
 * {{ element('foo', 'Hello World') }}
 * ```
 * 
 * By default the Element-Component will lookup for an `elements.php` file returning an array
 * where the key is the element name and value the closure to be execute.
 * 
 * Example elements.php
 * 
 * ```php
 * <?php
 * return [
 *    'button' => function($value, $link) {
 *        return '<a class="btn btn-primary" href="'.$link.'">'.$value.'</a>';  
 *    },
 *    'teaserbox' => function($title, $text, $buttonValue, $buttonLink) {
 *        return '<div class="teaser-box" style="padding:10px; border:1px solid red;"><h1>'.$title.'</h1><p>'.$text.'</p>'.$this->button($buttonValue, $buttonLink).'</div>';
 *    },
 * ];
 * ```
 * 
 * @author nadar
 */
class Element extends \yii\base\Component
{
    public $configFile = '@app/elements.php';
    
    private $_elements = [];
    
    private $_folder = null;
    
    public function init()
    {
        $path = Yii::getAlias($this->configFile);
        if (file_exists($path)) {
            $config = (include($path));
            foreach($config as $name => $closure) {
                $this->addElement($name, $closure);
            }
        }
    }
    
    public function addElement($name, $closure)
    {
        $this->_elements[$name] = $closure;
    }
    
    public function __call($name, $params)
    {
        return $this->run($name, $params);
    }
    
    public function run($name, array $params = [])
    {
        if (!array_key_exists($name, $this->_elements)) {
            throw new Exception("The requested element '$name' does not exists in the element list. You may register the element first with `addElement(name, closure)`.");
        }
        
        $func = $this->_elements[$name];
        return call_user_func_array($func, $params);
    }
    
    public function getFolder()
    {
        if ($this->_folder === null) {
            $this->_folder = Yii::getAlias('@app/views/elements/');
        }<commit_msg>added Element component getNames() method.<commit_after><?php

namespace luya\components;

use Yii;
use Exception;

/**
 * Ability to register small html elements via closure function and run those
 * parts an every part of the page.
 * 
 * ```php
 * Yii::$app->element->addElement('foo', function($param) {
 *     return '<button>' . $param . '</button>';
 * });
 * ```
 * 
 * The above example can be execute like this:
 * 
 * ```php
 * echo Yii::$app->element->foo('Hello World');
 * ```
 * 
 * or in Twig
 * 
 * ```twig
 * {{ element('foo', 'Hello World') }}
 * ```
 * 
 * By default the Element-Component will lookup for an `elements.php` file returning an array
 * where the key is the element name and value the closure to be execute.
 * 
 * Example elements.php
 * 
 * ```php
 * <?php
 * return [
 *    'button' => function($value, $link) {
 *        return '<a class="btn btn-primary" href="'.$link.'">'.$value.'</a>';  
 *    },
 *    'teaserbox' => function($title, $text, $buttonValue, $buttonLink) {
 *        return '<div class="teaser-box" style="padding:10px; border:1px solid red;"><h1>'.$title.'</h1><p>'.$text.'</p>'.$this->button($buttonValue, $buttonLink).'</div>';
 *    },
 * ];
 * ```
 * 
 * @author nadar
 */
class Element extends \yii\base\Component
{
    public $configFile = '@app/elements.php';
    
    private $_elements = [];
    
    private $_folder = null;
    
    public function init()
    {
        $path = Yii::getAlias($this->configFile);
        if (file_exists($path)) {
            $config = (include($path));
            foreach($config as $name => $closure) {
                $this->addElement($name, $closure);
            }
        }
    }
    
    public function addElement($name, $closure)
    {
        $this->_elements[$name] = $closure;
    }
    
    /**
     * returns all element names to use with run without their closre function (would not work in
     * the context returned from getNames()).
     * 
     * @return array
     */
    public function getNames()
    {
        $list = [];
        foreach($this->_elements as $name => $closure) {
            $list[] = $name;
        }
        return $list;
    }
    
    public function __call($name, $params)
    {
        return $this->run($name, $params);
    }
    
    public function run($name, array $params = [])
    {
        if (!array_key_exists($name, $this->_elements)) {
            throw new Exception("The requested element '$name' does not exists in the element list. You may register the element first with `addElement(name, closure)`.");
        }
        
        $func = $this->_elements[$name];
        return call_user_func_array($func, $params);
    }
    
    public function getFolder()
    {
        if ($this->_folder === null) {
            $this->_folder = Yii::getAlias('@app/views/elements/');
        }
{'fraction_non_alphanumeric': 0.17063942489595157, 'fraction_numerical': 0.0018917896329928112, 'mean_word_length': 2.4600785340314135, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 57, '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': '1432178', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1601, 'n_words': 419}