资讯专栏INFORMATION COLUMN

laravel cache get 是如何调用的?

solocoder / 2976人阅读

摘要:本文使用版本为使用实际调用的是这个映射是如何做的将里的数组里面的类设置别名来自于中数组为一个类创建别名这个文件没有,只有这里为是容器对象,实现了接口,最终调用的还是容器的方法

本文使用版本为laravel5.5

cache get
public function cache()
    {
        $c=Cache::get("app");
        if(!$c) {
            Cache::put("app", "cache", 1);
        }
        dump($c);//cache
    }
    
config/app.php
 "aliases" => [

        "App" => IlluminateSupportFacadesApp::class,
        "Artisan" => IlluminateSupportFacadesArtisan::class,
        "Auth" => IlluminateSupportFacadesAuth::class,
        "Blade" => IlluminateSupportFacadesBlade::class,
        "Broadcast" => IlluminateSupportFacadesBroadcast::class,
        "Bus" => IlluminateSupportFacadesBus::class,
        "Cache" => IlluminateSupportFacadesCache::class,
        
        ]

使用cache实际调用的是IlluminateSupportFacadesCache,这个映射是如何做的?

public/index.php
$response = $kernel->handle(
$request = IlluminateHttpRequest::capture()
);
bootstarp/app.php
$app->singleton(
    IlluminateContractsHttpKernel::class,
    AppHttpKernel::class
);
app/http/kernel.php
use IlluminateFoundationHttpKernel as HttpKernel;

class Kernel extends HttpKernel
{

}
Illuminate/Foundation/Http/Kernel.php
public function handle($request)
{
    try {
        $request->enableHttpMethodParameterOverride();
        $response = $this->sendRequestThroughRouter($request);
    } catch (Exception $e) {
        $this->reportException($e);

        $response = $this->renderException($request, $e);
    } catch (Throwable $e) {
        $this->reportException($e = new FatalThrowableError($e));

        $response = $this->renderException($request, $e);
    }

    $this->app["events"]->dispatch(
        new EventsRequestHandled($request, $response)
    );

    return $response;
}
protected function sendRequestThroughRouter($request)
    {
        $this->app->instance("request", $request);

        Facade::clearResolvedInstance("request");

        $this->bootstrap();

        return (new Pipeline($this->app))
                    ->send($request)
                    ->through($this->app->shouldSkipMiddleware() ? [] : $this->middleware)
                    ->then($this->dispatchToRouter());
    }
    public function bootstrap()
    {
        if (! $this->app->hasBeenBootstrapped()) {
            $this->app->bootstrapWith($this->bootstrappers());
        }
    }
Illuminate/Foundation/Application.php
public function bootstrapWith(array $bootstrappers)
    {
        $this->hasBeenBootstrapped = true;

        foreach ($bootstrappers as $bootstrapper) {
            $this["events"]->fire("bootstrapping: ".$bootstrapper, [$this]);

            $this->make($bootstrapper)->bootstrap($this);

            $this["events"]->fire("bootstrapped: ".$bootstrapper, [$this]);
        }
    }
Illuminate/Foundation/Bootstrap/RegisterFacades.php
public function bootstrap(Application $app)
    {
        Facade::clearResolvedInstances();

        Facade::setFacadeApplication($app);
//将config/app.php 里的aliases数组里面的Facades类设置别名
        AliasLoader::getInstance(array_merge(
            $app->make("config")->get("app.aliases", []),
            $app->make(PackageManifest::class)->aliases()
        ))->register();
    }
Illuminate/Foundation/AliasLoader.php
public function load($alias)
    {
        if (static::$facadeNamespace && strpos($alias, static::$facadeNamespace) === 0) {
            $this->loadFacade($alias);

            return true;
        }

      // $alias来自于config/app.php中aliases数组 
        if (isset($this->aliases[$alias])) {
            //"Route" => IlluminateSupportFacadesRoute::class,
            // class_alias 为一个类创建别名
            return class_alias($this->aliases[$alias], $alias);
        }
    }
Illuminate/Support/Facades/Cache.php
class Cache extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return "cache";
    }
}
Illuminate/Support/Facades/Facade.php

这个文件没有get set ,只有__callStatic

public static function __callStatic($method, $args)
    {
        $instance = static::getFacadeRoot();

        if (! $instance) {
            throw new RuntimeException("A facade root has not been set.");
        }

        return $instance->$method(...$args);
    }
   public static function getFacadeRoot()
    {
        return static::resolveFacadeInstance(static::getFacadeAccessor());
    } 
     protected static function resolveFacadeInstance($name)
    {
    //这里$name为cache
        if (is_object($name)) {
            return $name;
        }

        if (isset(static::$resolvedInstance[$name])) {
            return static::$resolvedInstance[$name];
        }
    //$app是容器对象,实现了ArrayAccess接口,最终调用的还是容器的make方法
        return static::$resolvedInstance[$name] = static::$app[$name];
    }
IlluminateContainerContainer.php
public function make($abstract, array $parameters = [])
    {
        return $this->resolve($abstract, $parameters);
    }
    protected function resolve($abstract, $parameters = [])
    {
        $abstract = $this->getAlias($abstract);

        $needsContextualBuild = ! empty($parameters) || ! is_null(
            $this->getContextualConcrete($abstract)
        );

        // If an instance of the type is currently being managed as a singleton we"ll
        // just return an existing instance instead of instantiating new instances
        // so the developer can keep using the same objects instance every time.
        if (isset($this->instances[$abstract]) && ! $needsContextualBuild) {
            return $this->instances[$abstract];
        }

        $this->with[] = $parameters;

        $concrete = $this->getConcrete($abstract);

        // We"re ready to instantiate an instance of the concrete type registered for
        // the binding. This will instantiate the types, as well as resolve any of
        // its "nested" dependencies recursively until all have gotten resolved.
        if ($this->isBuildable($concrete, $abstract)) {
            $object = $this->build($concrete);
        } else {
            $object = $this->make($concrete);
        }

        // If we defined any extenders for this type, we"ll need to spin through them
        // and apply them to the object being built. This allows for the extension
        // of services, such as changing configuration or decorating the object.
        foreach ($this->getExtenders($abstract) as $extender) {
            $object = $extender($object, $this);
        }

        // If the requested type is registered as a singleton we"ll want to cache off
        // the instances in "memory" so we can return it later without creating an
        // entirely new instance of an object on each subsequent request for it.
        if ($this->isShared($abstract) && ! $needsContextualBuild) {
            $this->instances[$abstract] = $object;
        }

        $this->fireResolvingCallbacks($abstract, $object);

        // Before returning, we will also set the resolved flag to "true" and pop off
        // the parameter overrides for this build. After those two things are done
        // we will be ready to return back the fully constructed class instance.
        $this->resolved[$abstract] = true;

        array_pop($this->with);

        return $object;
    }
Illuminate/Cache/CacheServiceProvider.php
 public function register()
    {
        $this->app->singleton("cache", function ($app) {
            return new CacheManager($app);
        });

        $this->app->singleton("cache.store", function ($app) {
            return $app["cache"]->driver();
        });

        $this->app->singleton("memcached.connector", function () {
            return new MemcachedConnector;
        });
    }
get set
$instance->$method(...$args)

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/29907.html

相关文章

  • Laravel学习笔记之Session源码解析(上)

    摘要:然后中间件使用方法来启动获取实例,使用类来管理主要分为两步获取实例,主要步骤是通过该实例从存储介质中读取该次请求所需要的数据,主要步骤是。 说明:本文主要通过学习Laravel的session源码学习Laravel是如何设计session的,将自己的学习心得分享出来,希望对别人有所帮助。Laravel在web middleware中定义了session中间件IlluminateSess...

    NervosNetwork 评论0 收藏0
  • Laravel学习笔记之Filesystem源码解析(下)

    摘要:源码解析这个类的源码主要就是文件的操作和文件属性的操作,而具体的操作是通过每一个实现的,看其构造函数看以上代码知道对于操作,实际上是通过的实例来实现的。可以看下的使用上文已经说了,使得对各种的操作变得更方便了,不管是还是得。 说明:本文主要学习下LeagueFlysystem这个Filesystem Abstract Layer,学习下这个package的设计思想和编码技巧,把自己的一...

    Luosunce 评论0 收藏0
  • 【译】深入研究Laravel依赖注入容器

    摘要:原文地址下面是中文翻译拥有强大的控制反转依赖注入容器。单例在使用自动绑定和时,每次需要时都会创建一个新的实例或者调用闭包。 原文地址 Laravels Dependency Injection Container in Depth 下面是中文翻译 Laravel拥有强大的控制反转(IoC)/依赖注入(DI) 容器。不幸的是官方文档并没有涵盖所有可用的功能,因此,我决定尝试写文档为自...

    chavesgu 评论0 收藏0
  • Laravel 模板引擎(Blade)原理简析

    摘要:上次提到过,模板引擎一般是要做三件事情变量值的输出条件判断和循环引入或继承其他文件现在就来看看的模板引擎是如何来处理这三件事情的。引擎接下来就是本文的重点是如何编译的。如果有兴趣的话,也可以实现一个自己的模板解析引擎。 上次提到过,模板引擎一般是要做三件事情: 变量值的输出(echo) 条件判断和循环(if ... else、for、foreach、while) 引入或继承其他文件 ...

    vvpvvp 评论0 收藏0
  • Laravel学习笔记之bootstrap源码解析

    摘要:总结本文主要学习了启动时做的七步准备工作环境检测配置加载日志配置异常处理注册注册启动。 说明:Laravel在把Request通过管道Pipeline送入中间件Middleware和路由Router之前,还做了程序的启动Bootstrap工作,本文主要学习相关源码,看看Laravel启动程序做了哪些具体工作,并将个人的研究心得分享出来,希望对别人有所帮助。Laravel在入口index...

    xiaoxiaozi 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<