常常為了地址的事情感到麻煩,所以寫了一個程式能把舊5都地址轉新5都地址,再取得區碼的程式
[packagist.org](https://packagist.org/packages/recca0120/twzipcode)
[github](https://github.com/recca0120/twzipcode)
```bash
composer require recca0120/twzipcode
```
```php
use Recca0120\Twzipcode\Twzipcode;
$twzipcode = new Twzipcode('北 縣 萬里鄉中正路100號');
$twzipcode->getZipcode(); // 207
$twzipcode->getCounty(); // 新北市
$twzipcode->getDistrict(); // 萬里區
$twzipcode->getAddress(); // 新北市萬里區中正路100號
$twzipcode->getShortAddress(); // 中正路100號
```
2016年6月6日 星期一
2016年6月5日 星期日
Laravel Repository
最近寫好的repository的package
還沒時間整理readme
所以就先拿phpunit來充當readme
[packagist.org](https://packagist.org/packages/recca0120/repository)
[github](https://github.com/recca0120/laravel-repository)
# Laravel Repository
[![Latest Stable Version](https://poser.pugx.org/recca0120/repository/v/stable)](https://packagist.org/packages/recca0120/repository)
[![Total Downloads](https://poser.pugx.org/recca0120/repository/downloads)](https://packagist.org/packages/recca0120/repository)
[![Latest Unstable Version](https://poser.pugx.org/recca0120/repository/v/unstable)](https://packagist.org/packages/recca0120/repository)
[![License](https://poser.pugx.org/recca0120/repository/license)](https://packagist.org/packages/recca0120/repository)
[![Monthly Downloads](https://poser.pugx.org/recca0120/repository/d/monthly)](https://packagist.org/packages/recca0120/repository)
[![Daily Downloads](https://poser.pugx.org/recca0120/repository/d/daily)](https://packagist.org/packages/recca0120/repository)
### Demo in tests/RepositoryTest.php
```php
use Faker\Factory as FakerFactory;
use Illuminate\Database\Eloquent\Model;
use Mockery as m;
use Recca0120\Repository\Criteria;
use Recca0120\Repository\EloquentRepository;
class EloquentRepositoryTest extends PHPUnit_Framework_TestCase
{
use Laravel;
public function setUp()
{
$app = $this->createApplication();
$db = $this->createDatabase();
Schema::create('users', function ($table) {
$table->increments('id');
$table->string('name');
$table->string('email');
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
Schema::create('roles', function ($table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
Schema::create('user_roles', function ($table) {
$table->integer('user_id');
$table->integer('role_id');
});
$faker = FakerFactory::create();
for ($i = 0; $i < 3; $i++) {
User::create([
'name' => sprintf('%03d', $i + 1),
'email' => sprintf('%03d@test.com', $i + 1),
'password' => $faker->text,
]);
}
Role::create([
'name' => 'superuser',
'description' => 'superuser',
]);
Role::create([
'name' => 'administrator',
'description' => 'administrator',
]);
User::find(1)->roles()->sync([1]);
}
public function tearDown()
{
m::close();
Schema::drop('users');
}
public function test_repository_create()
{
$repository = new EloquentRepository(new User());
$repositoryUser = $repository->create([
'name' => 'test9999',
'email' => 'test9999@test.com',
'password' => str_random(30),
]);
$modelUser = User::where('name', '=', 'test9999')->first();
$this->assertSame($repositoryUser->id, $modelUser->id);
}
public function test_repository_find()
{
$repository = new EloquentRepository(new User());
$repositoryUser = $repository->find(1);
$modelUser = User::find(1);
$this->assertSame($repositoryUser->id, $modelUser->id);
}
public function test_repository_update()
{
$repository = new EloquentRepository(new User());
$repositoryUser = $repository->update([
'password' => 'test_update',
], 2);
$modelUser = User::find(2);
$this->assertSame($repositoryUser->id, $modelUser->id);
$this->assertSame($modelUser->password, 'test_update');
}
public function test_repository_delete()
{
$counter = User::count();
$repository = new EloquentRepository(new User());
$this->assertTrue($repository->delete(1));
$this->assertSame(User::count(), $counter - 1);
}
public function test_repository_find_all()
{
$repository = new EloquentRepository(new User());
$repositoryUsers = $repository->findAll();
$modelUsers = User::all();
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
public function test_find_by_criteria()
{
$repository = new EloquentRepository(new User());
$criteria = (new Criteria())
->where('name', '0001')
->having('email', '=', '0001@test.com')
->groupBy('name');
$repositoryUsers = $repository->findBy($criteria);
$modelUsers = User::where('name', 'like', '0001')
->where('email', '0001@test.com')
->groupBy('name')
->get();
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
public function test_find_by_criteria_where()
{
$repository = new EloquentRepository(new User());
$criteria = (new Criteria())
->where('name', '0001');
$repositoryUsers = $repository->findBy($criteria);
$modelUsers = User::where('name', '0001')
->get();
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
public function test_find_by_criteria_where_closure()
{
$repository = new EloquentRepository(new User());
$criteria = (new Criteria())
->where(function ($criteria) {
return $criteria->where('name', '0001');
});
$repositoryUsers = $repository->findBy($criteria);
$modelUsers = User::where(function ($query) {
return $query->where('name', '0001');
})->get();
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
public function test_find_by_criteria_or_where()
{
$repository = new EloquentRepository(new User());
$criteria = (new Criteria())
->where('name', '0001')
->orWhere('name', '0002');
$repositoryUsers = $repository->findBy($criteria);
$modelUsers = User::where('name', '0001')
->orWhere('name', '0002')
->get();
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
public function test_find_by_criteria_or_where_closure()
{
$repository = new EloquentRepository(new User());
$criteria = (new Criteria())
->where(function ($criteria) {
return $criteria->where('name', '0001')
->orWhere('name', '0002');
});
$repositoryUsers = $repository->findBy($criteria);
$modelUsers = User::where(function ($query) {
return $query->where('name', '0001')
->orWhere('name', '0002');
})->get();
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
public function test_criteria_where_has()
{
$repository = new EloquentRepository(new User());
$criteria = (new Criteria())
->whereHas('roles', function ($criteria) {
return $criteria->where('id', '=', 1);
});
$repositoryUsers = $repository->findBy($criteria);
$modelUsers = User::whereHas('roles', function ($query) {
return $query->where('id', '=', 1);
})
->get();
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
public function test_criteria_join()
{
$repository = new EloquentRepository(new User());
$criteria = (new Criteria())
->join('user_roles', function ($criteria) {
return $criteria->on('users.id', '=', 'user_roles.user_id');
});
$repositoryUsers = $repository->findBy($criteria);
$modelUsers = User::join('user_roles', function ($query) {
return $query->on('users.id', '=', 'user_roles.user_id');
})
->get();
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
public function test_criteria_order_by()
{
$repository = new EloquentRepository(new User());
$criteria = (new Criteria())
->orderBy('name', 'desc');
$repositoryUsers = $repository->findBy($criteria);
$modelUsers = User::orderBy('name', 'desc')
->get();
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
public function test_criteria_select()
{
$repository = new EloquentRepository(new User());
$criteria = (new Criteria())
->select('name');
$repositoryUsers = $repository->findBy($criteria);
$modelUsers = User::select('name')
->get();
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
public function test_criteria_experssion()
{
$repository = new EloquentRepository(new User());
$criteria = (new Criteria())
->select(Criteria::expr('REPLACE(name, "0001", "0003")'));
$repositoryUsers = $repository->findBy($criteria);
$modelUsers = User::select(DB::raw('REPLACE(name, "0001", "0003")'))
->get();
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
public function test_criteria_paginated()
{
$repository = new EloquentRepository(new User());
$repositoryUsers = $repository->paginatedAll(15);
$modelUsers = User::paginate(15);
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
public function test_criteria_with()
{
$repository = new EloquentRepository(new User());
$criteria = (new Criteria())
->with('roles');
$repositoryUsers = $repository->findBy($criteria);
$modelUsers = User::with('roles')->get();
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
public function test_find_by_array()
{
$repository = new EloquentRepository(new User());
$repositoryUsers = $repository->findBy([
['name', '=', '0002'],
['email', '=', '0002@test.com'],
]);
$modelUsers = User::where('name', '0002')
->where('email', '0002@test.com')->get();
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
public function test_find_by_array_key()
{
$repository = new EloquentRepository(new User());
$repositoryUsers = $repository->findBy([
'name' => '0002',
'email' => '0002@test.com',
]);
$modelUsers = User::where('name', '0002')
->where('email', '0002@test.com')->get();
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
public function test_custom_criteria()
{
$repository = new EloquentRepository(new User());
$repositoryUsers = $repository->findBy(new CustomCriteria());
$modelUsers = User::where('name', '0002')->get();
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
public function test_multiple_criteria()
{
$repository = new EloquentRepository(new User());
$repositoryUsers = $repository->findBy([
new CustomCriteria(),
(new Criteria())->orderBy('name', 'desc'),
]);
$modelUsers = User::where('name', '0002')
->orderBy('name', 'desc')->get();
$this->assertSame($repositoryUsers->toArray(), $modelUsers->toArray());
}
}
class CustomCriteria extends Criteria
{
public function __construct()
{
$this->where('name', '0002');
}
}
class User extends Model
{
protected $fillable = [
'name', 'email', 'password',
];
public function roles()
{
return $this->belongsToMany(
Role::class,
'user_roles',
'role_id',
'user_id'
);
}
}
class Role extends Model
{
protected $fillable = [
'name', 'description',
];
public function users()
{
return $this->belongsToMany(
self::class,
'user_roles',
'user_id',
'role_id'
);
}
}
function dump()
{
call_user_func_array('var_dump', func_get_args());
}
```
Laravel Terminal
這是一個將artisan移植到web介面的package,
並增加了一些自定義的command
在server不支援bash shell時,還能執行一些aritsan的命令
[packagist.org](https://packagist.org/packages/recca0120/terminal)
[github](https://github.com/recca0120/laravel-terminal)
# Laravel Web Artisan
[![Latest Stable Version](https://poser.pugx.org/recca0120/terminal/v/stable)](https://packagist.org/packages/recca0120/terminal)
[![Total Downloads](https://poser.pugx.org/recca0120/terminal/downloads)](https://packagist.org/packages/recca0120/terminal)
[![Latest Unstable Version](https://poser.pugx.org/recca0120/terminal/v/unstable)](https://packagist.org/packages/recca0120/terminal)
[![License](https://poser.pugx.org/recca0120/terminal/license)](https://packagist.org/packages/recca0120/terminal)
[![Monthly Downloads](https://poser.pugx.org/recca0120/terminal/d/monthly)](https://packagist.org/packages/recca0120/terminal)
[![Daily Downloads](https://poser.pugx.org/recca0120/terminal/d/daily)](https://packagist.org/packages/recca0120/terminal)
## Installation
Add Presenter to your composer.json file:
```js
"require": {
"recca0120/terminal": "^1.3.3"
}
```
Now, run a composer update on the command line from the root of your project:
```
composer update
```
### Registering the Package
Include the service provider within `app/config/app.php`. The service povider is needed for the generator artisan command.
```php
'providers' => [
...
Recca0120\Terminal\ServiceProvider::class,
...
];
```
publish
```php
artisan vendor:publish --provider="Recca0120\Terminal\ServiceProvider"
```
### URL
http://localhost/path/to/terminal
### Whitelist
```php
return [
'whitelists' => ['127.0.0.1', 'your ip'],
];
```
## Available Commands
* artisan
* artisan tinker
* find
* mysql
### Find
not full support, but you can delete file use this function (please check file permission)
```bash
find ./vendor -name tests -type d -maxdepth 4 -delete
```
## Add Your Command
### Add Command Class
```php
// src/Console/Commands/Mysql.php
namespace Recca0120\Terminal\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Foundation\Inspiring;
class Inspire extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'inspire';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Display an inspiring quote';
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->comment(PHP_EOL.Inspiring::quote().PHP_EOL);
}
}
```
### Add Command
```php
// src/Console/Kernel.php
namespace Recca0120\Terminal\Console;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Recca0120\Terminal\Console\Application as Artisan;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
Commands\Inspire::class,
];
}
```
## ScreenShot
### Available Commands
```bash
$ help
```
![Available Commands](https://cdn.rawgit.com/recca0120/terminal/master/screenshots/available-commands.png)
### Artisan List
```bash
$ artisan
```
![Artisan List](https://cdn.rawgit.com/recca0120/terminal/master/screenshots/artisan-list.png)
### Migrate
```bash
$ artisan migrate --seed
```
![Migrate](https://cdn.rawgit.com/recca0120/terminal/master/screenshots/artisan-migrate.png)
### Artisan Tinker
```bash
$ artisan tinker
```
![Tinker](https://cdn.rawgit.com/recca0120/terminal/master/screenshots/artisan-tinker.png)
### Find Command
```bash
$ find ./ -name * -maxdepth 1
```
![Find Command](https://cdn.rawgit.com/recca0120/terminal/master/screenshots/find-command.png)
### Find and Delete
```bash
$ find ./storage/logs -name * -maxdepth 1 -delete
```
![Find and Delete](https://cdn.rawgit.com/recca0120/terminal/master/screenshots/find-and-delete.png)
### Vi
```bash
$ vi server.php
```
![Vi Command](https://cdn.rawgit.com/recca0120/terminal/master/screenshots/vi-command.png)
![Vi Editor](https://cdn.rawgit.com/recca0120/terminal/master/screenshots/vi-editor.png)
![Vi Save](https://cdn.rawgit.com/recca0120/terminal/master/screenshots/vi-save.png)
### Tail
```bash
$ tail
$ tail --line=1
$ tail server.php
$ tail server.php --line 5
```
![Tail Command](https://cdn.rawgit.com/recca0120/terminal/master/screenshots/tail-command.png)
### Cleanup
```bash
$ cleanup
```
![Cleanup Command](https://cdn.rawgit.com/recca0120/terminal/master/screenshots/cleanup-command.png)
Laravel Tracy
一個laravel的debug工具,它是MIT的!大家可以安裝來試看看
用得習慣的話就請多多使用....
[packagist.org](https://packagist.org/packages/recca0120/laravel-tracy)
[github](https://github.com/recca0120/laravel-tracy)
## [Nette Tracy](https://github.com/nette/tracy.git) for Laravel 5
Better Laravel Exception Handler
[![Latest Stable Version](https://poser.pugx.org/recca0120/laravel-tracy/v/stable)](https://packagist.org/packages/recca0120/laravel-tracy)
[![Total Downloads](https://poser.pugx.org/recca0120/laravel-tracy/downloads)](https://packagist.org/packages/recca0120/laravel-tracy)
[![Latest Unstable Version](https://poser.pugx.org/recca0120/laravel-tracy/v/unstable)](https://packagist.org/packages/recca0120/laravel-tracy)
[![License](https://poser.pugx.org/recca0120/laravel-tracy/license)](https://packagist.org/packages/recca0120/laravel-tracy)
[![Monthly Downloads](https://poser.pugx.org/recca0120/laravel-tracy/d/monthly)](https://packagist.org/packages/recca0120/laravel-tracy)
[![Daily Downloads](https://poser.pugx.org/recca0120/laravel-tracy/d/daily)](https://packagist.org/packages/recca0120/laravel-tracy)
## Features
- Visualization of errors and exceptions
- Debugger Bar
- Exception stack trace contains values of all method arguments.
## Online Demo
[Demo](https://cdn.rawgit.com/recca0120/laravel-tracy/master/screenshots/tracy-exception.html)
## Installing
To get the latest version of Laravel Exceptions, simply require the project using [Composer](https://getcomposer.org):
```bash
composer require recca0120/laravel-tracy
```
Instead, you may of course manually update your require block and run `composer update` if you so choose:
```json
{
"require": {
"recca0120/laravel-tracy": "~1.3.5"
}
}
```
Include the service provider within `config/app.php`. The service povider is needed for the generator artisan command.
```php
'providers' => [
...
Recca0120\LaravelTracy\ServiceProvider::class,
...
];
```
publish
```bash
artisan vendor:publish --provider="Recca0120\LaravelTracy\ServiceProvider"
```
## Config
```php
return [
'ajax' => [
'debugbar' => false, // enable render debugbar when http request is ajax
'gzCompressLevel' => 5, // gzcompress level
/*
* http://stackoverflow.com/questions/3326210/can-http-headers-be-too-big-for-browsers/3431476#3431476
* Lowest limit found in popular browsers:
* - 10KB per header
* - 256 KB for all headers in one response.
* - Test results from MacBook running Mac OS X 10.6.4:
*/
'maxHeaderSize' => 102400, // 102400b its => 100 kb
],
'basePath' => null,
'strictMode' => true,
'maxDepth' => 4,
'maxLen' => 1000,
'showLocation' => true,
'editor' => 'subl://open?url=file://%file&line=%line',
'panels' => [
'routing' => true,
'database' => true,
'view' => true,
'session' => true,
'request' => true,
'event' => false,
'user' => true,
'terminal' => true,
],
// value: js or tracy
'panelDumpMethod' => 'js', // tracy dump need more memory
];
```
### Editor Link
windows
```
copy /recca0120/laravel-tracy/tools/subl-handler/subl-handler.vbs to any directory where you want to place
double click subl-handler.vbs and select editor (support eclipse, sublime, notepad++, else...)
```
OSX
```
https://github.com/dhoulb/subl
```
## Debugger Bar
### SystemInfo
![SystemInfo](https://cdn.rawgit.com/recca0120/laravel-tracy/master/screenshots/systeminfo.png)
### Route
![Route](https://cdn.rawgit.com/recca0120/laravel-tracy/master/screenshots/route.png)
### View
![View](https://cdn.rawgit.com/recca0120/laravel-tracy/master/screenshots/view.png)
### Session
![Session](https://cdn.rawgit.com/recca0120/laravel-tracy/master/screenshots/session.png)
### Request
![Request](https://cdn.rawgit.com/recca0120/laravel-tracy/master/screenshots/request.png)
### Login
![Login](https://cdn.rawgit.com/recca0120/laravel-tracy/master/screenshots/login.png)
### Web Artisan
web artisan is another package [recca0120/terminal](https://github.com/recca0120/laravel-terminal)
![Terminal](https://cdn.rawgit.com/recca0120/laravel-tracy/master/screenshots/terminal.png)
#### notice
if you install terminal before, this panel will throw errors, please remove folder `app/resources/views/vendor/terminal`
## ISSUE
when ajax debugbar is enabled and debugbar is bigger than 256k, will throw 500 exception, or browser will be no response
so I try to compress debugbar in php, and decompress debugbar in javascript.
It looks like working at chrome 48.0.2564.116 64bit, windows 10
but if you use Laravel-Tracy and it doesn't work correctly
you can try
- disable panel [view , request, event]
- panelDumpMethod change to js
- disable ajax debugbar
2016年5月12日 星期四
活用 es6 generator + co 讓async看起來像sync
仔細研究了es6的generator function + [co](https://github.com/tj/co "co")
才發現可以利用它來改善我們的程式寫法
讓程式的可讀性更高
先讓我們看看用Promise來撰寫非同步code的樣子
```javascript
let num = function (num) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(num+1);
}, 500);
});
}
num(1).then((res) => {
return num(res);
}).then((res) => {
return num(res);
}).then((res) => {
// output
// promise result: 4
console.log(`promise result: ${res}`);
});
```
接下來讓我們來看看generator function + [co](https://github.com/tj/co "co")的寫法
```javascript
let num = function (num) {
return (callback) => {
setTimeout(function() {
callback(null, num+1);
}, 500);
}
}
co(function*() {
let num1 = yield num(1);
let num2 = yield num(num1);
let num3 = yield num(num2);
return num3;
}).then((res) => {
// output
// generator result: 4
console.log(`generator result: ${res}`);
});
```
[jsfiddle](https://jsfiddle.net/recca0120/eocp5h2c/ "jsfiddle範例")
2016年4月25日 星期一
ubuntu下安裝atom editor
只要執行三行指令即可
```bash
sudo add-apt-repository ppa:webupd8team/atom
sudo apt-get update
sudo apt-get install atom
```
Linux使用php7連接mssql,並設定utf-8
使用php7要連接mssql要安裝的套件為pdo_dblib
```bash
# centos
yum install php70w-pdo_dblib.x86_64
# ubuntu
sudo apt-get install freetds-bin php-sybase
```
接下來我們更改freetds的設定檔,裝連線設定為utf8
```bash
# centos
vi /etc/freetds.conf
# ubuntu
sudo vi /etc/freetds/freetds.conf
```
加入兩行設定即可
```ini
# centos
tds version = 7.2
client charset = UTF-8
# ubuntu
tds version = 7.1
client charset = UTF-8
```
![freetds config](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhfZxvhvTwGAuP2pwtB80Bbypwb54mp8uAIPe5kAPEB8Swk4l5BFpt0ZncIr_SMZ43RW5ibnF7SOP_AeE5D7PX5JFbk2uBPZkK_Q0jL0MFFlMXRkmgJeUNFv17CszpfALpdfu-HJ23aKN92/s1600/Image+3.png)
設定完成後連線到mssql charset就會是utf8
如果還是顯示為亂碼的話則可以利用tsql的指令來debug
```bash
tsql -S [ip] -U [username] -P [password]
```
![tsql](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhplyPJmyyXaqfmXpo1D9Tl72GhIOrn_P2QgjwASD3-QNn_3YUyVBsCX6FPv0TdYC6D9hhYd2JIVR8OEHZXnBErLy2004Kmsah5y1YOqCoZ5WtiPL5sbYzWiB-6dxaPz3OKny6Ji2qJw-Vf/s1600/Image+2.png)
2016年4月3日 星期日
es6如何拓增prototype
寫es6時要增加Object的prototype
已經和以往的寫法大為不同了
所以就來介紹一下方法吧
```js
// $.inArray 模擬 Array.includes
import $ from 'jquery';
if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, 'includes', {
value(...args) {
return $.inArray(...args, this) !== -1;
}
});
}
```
利用jQuery Promise模擬Native Promise
我們在寫es6時,會用到一些es6才有的函式或物件
但如果可以使用jQuery的一些內建函式來模擬的話
我們就不需要import 'babel-polyfill'
這樣產出來的javascript檔案就會少個90k左右
以下為程式碼
```js
'use strict';
import $ from 'jquery';
if (!window.Promise) {
class Promise {
constructor(callback) {
this.deferred = $.Deferred();
callback((o) => {
this.deferred.resolve(o);
}, (o) => {
this.deferred.reject(o);
});
this.promise = this.deferred.promise();
}
then(resolve, reject) {
this.promise.done(resolve);
this.promise.fail(reject);
return this;
}
catch(reject) {
this.promise.fail(reject);
return this;
}
}
window.Promise = Promise;
}
```
解決babel6 class extends 在 ie10 以下版本super無法呼叫 parent constructor
先直接看程式碼
```js
class A {
constructor() {
console.log('A');
}
}
class B extends A {
constructor() {
super();
}
}
new B();
```
這是一個很簡單物件繼承,預期console內輸出 "A"
實際做了測試之後發現在ie11, chrome, firefox都能正常輸出
但在ie9, ie10卻完全失效
後來查了原因後原來Object.getPrototypeOf在es5及es6的功能完全不同
才造成super失效
在github上已經有人提出兩種出解決方案
1.設定babel的plugins
```json
{
"presets": ["react", "es2015"],
"plugins": [
["transform-es2015-classes", { "loose": true }],
"transform-proto-to-assign"
]
}
```
2.修改Object.getPrototypeOf 來自[https://github.com/seznam/IMA.js-babel6-polyfill](https://github.com/seznam/IMA.js-babel6-polyfill "polyfill")
```js
(function() {
var testObject = {};
if (!(Object.setPrototypeOf || testObject.__proto__)) {
var nativeGetPrototypeOf = Object.getPrototypeOf;
Object.getPrototypeOf = function(object) {
if (object.__proto__) {
return object.__proto__;
} else {
return nativeGetPrototypeOf.call(Object, object);
}
}
}
})();
```
2016年3月13日 星期日
php composite + __call
```php
class Weapon
{
protected $name;
protected $attack = 0;
protected $addions = [];
public function __construct($name, $attack, $addions = [])
{
$this->name = $name;
$this->attack = $attack;
$this->addions = $addions;
}
public function setAttack($attack)
{
$this->attack = $attack;
}
public function attack()
{
$attack = $this->attack;
foreach ($this->addions as $addion) {
$attack += $addion->attack();
}
return $attack;
}
public function __call($method, $parameters)
{
foreach ($this->addions as $addion) {
if (method_exists($addion, $method) === true) {
return call_user_func_array([$addion, $method], $parameters);
}
}
die('error');
}
}
abstract class Addion
{
protected $attack = 0;
public function attack()
{
return $this->attack;
}
}
class ToxicAddion extends Addion
{
protected $attack = 15;
public function toxic()
{
echo 'toxic';
}
}
class LightingAddion extends Addion
{
protected $attack = 10;
public function lighting()
{
echo 'lighting';
}
}
$sword = new Weapon('劍', 10, [
new ToxicAddion(),
new LightingAddion(),
]);
echo $sword->attack();
echo $sword->toxic();
echo $sword->lighting();
// output
// 35
// toxic
// lighting
```
PHP decorator + __call
```
class Weapon
{
public function attack()
{
return 10;
}
}
abstract class Decorator
{
protected $weapon;
public function __construct($weapon)
{
$this->weapon = $weapon;
}
abstract public function attack();
public function __call($method, $parameters)
{
return call_user_func_array([$this->weapon, $method], $parameters);
}
}
class ToxicDecorator extends Decorator
{
public function attack()
{
return $this->weapon->attack() + 15;
}
public function toxic()
{
echo 'toxic';
}
}
class LightingDecorator extends Decorator
{
public function attack()
{
return $this->weapon->attack() + 5;
}
public function lighting()
{
echo 'lighting';
}
}
$weapon = new Weapon();
$weapon = new ToxicDecorator($weapon);
$weapon = new LightingDecorator($weapon);
echo $weapon->attack();
echo $weapon->toxic();
echo $weapon->lighting();
// output
// 30
// toxic
// lighting
```
2016年2月16日 星期二
PHP 超簡易的樣版引擎
PHP的樣版引擎實在是多到不可數
而且每個樣版引擎的做法都不盡相同
有時何我們只需要一個很簡單的把php程式碼分離
就可以用這個最簡單的做法
test.php
```php
<?php foreach ($data as $v): ?>
<?php echo $v; ?>
<?php endforeach; ?>
```
view.php
```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
```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
```php
@foreach ([1,2,3] as $v)
{{ $v }}
@endforeach
```
index.php
```php
require __DIR__.'/config.php';
echo View::make('test')->render();
```
完整檔案在[Github](https://github.com/recca0120/laravel-blade-standalone-demo)
2016年2月4日 星期四
Mock Laravel Application
研究了好久的mockery終於理出一些頭緒了
Laravel的Application的Mock原來如此簡單
```php
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
```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());
```
2016年1月31日 星期日
如何獨立使用Laravel Eloquent
Laravel的套件是可以獨立使用的,並不需要加入整個framework
我們一起來了解如何操作
composer 新增套件
```bash
composer require illuminate/container illuminate/contracts illuminate/database illuminate/events illuminate/hashing illuminate/support nesbot/carbon
```
新增bootstrap.php
```php
require __DIR__.'/../vendor/autoload.php';
use Illuminate\Container\Container;
use Illuminate\Support\Facades\Facade;
use Illuminate\Support\Str;
class Application extends Container
{
public $aliases = [
\Illuminate\Support\Facades\Facade::class => 'Facade',
\Illuminate\Support\Facades\App::class => 'App',
\Illuminate\Support\Facades\Schema::class => 'Schema',
];
public function __construct()
{
date_default_timezone_set('UTC');
if (class_exists('\Carbon\Carbon') === true) {
\Carbon\Carbon::setTestNow(\Carbon\Carbon::now());
}
$this['app'] = $this;
$this->setupAliases();
$this->setupDispatcher();
$this->setupConnection();
Facade::setFacadeApplication($this);
Container::setInstance($this);
}
public function setupDispatcher()
{
if (class_exists('\Illuminate\Events\Dispatcher') === false) {
return;
}
$this['events'] = new \Illuminate\Events\Dispatcher($this);
}
public function setupAliases()
{
foreach ($this->aliases as $className => $alias) {
class_alias($className, $alias);
}
}
public function setupConnection()
{
if (class_exists('\Illuminate\Database\Capsule\Manager') === false) {
return;
}
$connection = new \Illuminate\Database\Capsule\Manager();
$connection->addConnection([
'driver' => 'sqlite',
'database' => ':memory:',
]);
if (isset($this['events']) === true) {
$connection->setEventDispatcher($this['events']);
}
$connection->bootEloquent();
$connection->setAsGlobal();
$this['db'] = $connection;
}
public function migrate($method)
{
if (class_exists('\Illuminate\Database\Capsule\Manager') === false) {
return;
}
foreach (glob(__DIR__.'/../database/migrations/*.php') as $file) {
include_once $file;
if (preg_match('/\d+_\d+_\d+_\d+_(.*)\.php/', $file, $m)) {
$className = Str::studly($m[1]);
$migration = new $className();
call_user_func_array([$migration, $method], []);
}
}
}
public function environment()
{
return 'testing';
}
}
if (function_exists('bcrypt') === false) {
/**
* Hash the given value.
*
* @param string $value
* @param array $options
*
* @return string
*/
function bcrypt($value, $options = [])
{
return (new \Illuminate\Hashing\BcryptHasher())->make($value, $options);
}
}
if (function_exists('app') === false) {
function app()
{
return App::getInstance();
}
}
if (Application::getInstance() == null) {
new Application();
}
```
只要再建立資料表及建立Model
就可以正常使用Eloquent了
2016年1月3日 星期日
訂閱:
文章 (Atom)