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
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 / 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 / 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 / example.js
Last active January 19, 2021 10:27
IIFE skeleton.
(function($, undefined) {
'use strict';
if (window.className !== undefined) {
return;
}
window.className = {
init: function() {
// code...
<?php
defined( 'ABSPATH' ) || exit;
class WC_REST_API_Key {
// See WC_Auth::create_keys
public static function create( $user_id, $description, $permissions = 'read' ) {
global $wpdb;
@gundamew
gundamew / helper.php
Last active September 27, 2019 08:51
Simple helpers to run tasks before/until the deadline.
if (! function_exists('stop_before')) {
function stop_before(callable $func, DateTimeInterface $dt)
{
$tz = $dt->getTimezone();
$now = new DateTime('now', $tz);
if ($now > $dt) {
call_user_func($func);
}
@gundamew
gundamew / restart-docker-containers.sh
Created August 16, 2018 07:07
Stop , remove, and rebuild Docker containers.
#!/usr/bin/env bash
set -euo pipefail
docker stop $(docker ps -aq)
docker rm $(docker ps -aq)
docker rmi $(docker images --format "{{.Repository}}" | grep 'bsdock')
[[ -f "$PWD/docker-compose.yml" ]] && docker-compose up -d --build
@gundamew
gundamew / update.sh
Created September 1, 2018 07:12
Script to update Homebrew and Composer packages.
#!/usr/bin/env bash
set -euo pipefail
echo "Updating Homebrew..."
brew update && brew upgrade && brew cleanup
echo "Updating Composer..."
composer self-update --clean-backups && composer g update && composer g dump-autoload
#composer g clear-cache
@gundamew
gundamew / McryptAes256.php
Last active June 13, 2016 17:31
An AES encryption implementation in PHP with Mcrypt module.
<?php
/**
* An AES encryption implementation in PHP with Mcrypt module.
*
* Key length is 256, CBC mode, and use PKCS#7 padding.
*
* Example:
* $aes = new McryptAes256;
* $encryptedString = $aes->encrypt($rawString);
* $decryptedString = $aes->decrypt($encryptedString);
@gundamew
gundamew / OpensslAes256.php
Last active June 1, 2016 17:34
An AES encryption implementation in PHP with OpenSSL module.
<?php
// Set cipher method
$cipherMethod = 'AES-256-CBC';
// Set encryption key
// The key length must be 256 bits long
$key = (empty($key)) ? openssl_random_pseudo_bytes(32) : '...';
// Set initialization vector
$iv = (empty($iv)) ? openssl_random_pseudo_bytes(openssl_cipher_iv_length($cipherMethod)) : '...';