`
weiqingfei
  • 浏览: 311478 次
  • 性别: Icon_minigender_1
  • 来自: 黑洞
社区版块
存档分类
最新评论

Laravel中,一个Facade模式的调用例

    博客分类:
  • PHP
 
阅读更多

 

<?php

namespace Illuminate\Support\Facades;

/**
 * @see \Illuminate\Auth\AuthManager
 * @see \Illuminate\Auth\Guard
 */
class Auth extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'auth';
    }
}

 [auth]的注册

 

 

<?php

namespace Illuminate\Auth;

use Illuminate\Support\ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{

    protected function registerAuthenticator()
    {
        $this->app->singleton('auth', function ($app) {
            // Once the authentication service has actually been requested by the developer
            // we will set a variable in the application indicating such. This helps us
            // know that we need to set any queued cookies in the after event later.
            $app['auth.loaded'] = true;

            return new AuthManager($app);
        });

        $this->app->singleton('auth.driver', function ($app) {
            return $app['auth']->driver();
        });
    }

}

 由于不存在这个方法

Illuminate\Auth\AuthManager->guest() 

 所以调用父类的以下方法

 

 

<?php

namespace Illuminate\Support;

use Closure;
use InvalidArgumentException;

abstract class Manager
{
    public function __call($method, $parameters)
    {
        return call_user_func_array([$this->driver(), $method], $parameters);
    }
}

 $this->driver()也就是

 

 

    public function driver($driver = null)
    {
        $driver = $driver ?: $this->getDefaultDriver();

        // If the given driver has not been created before, we will create the instances
        // here and cache it so we can return it next time very quickly. If there is
        // already a driver created by this name, we'll just return that instance.
        if (!isset($this->drivers[$driver])) {
            $this->drivers[$driver] = $this->createDriver($driver);
        }

        return $this->drivers[$driver];
    }

 

 

$this->getDefaultDriver()也就是

 

<?php

namespace Illuminate\Auth;

use Illuminate\Support\Manager;

class AuthManager extends Manager
{
    public function getDefaultDriver()
    {
        return $this->app['config']['auth.driver'];
    }

}

 $this->app['config']['auth.driver'];其实就是config目录下,auth.php文件的driver设值

 

<?php

return [
'driver' => 'eloquent',

 那么上面的$this->createDriver($driver);就是

<?php

namespace Illuminate\Support;

use Closure;
use InvalidArgumentException;

abstract class Manager
{

     */
    protected function createDriver($driver)
    {
        $method = 'create'.ucfirst($driver).'Driver';

        // We'll check to see if a creator method exists for the given driver. If not we
        // will check for a custom driver creator, which allows developers to create
        // drivers using their own customized driver creator Closure to create it.
        if (isset($this->customCreators[$driver])) {
            return $this->callCustomCreator($driver);
        } elseif (method_exists($this, $method)) {
            return $this->$method();
        }

        throw new InvalidArgumentException("Driver [$driver] not supported.");
    }
}

 也就是

<?php

namespace Illuminate\Auth;

use Illuminate\Support\Manager;

class AuthManager extends Manager
{

    /**
     * Create an instance of the Eloquent driver.
     *
     * @return \Illuminate\Auth\Guard
     */
    public function createEloquentDriver()
    {
        $provider = $this->createEloquentProvider();

        return new Guard($provider, $this->app['session.store']);
    }

}

 所以auth最终就是Guard

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics