<commit_before><?php

declare(strict_types=1);

/*
 * This file is part of the ************************ package.
 * _____________                           _______________
 *  ______/     \__  _____  ____  ______  / /_  _________
 *   ____/ __   / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
 *    __/ / /  / /_/ /  __/ /  \  / /_/ / / / / /_/ /__
 *      \_\ \_/\____/\___/_/   / / .___/_/ /_/ .___/
 *         \_\                /_/_/         /_/
 *
 * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
 * (c) 2010-2019 http://queryphp.com All rights reserved.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Leevel\Event;

use Closure;
use InvalidArgumentException;
use SplObserver;
use SplSubject;

/**
 * 观察者角色 observer.
 *
 * @author <NAME> <<EMAIL>>
 *
 * @since 2017.06.23
 * @see http://php.net/manual/zh/class.splobserver.php
 *
 * @version 1.0
 */
class Observer implements SplObserver
{
    /**
     * 观察者目标角色 subject.
     *
     * @var \SplSubject
     */
    protected $subject;

    /**
     * 观察者实现.
     *
     * @var \Closure
     */
    protected $handle;

    /**
     * 构造函数.
     *
     * @param null|\Closure $handle
     */
    public function __construct(?Closure $handle = null)
    {
        $this->handle = $handle;
    }

    /**
     * 观察者实现.
     *
     * @param array ...$args
     */
    public function __invoke(...$args)
    {
        call_user_func($this->handle, ...$args);
    }

    /**
     * {@inheritdoc}
     *
     * @throws \InvalidArgumentException
     */
    public function update(SplSubject $subject): void
    {
        if (method_exists($this, 'handle')) {
            $handle = [$this, 'handle'];
        } elseif ($this->handle) {
            $handle = [$this, '__invoke'];
        } else {
            $handle = null;
        }

        if (!is_callable($handle)) {
            $e = sprintf('Observer %s must has handle method.', get_class($this));

            throw new InvalidArgumentException($e);
        }

        $subject->container->call($handle, $subject->notifyArgs);
    }
}<commit_msg>perf(event): improves observer performance<commit_after><?php

declare(strict_types=1);

/*
 * This file is part of the ************************ package.
 * _____________                           _______________
 *  ______/     \__  _____  ____  ______  / /_  _________
 *   ____/ __   / / / / _ \/ __`\/ / __ \/ __ \/ __ \___
 *    __/ / /  / /_/ /  __/ /  \  / /_/ / / / / /_/ /__
 *      \_\ \_/\____/\___/_/   / / .___/_/ /_/ .___/
 *         \_\                /_/_/         /_/
 *
 * The PHP Framework For Code Poem As Free As Wind. <Query Yet Simple>
 * (c) 2010-2019 http://queryphp.com All rights reserved.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Leevel\Event;

use Closure;
use InvalidArgumentException;
use SplObserver;
use SplSubject;

/**
 * 观察者角色 observer.
 *
 * @author <NAME> <<EMAIL>>
 *
 * @since 2017.06.23
 * @see http://php.net/manual/zh/class.splobserver.php
 *
 * @version 1.0
 */
class Observer implements SplObserver
{
    /**
     * 观察者目标角色 subject.
     *
     * @var \SplSubject
     */
    protected $subject;

    /**
     * 观察者实现.
     *
     * @var \Closure
     */
    protected $handle;

    /**
     * 构造函数.
     *
     * @param null|\Closure $handle
     */
    public function __construct(?Closure $handle = null)
    {
        $this->handle = $handle;
    }

    /**
     * 观察者实现.
     *
     * @param array ...$args
     */
    public function __invoke(...$args)
    {
        $handle = $this->handle;
        $handle(...$args);
    }

    /**
     * {@inheritdoc}
     *
     * @throws \InvalidArgumentException
     */
    public function update(SplSubject $subject): void
    {
        if (method_exists($this, 'handle')) {
            $handle = [$this, 'handle'];
        } elseif ($this->handle) {
            $handle = [$this, '__invoke'];
        } else {
            $handle = null;
        }

        if (!is_callable($handle)) {
            $e = sprintf('Observer %s must has handle method.', get_class($this));

            throw new InvalidArgumentException($e);
        }

        $subject->container->call($handle, $subject->notifyArgs);
    }
}
{'fraction_non_alphanumeric': 0.16391941391941392, 'fraction_numerical': 0.0086996336996337, 'mean_word_length': 1.6935881627620222, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '4866426', 'n_tokens_mistral': 1493, 'n_tokens_neox': 1422, 'n_words': 284}