LazyCollection 加 Generate 踩雷記 發表於 2020-08-17 分類於 laravel 在看文件的時候就知道 LazyCollection 搭 Generator 是一個很棒的組合就誤以為直接丟 generator 就可以當找到 $i > 5 之後就會停止執行迴圈但事與願違啊 123456789101112131415161718192021222324252627282930313233343536373839404142434445use Illuminate\Support\LazyCollection;use PHPUnit\Framework\TestCase;class ExampleTest extends TestCase{ public function test_lazy_collect() { $this->assertEquals(6, LazyCollection::make($this->generator())->collapse()->first(function ($i) { var_dump($i); return $i > 5; })); } public function generator() { $x = 1; for ($i = 1; $i <= 9; $i++) { var_dump(sprintf('loop-%02d', $x)); $temp = []; for ($j = 1; $j <= 9; $j++) { $temp[] = $i * $j; } $x++; yield $temp; } }}// 會跑完所有迴圈// string(7) "loop-01"// string(7) "loop-02"// string(7) "loop-03"// string(7) "loop-04"// string(7) "loop-05"// string(7) "loop-06"// string(7) "loop-07"// string(7) "loop-08"// string(7) "loop-09"// int(1)// int(2)// int(3)// int(4)// int(5)// int(6) 查了一下原始碼必須用 Closure 來回傳 Generator 123456789101112131415161718192021222324252627282930313233343536373839class LazyCollection implements Enumerable{ public function __construct($source = null) { if ($source instanceof Closure || $source instanceof self) { $this->source = $source; } elseif (is_null($source)) { $this->source = static::empty(); } else { $this->source = $this->getArrayableItems($source); } } /** * Results array of items from Collection or Arrayable. * * @param mixed $items * @return array */ protected function getArrayableItems($items) { if (is_array($items)) { return $items; } elseif ($items instanceof Enumerable) { return $items->all(); } elseif ($items instanceof Arrayable) { return $items->toArray(); } elseif ($items instanceof Jsonable) { return json_decode($items->toJson(), true); } elseif ($items instanceof JsonSerializable) { return (array) $items->jsonSerialize(); } elseif ($items instanceof Traversable) { // 不是用 Closure 就會跑到一段 return iterator_to_array($items); } return (array) $items; }} 所以正確的寫法應該是 12345678910111213141516171819202122232425262728293031323334353637383940use Illuminate\Support\LazyCollection;use PHPUnit\Framework\TestCase;class ExampleTest extends TestCase{ public function test_lazy_collect() { // 必須使用 Closure $this->assertEquals(6, LazyCollection::make(function () { return $this->generator(); })->collapse()->first(function ($i) { var_dump($i); return $i > 5; })); } public function generator() { $x = 1; for ($i = 1; $i <= 9; $i++) { var_dump(sprintf('loop-%02d', $x)); $temp = []; for ($j = 1; $j <= 9; $j++) { $temp[] = $i * $j; } $x++; yield $temp; } }}// 只執行了第一行迴圈string(7) "loop-01"int(1)int(2)int(3)int(4)int(5)int(6) 這告訴我們文件必須仔細看啊
GuzzleHttp Psr7 Response 取得網頁內容的注意事項 發表於 2020-08-05 分類於 php 使用 GuzzleHttp Psr7 Response 要取得 Response 的回傳內容方法有兩種 12345// method 1(string) $response->getBody();// method 2$response->getBody()->getContents(); 取得的內容會一模一樣,但是同一個 method 執行兩次是有差異的 1234567// method 1(string) $response->getBody(); // 正常回傳(string) $response->getBody(); // 正常回傳// method 2$response->getBody()->getContents(); // 正常回傳$response->getBody()->getContents(); // 空值 後來去查了一下原始碼發現,原來 __toString 會執行 seek(0),自動將指標歸零,才能一直重覆執行啊 1234567891011121314// 部份程式碼class Stream implements StreamInterface{ public function __toString() { try { // 就是這一行 $this->seek(0); return (string) stream_get_contents($this->stream); } catch (\Exception $e) { return ''; } }}
樹莓派用 command 連結藍芽 發表於 2020-07-21 分類於 raspberry pi 執行 bluetoothctl 後會顯示 [bluetooth] 1sudo bluetoothctl 裝 agent 設為 on 12agent ondefault-agent 掃瞄裝置 1scan on 配對 1pair xx:xx:xx:xx:xx:xx 信任裝置 1trust xx:xx:xx:xx:xx:xx 連線 1connect xx:xx:xx:xx:xx:xx
Redis Connection Refused 發表於 2020-07-21 分類於 redis 將 Redis 升級到 6.0.5 後,執行 redis-cli 後會提示 Could not connect to Redis at 127.0.0.1:6379: Connection refused,這時只要修改 redis.conf 將 bind 刪除及將 protected-mode 設為 no 即可 12345# 將 bind 刪除或標註# bind 192.168.0.5# 將 protected-mode yes 改為 noprotected-mode no
用 Blade 來 Render Markdown 發表於 2020-07-14 分類於 laravel 用 markdown 來寫文件是一個超級便利的事情,如果能再加上 Laravel 的 Blade 就更棒了,好在 Blade 可以做這件事,兩個願望一次達成啊 How to安裝 markdown 套件1composer require league/commonmark Render Markdown123456789101112131415161718192021222324252627282930use Illuminate\View\Factory;use League\CommonMark\CommonMarkConverter;use League\CommonMark\Environment;class DocsController extends Controller{ /** * @var Factory */ private $viewFactory; public function __construct(Factory $viewFactory) { $this->viewFactory = $viewFactory; // 設定 md 的副檔名用 blade engine render $this->viewFactory->addExtension('md', 'blade'); } public function index() { $environment = Environment::createGFMEnvironment(); $converter = new CommonMarkConverter([ 'allow_unsafe_links' => false, ], $environment); // 透過 blade engine 及 markdown parser 做轉換 return $converter->convertToHtml($this->viewFactory->make('demo.md')); }}
Windows 10 在 1G 記憶體的機器如何跑的順暢 發表於 2020-07-06 分類於 windows Memory Compression用 administrator 的身份並用 powershell 執行以下命令 停用 1Disable-MMAgent -mc 啟用 1Enable-MMAgent -mc Superfetch用 administrator 的身份並用 powershell 執行以下命令 停用 1Stop-Service -Force -Name "SysMain"; Set-Service -Name "SysMain" -StartupType Disabled 啟用 1Stop-Service -Force -Name "SysMain" Windows Definder用 administrator 的身份並用 powershell 執行以下命令,參考 停用 1Set-MpPreference -DisableIntrusionPreventionSystem $true -DisableIOAVProtection $true -DisableRealtimeMonitoring $true -DisableScriptScanning $true -EnableControlledFolderAccess Disabled -EnableNetworkProtection AuditMode -Force -MAPSReporting Disabled -SubmitSamplesConsent NeverSend recca0120 15 文章 8 分類 14 標籤 GitHub E-Mail FB Page