<commit_before><?php
namespace MapasCulturais\Traits;

use Error;
use MapasCulturais\App;

/**
 * Defines the magic callers that returns false if a method starts with **uses**
 */
trait MagicCallers {
    
    /**
     * This class uses magic callers
     * 
     * @return true
     */
    public static function usesMagicCallers(){
        return true;
    }

    /**
     * Magic Call
     *
     * Returns false to all methods that starts with **uses** (traits must define a method like usesTraitName() that returns true)
     *
     * @param string $name the name of the method that was called.
     * @param array $arguments the params passed to the method
     * @return mixed
     */
    public function __call($name, $arguments) {
        $app = App::i();
        
        if(method_exists($this, 'getHookClassPath')) {
            $class = $this->getHookClassPath();
        } else if(method_exists($this,'getClassName')){
            $class = $this->getClassName();
        } else {
            $class = get_called_class();
        }

        if (substr($name, 0, 4) === 'uses') {
            return false;
        } else if ($hooks = $app->_getHookCallables("{$class}::{$name}")) {
            $result = null;
            foreach ($hooks as $callable) {
                $callable = \Closure::bind($callable, $this);
                $args = [&$result];
                foreach($arguments as $arg){
                    $args[] = $arg;
                }
                call_user_func_array($callable, $args);
            }

            return $result;
        } else {
            $class = self::class;
            $trace = debug_backtrace();
            $l = $trace[0];
            throw new Error("Call to undefined method {$class}::{$name}() in {$l['file']} on line {$l['line']}");
        }
    }

    /**
     * Magic Static Call
     *
     * Returns false to all methods that starts with **uses** (traits must define a method like usesTraitName() that returns true)
     *
     * @param string $name the name of the method that was called.
     * @param array $arguments the params passed to the method
     * @return mixed
     */
    static public function __callStatic($name, $arguments){
        $app = App::i();

        $class = get_called_class();
        
        if(method_exists($class, 'getHookClassPath')) {
            $class = $class::getHookClassPath();
        } else if(method_exists($class, 'getClassName')){
            $class = $class::getClassName();
        } 

        if (substr($name, 0, 4) === 'uses') {
            return false;
        } else if ($hooks = $app->_getHookCallables("{$class}::{$name}")) {
            $result = null;
            foreach ($hooks as $callable) {
                $args = [&$result];
                foreach($arguments as $arg){
                    $args[] = $arg;
                }
                call_user_func_array($callable, $args);
            }

            return $result;
        } else {
            $class = self::class;
            $trace = debug_backtrace();
            $l = $trace[0];
            throw new Error("Call to undefined static method {$class}::{$name}() in {$l['file']} on line {$l['line']}");
        }
    }
}<commit_msg>refatora hooks do MagicCaller para considerar as parent classes (refs #1888)<commit_after><?php
namespace MapasCulturais\Traits;

use Error;
use MapasCulturais\App;

/**
 * Defines the magic callers that returns false if a method starts with **uses**
 */
trait MagicCallers {
    
    /**
     * This class uses magic callers
     * 
     * @return true
     */
    public static function usesMagicCallers(){
        return true;
    }

    private static function __getMagicCallersHooks($name) {
        $app = App::i();

        $class = get_called_class();
        if(method_exists($class,'getClassName')){
            $class = $class::getClassName();
        }
        $classes = class_parents($class);
        array_unshift($classes, $class);

        $classes = array_map(function($class) { 
            return str_replace('MapasCulturais\\', '', $class);
        }, $classes);

        $hooks = [];

        foreach ($classes as $class) {
            $class_hooks = $app->_getHookCallables("{$class}::{$name}");
            $hooks = array_merge($class_hooks, $hooks);
        }

        return $hooks;
    }

    /**
     * Magic Call
     *
     * Returns false to all methods that starts with **uses** (traits must define a method like usesTraitName() that returns true)
     *
     * @param string $name the name of the method that was called.
     * @param array $arguments the params passed to the method
     * @return mixed
     */
    public function __call($name, $arguments) {

        if (substr($name, 0, 4) === 'uses') {
            return false;
        } else if ($hooks = $this->__getMagicCallersHooks($name)) {
            $result = null;
            foreach ($hooks as $callable) {
                $callable = \Closure::bind($callable, $this);
                $args = [$result];
                foreach($arguments as $arg){
                    $args[] = $arg;
                }
                $result = call_user_func_array($callable, $args);
            }

            return $result;
        } else {
            $class = self::class;
            $trace = debug_backtrace();
            $l = $trace[0];
            throw new Error("Call to undefined method {$class}::{$name}() in {$l['file']} on line {$l['line']}");
        }
    }

    /**
     * Magic Static Call
     *
     * Returns false to all methods that starts with **uses** (traits must define a method like usesTraitName() that returns true)
     *
     * @param string $name the name of the method that was called.
     * @param array $arguments the params passed to the method
     * @return mixed
     */
    static public function __callStatic($name, $arguments){
        
        if (substr($name, 0, 4) === 'uses') {
            return false;
        } else if ($hooks = self::__getMagicCallersHooks($name)) {
            $result = null;
            foreach ($hooks as $callable) {
                $args = [$result];
                foreach($arguments as $arg){
                    $args[] = $arg;
                }
                $result = call_user_func_array($callable, $args);
            }

            return $result;
        } else {
            $class = self::class;
            $trace = debug_backtrace();
            $l = $trace[0];
            throw new Error("Call to undefined static method {$class}::{$name}() in {$l['file']} on line {$l['line']}");
        }
    }
}
{'fraction_non_alphanumeric': 0.13569589580493716, 'fraction_numerical': 0.002423140996516735, 'mean_word_length': 1.8638334778837815, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '1793403', 'n_tokens_mistral': 1881, 'n_tokens_neox': 1829, 'n_words': 529}