[李景山php]每天laravel-20161022|Request.php-3

    //2016-08-26
    /**
     * Check that the given file is a valid file instance.
     *
     * @param  mixed  $file
     * @return bool
     */
    protected function isValidFile($file)
    {
        return $file instanceof SplFileInfo && $file->getPath() != ‘‘;
       // if both about this is ok than it is ok
    }//Check that the given file is a valid file instance.

    /**
     * Retrieve a header from the request.
     *
     * @param  string  $key
     * @param  string|array|null  $default
     * @return string|array
     */
    public function header($key = null, $default = null)
    {
        return $this->retrieveItem(‘headers‘, $key, $default);
    }//Retrieve a header from the request.
   // retrieve  like search
   // get the request header
   // use a wrap function to set this options to get the value

    /**
     * Retrieve a server variable from the request.
     *
     * @param  string  $key
     * @param  string|array|null  $default
     * @return string|array
     */
    public function server($key = null, $default = null)
    {
        return $this->retrieveItem(‘server‘, $key, $default);
    }// like brother

    /**
     * Retrieve an old input item.
     *
     * @param  string  $key
     * @param  string|array|null  $default
     * @return string|array
     */
    public function old($key = null, $default = null)
    {
        return $this->session()->getOldInput($key, $default);
    }// this is amazing method to get you old boy friend
   // like get you history

    /**
     * Flash the input for the current request to the session.
     *
     * @param  string  $filter
     * @param  array   $keys
     * @return void
     */
    public function flash($filter = null, $keys = [])
    {
        $flash = (! is_null($filter)) ? $this->$filter($keys) : $this->input();

        $this->session()->flashInput($flash);// use a flash method
    }// flash the input for the current request to the session

    /**
     * Flash only some of the input to the session.
     *
     * @param  array|mixed  $keys
     * @return void
     */
    public function flashOnly($keys)
    {
        $keys = is_array($keys) ? $keys : func_get_args();// get the keys

        return $this->flash(‘only‘, $keys);// return this flash
    }// flash only some of the input to the session

    /**
     * Flash only some of the input to the session.
     *
     * @param  array|mixed  $keys
     * @return void
     */
    public function flashExcept($keys)
    {
        $keys = is_array($keys) ? $keys : func_get_args();// get this right key.

        return $this->flash(‘except‘, $keys);// use a key to store this value
    }//flash only some of the input to the session.

    /**
     * Flush all of the old input from the session.
     *
     * @return void
     */
    public function flush()
    {
        $this->session()->flashInput([]);
    }// if this values is null flash all value.

    /**
     * Retrieve a parameter item from a given source.
     *
     * @param  string  $source
     * @param  string  $key
     * @param  string|array|null  $default
     * @return string|array
     */
    protected function retrieveItem($source, $key, $default)
    {
        if (is_null($key)) {
            return $this->$source->all();
        }// is_null

        return $this->$source->get($key, $default);// get the key
    }//this is the retrieve method ,like a search function.

    /**
     * Merge new input into the current request‘s input array.
     *
     * @param  array  $input
     * @return void
     */
    public function merge(array $input)
    {
        $this->getInputSource()->add($input);// to get the Source
    }//get the merge

    /**
     * Replace the input for the current request.
     *
     * @param  array  $input
     * @return void
     */
    public function replace(array $input)
    {
        $this->getInputSource()->replace($input);
    }//replace the input for the current request.

    /**
     * Get the JSON payload for the request.
     *
     * @param  string  $key
     * @param  mixed   $default
     * @return mixed
     */
    public function json($key = null, $default = null)
    {
        if (! isset($this->json)) {
            $this->json = new ParameterBag((array) json_decode($this->getContent(), true));
        }//set this json

        if (is_null($key)) {
            return $this->json;
        }// return all

        return data_get($this->json->all(), $key, $default);// get the key from this store array
    }//get the JSON payload fro the request.

    /**
     * Get the input source for the request.
     *
     * @return \Symfony\Component\HttpFoundation\ParameterBag
     */
    protected function getInputSource()
    {
        if ($this->isJson()) {
            return $this->json();
        }// if it is a Json

        return $this->getMethod() == ‘GET‘ ? $this->query : $this->request;// get this GET
    }//Get the input source for the request

    /**
     * Determine if the given content types match.
     *
     * @param  string  $actual
     * @param  string  $type
     * @return bool
     */
    public static function matchesType($actual, $type)
    {// Determine if the given content types match.
        if ($actual === $type) {
            return true;
        }// determine this two type is ok

        $split = explode(‘/‘, $actual);//get this value

        if (isset($split[1]) && preg_match(‘/‘.$split[0].‘\/.+\+‘.$split[1].‘/‘, $type)) {
            return true;
        }//has this check

        return false;
    }// default it is wrong.

    /**
     * Determine if the request is sending JSON.
     *
     * @return bool
     */
    public function isJson()
    {
        return Str::contains($this->header(‘CONTENT_TYPE‘), [‘/json‘, ‘+json‘]);
       // Str:: contains
       // to set this header
    }//Determine if the request is sending JSON

    /**
     * Determine if the current request is asking for JSON in return.
     *
     * @return bool
     */
    public function wantsJson()
    {
        $acceptable = $this->getAcceptableContentTypes();// use a magic function

        return isset($acceptable[0]) && Str::contains($acceptable[0], [‘/json‘, ‘+json‘]);
    }// check this request method to return by json type

    /**
     * Determines whether the current requests accepts a given content type.
     *
     * @param  string|array  $contentTypes
     * @return bool
     */
    public function accepts($contentTypes)
    {//Determines whether the current requests accepts a given content type.
        $accepts = $this->getAcceptableContentTypes();// get this accepts

        if (count($accepts) === 0) {
            return true;
        }// if count is zero will return true.

        $types = (array) $contentTypes;// get this type

        foreach ($accepts as $accept) {// loop accepts as accept
            if ($accept === ‘*/*‘ || $accept === ‘*‘) {
                return true;
            }// if it is all ,will return good

            foreach ($types as $type) {// loop this can be accept types ,and match it
                if ($this->matchesType($accept, $type) || $accept === strtok($type, ‘/‘).‘/*‘) {
                    return true;
                }// if has it ,just return  true.
            }
        }

        return false;
    }

    /**
     * Return the most suitable content type from the given array based on content negotiation.
     *
     * @param  string|array  $contentTypes
     * @return string|null
     */
    public function prefers($contentTypes)
    {//return the most suitable content type from the given array based on content negotiation.
        $accepts = $this->getAcceptableContentTypes();// get this accept type

        $contentTypes = (array) $contentTypes;// change this best type.

        foreach ($accepts as $accept) {// loop again , i can see the after
            if (in_array($accept, [‘*/*‘, ‘*‘])) {
                return $contentTypes[0];
            }// in_array

            foreach ($contentTypes as $contentType) {// loop this contentTypes
                $type = $contentType;// type

                if (! is_null($mimeType = $this->getMimeType($contentType))) {
                    $type = $mimeType;
                }// get this mimeType;

                if ($this->matchesType($type, $accept) || $accept === strtok($type, ‘/‘).‘/*‘) {
                    return $contentType;
                }// matches Type
            }
        }
    }

    /**
     * Determines whether a request accepts JSON.
     *
     * @return bool
     */
    public function acceptsJson()
    {
        return $this->accepts(‘application/json‘);
    }// a wrap about json

    /**
     * Determines whether a request accepts HTML.
     *
     * @return bool
     */
    public function acceptsHtml()
    {
        return $this->accepts(‘text/html‘);
    }// a wrap about html

    /**
     * Get the data format expected in the response.
     *
     * @param  string  $default
     * @return string
     */
    public function format($default = ‘html‘)
    {
        foreach ($this->getAcceptableContentTypes() as $type) {
            if ($format = $this->getFormat($type)) {
                return $format;
            }
        }// loop for every thing

        return $default;
    }// i like this function to format every thing

    /**
     * Get the bearer token from the request headers.
     *
     * @return string|null
     */
    public function bearerToken()
    {
        $header = $this->header(‘Authorization‘, ‘‘);

        if (Str::startsWith($header, ‘Bearer ‘)) {
            return Str::substr($header, 7);
        }// get the Str::
    }// bearer Token

    /**
     * Create an Illuminate request from a Symfony instance.
     *
     * @param  \Symfony\Component\HttpFoundation\Request  $request
     * @return \Illuminate\Http\Request
     */
    public static function createFromBase(SymfonyRequest $request)
    {//Create an Illuminate request from a Symfony instance.
        if ($request instanceof static) {
            return $request;
        }// request instanceof static

        $content = $request->content;// get this content

        $request = (new static)->duplicate(

            $request->query->all(), $request->request->all(), $request->attributes->all(),

            $request->cookies->all(), $request->files->all(), $request->server->all()
        );// get request

        $request->content = $content;

        $request->request = $request->getInputSource();

        return $request;
    }// a wrap function

    /**
     * {@inheritdoc}
     */
    public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
    {
        return parent::duplicate($query, $request, $attributes, $cookies, array_filter((array) $files), $server);
    }// duplicate is a good function

    /**
     * Get the session associated with the request.
     *
     * @return \Illuminate\Session\Store
     *
     * @throws \RuntimeException
     */
    public function session()
    {
        if (! $this->hasSession()) {
            throw new RuntimeException(‘Session store not set on request.‘);
        }// has Session

        return $this->getSession();// get Session
    }//Get the session associated withe the request.

    /**
     * Get the user making the request.
     *
     * @param  string|null  $guard
     * @return mixed
     */
    public function user($guard = null)
    {
        return call_user_func($this->getUserResolver(), $guard);// just call user fucntion
    }//Get the user making the request.

    /**
     * Get the route handling the request.
     *
     * @param string|null $param
     *
     * @return \Illuminate\Routing\Route|object|string
     */
    public function route($param = null)
    {//Get the route handling the request.
        $route = call_user_func($this->getRouteResolver());// a function to get route

        if (is_null($route) || is_null($param)) {
            return $route;// get this route
        } else {
            return $route->parameter($param);// get the parameter
        }
    }

    /**
     * Get a unique fingerprint for the request / route / IP address.
     *
     * @return string
     *
     * @throws \RuntimeException
     */
    public function fingerprint()
    {// get the unique fingerprint for the request /route /IP address
        if (! $this->route()) {
            throw new RuntimeException(‘Unable to generate fingerprint. Route unavailable.‘);
        }// if throw new Runtime Exception

        return sha1(
            implode(‘|‘, $this->route()->methods()).
            ‘|‘.$this->route()->domain().
            ‘|‘.$this->route()->uri().
            ‘|‘.$this->ip()
        );// a sha1
    }

    /**
     * Get the user resolver callback.
     *
     * @return \Closure
     */
    public function getUserResolver()
    {
        return $this->userResolver ?: function () {
            //
        };
    }//Get the user resolver callback

    /**
     * Set the user resolver callback.
     *
     * @param  \Closure  $callback
     * @return $this
     */
    public function setUserResolver(Closure $callback)
    {
        $this->userResolver = $callback;

        return $this;//a function
    }// Set the user resolver callback

    /**
     * Get the route resolver callback.
     *
     * @return \Closure
     */
    public function getRouteResolver()
    {
        return $this->routeResolver ?: function () {
            //
        };// a null function
    }//Get the route resolver callback.

    /**
     * Set the route resolver callback.
     *
     * @param  \Closure  $callback
     * @return $this
     */
    public function setRouteResolver(Closure $callback)
    {
        $this->routeResolver = $callback;

        return $this;
    }//Set the route resolver callback.

    /**
     * Get all of the input and files for the request.
     *
     * @return array
     */
    public function toArray()
    {
        return $this->all();// return all
    }//Get all of the input and files for the request.

    /**
     * Determine if the given offset exists.
     *
     * @param  string  $offset
     * @return bool
     */
    public function offsetExists($offset)
    {
        return array_key_exists($offset, $this->all());
    }//array_key_exists

    /**
     * Get the value at the given offset.
     *
     * @param  string  $offset
     * @return mixed
     */
    public function offsetGet($offset)
    {
        return data_get($this->all(), $offset);
    }//get the offset Get

    /**
     * Set the value at the given offset.
     *
     * @param  string  $offset
     * @param  mixed  $value
     * @return void
     */
    public function offsetSet($offset, $value)
    {
        return $this->getInputSource()->set($offset, $value);
    }// offset Set

    /**
     * Remove the value at the given offset.
     *
     * @param  string  $offset
     * @return void
     */
    public function offsetUnset($offset)
    {
        return $this->getInputSource()->remove($offset);
    }// Unset

    /**
     * Check if an input element is set on the request.
     *
     * @param  string  $key
     * @return bool
     */
    public function __isset($key)
    {
        return ! is_null($this->__get($key));
    }// isset method

    /**
     * Get an input element from the request.
     *
     * @param  string  $key
     * @return mixed
     */
    public function __get($key)
    {
        $all = $this->all();

        if (array_key_exists($key, $all)) {
            return $all[$key];
        } else {
            return $this->route($key);
        }
    }// a  magic get
}
时间: 2024-08-14 09:03:04

[李景山php]每天laravel-20161022|Request.php-3的相关文章

[李景山php]每天laravel-20161021|Request.php-2

/**  * Determine if the current request URL and query string matches a pattern.  *  * @param  mixed  string  * @return bool  */ public function fullUrlIs() {// check string like URL     $url = $this->fullUrl();     foreach (func_get_args() as $patter

[李景山php]每天laravel-20160920|Writer-2

    //2016-07-20     /**      * Register a file log handler.      *      * @param  string  $path      * @param  string  $level      * @return void      */     public function useFiles($path, $level = 'debug')     {         $this->monolog->pushHandle

[李景山php]每天laravel-20161108|ShareErrorsFromSession.php

<?php namespace Illuminate\View\Middleware; use Closure; use Illuminate\Support\ViewErrorBag; use Illuminate\Contracts\View\Factory as ViewFactory; class ShareErrorsFromSession {     /**      * The view factory implementation.      *      * @var \Ill

[李景山php]每天TP5-20161207|Loader.php-5

/**  * 实例化(分层)模型  * @param string $name         Model名称  * @param string $layer        业务层名称  * @param bool   $appendSuffix 是否添加类名后缀  * @param string $common       公共模块名  * @return Object  * @throws ClassNotFoundException  */ public static function m

[李景山php]每天TP5-20170117|thinkphp5-Url.php-2

    // 检测域名     protected static function parseDomain(&$url, $domain)     {// 根据 url 及传入的 域名进行解析         if (!$domain) {// 如果没有传入合适的数据,直接 返回             return '';         }         $request = Request::instance();// 请求实例化         if (true === $domain

[李景山php]每天TP5-20161212|App.php-2

public static function run(Request $request = null) {// thinkphp经过了 自动加载.错误接管.配置文件预设,终于开始执行了.     // 第一步:获取请求参数     is_null($request) && $request = Request::instance();     // self::$instance = new static($options); 执行了 这个 instance     // 默认 没有传入任

[李景山php]每天TP5-20170201|thinkphp5-Request.php-4

/**  * 设置获取获取当前请求的参数  * @access public  * @param string|array  $name 变量名  * @param mixed         $default 默认值  * @param string|array  $filter 过滤方法  * @return mixed  */ public function param($name = '', $default = null, $filter = null) {// 设置 或者 获取 当前

[李景山php]每天TP5-20161217|thinkphp5-helper.php-2

<?php // +---------------------------------------------------------------------- // | ThinkPHP [ WE CAN DO IT JUST THINK ] // +---------------------------------------------------------------------- // | Copyright (c) 2006~2016 http://thinkphp.cn All 

[李景山php]每天TP5-20170116|thinkphp5-Url.php-1

namespace think; use think\Config; use think\Loader; use think\Request; use think\Route; // 使用 think 里面,内置的 Config Loader Request Route class Url {     // 生成URL地址的root     protected static $root;// 根地址     /**      * URL生成 支持路由反射      * @param string