Read partial file content as string and store in context

@param array<string,array|string|integer> $context Current context of compiler progress.
@param string $name partial name

@return string|null $code compiled PHP code when success
438894
public static function read(&$context, $name) {
        $isPB = ($name === '@partial-block');
        $context['usedFeature']['partial']++;

        if (isset($context['usedPartial'][$name])) {
            return;
        }

        $cnt = static::resolve($context, $name);

        if ($cnt !== null) {
            $context['usedPartial'][$name] = SafeString::escapeTemplate($cnt);
            return static::compileDynamic($context, $name);
        }

        if (!$context['flags']['skippartial'] && !$isPB) {
            $context['error'][] = "Can not find partial for '$name', you should provide partials or partialresolver in options";
        }
    }
659474
final public function output(string $name = null)
    {
        $result = implode(static::EOL, $this->registries) . static::EOL
            . static::EOF;

        if ($name === null) {
            return $result;
        }

        Utils::checkOutput(pathinfo($name)['extension'] ?? '');

        header('Content-Type: text/plain');
        header('Content-Length: ' . strlen($result));
        header('Content-Disposition: attachment; filename="' . $name . '"');
        echo $result;
    }
1354387
private static function getCoroutineContext(string $name)
    {
        $coroutineId = self::getcoroutine();
        if (!isset(self::$coroutineLocal[$coroutineId])) {
            throw new InvalidValueException("协程上下文不存在，coroutineId=".$coroutineId);
        }

        $coroutineContext = self::$coroutineLocal[$coroutineId];
        if (isset($coroutineContext[$name])) {
            return $coroutineContext[$name];
        }
        return null;
    }
1039201
public function render($name, array $context = [])
    {
        $renderedHtml = $this->twig->render($name, $context);

        foreach ($this->postProcessors as $postProcessor) {
            if ($postProcessor instanceof PostProcessorInterface) {
                $renderedHtml = $postProcessor->process($renderedHtml, $name, $context, $this->twig);
            }
        }

        return $renderedHtml;
    }
173772
public function config_read($name) {
        global $CFG;
        if (!empty($this->plugin)) {
            $value = get_config($this->plugin, $name);
            return $value === false ? NULL : $value;

        } else {
            if (isset($CFG->$name)) {
                return $CFG->$name;
            } else {
                return NULL;
            }
        }
    }
534169
private function readMetaData($name) {
        $refData = [
            'ID' => '',
            'Size' => 0,
            'Offset' => 0,
            'Extension' => '',
            'FileName' => '',
            'MimeType' => '',
            'IsPartial' => true,
            'IsFinal' => false,
            'PartialUploads' => null, // unused
        ];

        $storageFileName = $this->directory . $name . '.info';
        if (file_exists($storageFileName)) {
            $json = file_get_contents($storageFileName);
            $data = Json::decode($json, Json::TYPE_ARRAY);
            if (is_array($data)) {
                return array_merge($refData, $data);
            }
        }
        return $refData;
    }
518179
public function render($name, array $context = [])
    {
        $template = $this->environment->loadTemplate($name);
        return $template->render($context);
    }
265099
public function getField(string $name)
    {
        $name = trim($name);
        if (!$this->isFieldSet($name)) {
            return null;
        }

        $field_key = array_search($name, $this->keys);
        return $this->method[$field_key] ?? $_FILES[$field_key];
    }
17726
protected function getFileFromField(
        $name,
        $filename,
        $unverifiedContentType
    )
    {
        # create and open a temp file to write the contents to
        $tempPath = $this->getTempFilePath();
        $tempHandle = fopen($tempPath, "w");
        if ($tempHandle === false) {
            fclose($this->fp);
            throw new \RuntimeException("failed to open '$tempPath' for writing");
        }

        # number of bytes read from the input stream in the last loop cycle
        $bytesRead = 0;
        # the total number of bytes written to the temp file
        $bytesWritten = 0;

        # if anything is left over from the previous field, add it to the file
        if ($this->buffer !== "") {
            $bytesRead = fwrite($tempHandle, $this->buffer);
            if ($bytesRead === false) {
                fclose($this->fp);
                fclose($tempHandle);
                throw new \RuntimeException(
                    "fwrite() failed to write to '$tempPath'"
                );
            }
            $bytesWritten += $bytesRead;
        }

        while (($pos = strpos($this->buffer, $this->boundary)) === false) {
            $this->buffer = fread($this->fp, $this->bytesPerRead);
            $bytesRead = fwrite($tempHandle, $this->buffer);
            if ($bytesRead === false) {
                fclose($this->fp);
                fclose($tempHandle);
                throw new \RuntimeException(
                    "fwrite() failed to write to '$tempPath'"
                );
            }
            $bytesWritten += $bytesRead;
        }

        # determine the size of the file based on the boundary position
        $size = $bytesWritten - $bytesRead + $pos - 2;

        # trim the excess contents of the local buffer to the object buffer
        $this->buffer = substr($this->buffer, $pos);

        # if the uploaded file was empty
        if ($size < 1) {
            return $this->fileUploadError(
                UPLOAD_ERR_NO_FILE,
                $tempPath,
                $tempHandle,
                $filename,
                $unverifiedContentType,
                0
            );
        } # if the file exceeded the max upload size
        elseif ($size > $this->maxFileSize) {
            return $this->fileUploadError(
                UPLOAD_ERR_INI_SIZE,
                $tempPath,
                $tempHandle,
                $filename,
                $unverifiedContentType,
                0
            );
        }

        ftruncate($tempHandle, $size);
        fclose($tempHandle);

        return [
            "name"     => $filename,
            "type"     => $unverifiedContentType,
            "tmp_name" => $tempPath,
            "error"    => UPLOAD_ERR_OK,
            "size"     => $size
        ];
    }
747834
final public function execute($name, AnCommandContext $context)
    {
        //Set the template
        $this->_template = $context->caller;

        //Set the data
        $data = $context->data;

        if (($name & LibBaseTemplateFilter::MODE_READ) && $this instanceof LibBaseTemplateFilterRead) {
            $this->read($data);
        }

        if (($name & LibBaseTemplateFilter::MODE_WRITE) && $this instanceof LibBaseTemplateFilterWrite) {
            $this->write($data);
        }

        //Get the data
        $context->data = $data;

        //Reset the template
        $this->_template = null;

        //@TODO : Allows filters to return false and halt the filter chain
        return true;
    }
1175159
public static function getPartial($name)
    {
        if (isset(self::$partials[$name])) {
            return self::$partials[$name];
        }
        
        return null;
    }
661557
public function viewHelper($context, $name = null)
    {
        if ($name !== null && isset($context['this'])) {
            $this->call($context['this'], Inflector::variablize($name));
        }
    }
221969
public function getContext($name = null)
    {
        if (is_null($name)) {
            $name = $this->context;
        }

        return isset($this->contexts[$name]) ? $this->contexts[$name] : null;
    }
553726
protected function loadContext($name = null) {
		$context = parent::loadContext($name);
		if (isset($context['iterator'])) {
			$this->iterator = $context['iterator'];
		} else if (isset($context['array'])) {
			$this->iterator = new \ArrayIterator($context['array']);
		} else {
			throw new \BadMethodCallException('Invalid context, iterator or array not set');
		}
		return $context;
	}
1313029
public function partial($name, array $data = [])
    {
        $templatePath = $this->manager->pathByName($name);

        // Use special variable names here to avoid conflict when extracting data
        if (is_array($data)) {
            extract($data, EXTR_PREFIX_SAME, 'data_');
        }

        ob_start();
        include $templatePath;
        return ob_get_clean();
    }
41198
public function compile($name): string
    {
        [
            'template' => $template,
            'cache' => $cache,
        ] = $this->getTemplateFile($name);

        if (\filemtime($template) < @\filemtime($cache)) {
            return $cache;
        }

        $root = Tokenizer\Tokenizer::parseFile($template);

        Processor::parseToken($root, $this->processors);

        \file_put_contents($cache, (string) $root);

        return $cache;
    }
1143914
public static function getPartial($name)
    {
        //Argument 1 must be a string
        Argument::i()->test(1, 'string');
        
        if (isset(self::$partials[$name])) {
            return self::$partials[$name];
        }
        
        return null;
    }