2016年2月16日 星期二

PHP 超簡易的樣版引擎

PHP的樣版引擎實在是多到不可數 而且每個樣版引擎的做法都不盡相同 有時何我們只需要一個很簡單的把php程式碼分離 就可以用這個最簡單的做法

test.php

<?php foreach ($data as $v): ?>
    <?php echo $v; ?>
<?php endforeach; ?>

view.php

function view($file, $data)
{
    ob_start();
    extract($data);
    require $file;

    return ob_get_clean();
}

echo view('test.php', ['data' => [
    1, 2, 3,
]]);

獨立使用Laravel Blade Template Engine

有別於之前獨立使用Socialite 這次我們利用ServiceProvider來設定Laravel Blade Engine

利用composer安裝 illuminate/view

composer require illunimate/view

config.php

require __DIR__.'/vendor/autoload.php';

use Illuminate\Container\Container;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Fluent;
use Illuminate\View\ViewServiceProvider;

$app = new Container();
$app['events'] = new Dispatcher();
$app['config'] = new Fluent();
$app['files'] = new Filesystem();

// 設定view資料夾
$app['config']['view.paths'] = [__DIR__.'/views/'];
// 設定快取資料夾
$app['config']['view.compiled'] = __DIR__.'/compiled/';

// 利用ServiceProvider來初始化View
$serviceProvider = new ViewServiceProvider($app);
// 註冊
$serviceProvider->register();
// boot
$serviceProvider->boot();

Facade::setFacadeApplication($app);

// 設Facade View Alias方便使用
class_alias(View::class, 'View');

views/test.blade.php

@foreach ([1,2,3] as $v)
    {{ $v }}
@endforeach

index.php

require __DIR__.'/config.php';

echo View::make('test')->render();

完整檔案在Github

2016年2月4日 星期四

Mock Laravel Application

研究了好久的mockery終於理出一些頭緒了

Laravel的Application的Mock原來如此簡單

m::mock('Illuminate\Container\Container, Illuminate\Contracts\Foundation\Application')
    ->makePartial()
    ->shouldReceive('basePath')->andReturn(realpath(__DIR__.'/../').'/')
    ->shouldReceive('version')->andReturn('5.x.testing')
    ->shouldReceive('environment')->andReturn('testing')
    ->mock();

2016年2月1日 星期一

獨立使用Laravel Socialite

有別於前一篇,這次我們用不使用Illuminate\Container\Container來執行Laravel Socialite

composer

 composer require laravel/socialite

php

require __DIR__.'/vendor/autoload.php';

use Illuminate\Http\Request;
use Illuminate\Support\Fluent;
use Laravel\Socialite\SocialiteManager;
use Symfony\Component\HttpFoundation\Session\Session;

/*
 * 用   \Illuminate\Support\Fluent
 * 替代 \Illuminate\Container\Container
 */
$app = new Fluent();

/*
 * 用   \Illuminate\Support\Fluent
 * 替代 \Illuminate\Contracts\Config\Repository
 */
$config = new Fluent();

/*
 * 取得 Request
 */
$request = Request::capture();

/*
 * 使用 Symfony Session
 */
$session = new Session();

/*
 * session start
 */
$session->start();

/*
 * 設置 session
 */
$request->setSession($session);

/*
 * Facebook飍數
 */
$config['services.facebook'] = [
    'client_id'     => '900358040081723',
    'client_secret' => '06134ad6946069c6457d06688b22c259',
    'redirect'      => 'http://localhost/test/socialite/index.php/callback',
];

/*
 * 將 config, request置入 app
 */
$app['config'] = $config;
$app['request'] = $request;

// 以上為 socalite app所需設定,接下來參考官方文件即可

$socialiteManager = new SocialiteManager($app);
$provider = $socialiteManager
    ->with('facebook')
    /*
     * 只要是oauth2就必須執行此方法,
     * 因為我們用的是Symfony Session而不是Laravel Session
     */
    ->stateless();

if (strpos($_SERVER['REQUEST_URI'], '/callback') === false) {
    $response = $provider->redirect();

    return $response->send();
}

var_dump((array) $provider->user());