Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

view-engine包中的loop是不是存在数据错乱的可能 #6921

Open
cceessff opened this issue Jun 29, 2024 · 3 comments
Open

view-engine包中的loop是不是存在数据错乱的可能 #6921

cceessff opened this issue Jun 29, 2024 · 3 comments
Labels
question Further information is requested

Comments

@cceessff
Copy link

<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  [email protected]
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace Hyperf\ViewEngine\Concern;

use Countable;
use Hyperf\Collection\Arr;
use stdClass;

trait ManagesLoops
{
    /**
     * The stack of in-progress loops.
     */
    protected array $loopsStack = [];

    /**
     * Add new loop to the stack.
     *
     * @param null|array|Countable $data
     */
    public function addLoop($data)
    {
        $length = is_countable($data) ? count($data) : null;

        $parent = Arr::last($this->loopsStack);

        $this->loopsStack[] = [
            'iteration' => 0,
            'index' => 0,
            'remaining' => $length ?? null,
            'count' => $length,
            'first' => true,
            'last' => isset($length) ? $length == 1 : null,
            'odd' => false,
            'even' => true,
            'depth' => count($this->loopsStack)   1,
            'parent' => $parent ? (object) $parent : null,
        ];
    }

    /**
     * Increment the top loop's indices.
     */
    public function incrementLoopIndices()
    {
        $loop = $this->loopsStack[$index = count($this->loopsStack) - 1];

        $this->loopsStack[$index] = array_merge($this->loopsStack[$index], [
            'iteration' => $loop['iteration']   1,
            'index' => $loop['iteration'],
            'first' => $loop['iteration'] == 0,
            'odd' => ! $loop['odd'],
            'even' => ! $loop['even'],
            'remaining' => isset($loop['count']) ? $loop['remaining'] - 1 : null,
            'last' => isset($loop['count']) ? $loop['iteration'] == $loop['count'] - 1 : null,
        ]);
    }

    /**
     * Pop a loop from the top of the loop stack.
     */
    public function popLoop()
    {
        array_pop($this->loopsStack);
    }

    /**
     * Get an instance of the last loop in the stack.
     *
     * @return null|stdClass|void
     */
    public function getLastLoop()
    {
        if ($last = Arr::last($this->loopsStack)) {
            return (object) $last;
        }
    }

    /**
     * Get the entire loop stack.
     *
     * @return array
     */
    public function getLoopStack()
    {
        return $this->loopsStack;
    }
}

这里的 $loopsStack 是所有携程共用的,如果 A协程往其中写入数据 a, B协程往其中写入数据b,然后再有多个携程区获取当前的loop,是不是获取到的可能不是自己协程的数据

@cceessff cceessff added the question Further information is requested label Jun 29, 2024
@tadink
Copy link

tadink commented Jul 18, 2024

这个问题没人知道吗? @limingxinleo @huangzhhui

@limingxinleo
Copy link
Member

这个有用到么?

@tadink
Copy link

tadink commented Jul 19, 2024

这个有用到么?

在模板中有用到,并且我还发现 Hyperf\ViewEngine\Factory 里面的 shared 属性可能会有同样的问题。在 Hyperf\ViewEngine\Http\Middleware\ShareErrorsFromSession 里面

$errors = $this->session->get('errors') ?: new ViewErrorBag();
$this->view->share('errors', $errors); 

这个中间件是每个请求都会执行的,这个 errors 不会被覆盖吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants