Skip to content

Instantly share code, notes, and snippets.

View gundamew's full-sized avatar
🐢
Stepping forward

Steven Chen gundamew

🐢
Stepping forward
  • Taipei City, Taiwan
View GitHub Profile
@gundamew
gundamew / example.php
Last active January 19, 2021 10:30
PDO with options.
<?php
function getPDOInstance($host, $name, $user, $password)
{
$dsn = str_replace(
['{HOST}', '{NAME}'],
[$host, $name],
'mysql:host={HOST};dbname={NAME};charset=utf8mb4'
);
@gundamew
gundamew / download.php
Last active June 13, 2022 18:50
Download/Upload files with Guzzle.
<?php
$client = new GuzzleHttp\Client();
// Ref: https://guzzle.readthedocs.io/en/latest/request-options.html#sink-option
$client->request('GET', 'http://example.com', [
'sink' => '/tmp/' . sha1(file_get_contents('https://example.com'))
]);
@gundamew
gundamew / OrderPermissions.php
Created January 19, 2021 10:39
A Laravel native RBAC example. Try to do it myself.
<?php
namespace App\Permissions;
class OrderPermissions
{
const VIEW = 'view_order';
const VIEW_ANY = 'view_any_orders';
const CREATE = 'create_order';
const UPDATE = 'update_order';
@gundamew
gundamew / FooLogger.php
Last active April 26, 2022 19:31
Custom Logger for Laravel 5.6. Send messages to Telegram.
<?php
namespace App\Logging;
use DateTimeZone;
use Monolog\Logger;
use App\Logging\TelegramBotHandler;
use Monolog\Formatter\LineFormatter;
class FooLogger
@gundamew
gundamew / example.php
Created January 19, 2021 10:45
Adopted Google API client library with Laravel.
<?php
use Illuminate\Support\Carbon;
use Google_Client;
use Google_Service_Sheets;
use Google_Service_Sheets_Request;
use Google_Service_Sheets_ValueRange;
use Google_Service_Sheets_BatchUpdateSpreadsheetRequest;
class Example
@gundamew
gundamew / example.php
Created January 19, 2021 10:48
Create a custom role based on an existing role in WordPress.
<?php
if ( ! wp_roles()->is_role( 'my_customer' ) ) {
add_role(
'my_customer',
__( 'My customer' ),
array_merge(
wp_roles()->get_role( 'customer' )->capabilities,
array( 'read_shop_order', 'read_private_shop_orders' )
)
@gundamew
gundamew / example.sh
Created January 19, 2021 10:51
Finding a Linux AMI.
#!/usr/bin/env bash
set -euo pipefail
# Ref: Ref: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/finding-an-ami.html
aws ec2 describe-images \
--owners 099720109477 \
--filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-*" \
"Name=architecture,Values=x86_64" \
"Name=virtualization-type,Values=hvm" \
"Name=root-device-type,Values=ebs"
@gundamew
gundamew / Media.php
Created January 19, 2021 10:53
A many-to-many polymorphic relations example of Laravel.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Media extends Model {}
@gundamew
gundamew / AbstractCalculator.php
Last active January 19, 2021 11:07
A chain of responsibility pattern practice.
<?php
use App\Models\Member;
abstract class AbstractCalculator
{
/** @var self */
private $next = null;
/**
@gundamew
gundamew / ntd-coin-changer.php
Last active August 12, 2021 08:06
Get the lowest amount of coins for a given change value.
<?php
function change_make($price, $pay)
{
if ($price === 0 || $pay === 0 || $price > $pay) {
return;
}
$change = $pay - $price;