Skip to content

Instantly share code, notes, and snippets.

View hungtrinh's full-sized avatar

Hưng Trịnh hungtrinh

  • Ha Noi, Viet Nam
View GitHub Profile
@hungtrinh
hungtrinh / Vagrantfile
Created March 1, 2017 13:21
Vagrant configuration file with OS detection - on windows we mount shared folders without NFS - on all other OS we use NFS for speeding up the project inside the virtual machine
# -*- mode: ruby -*-
# vi: set ft=ruby :
module OS
def OS.windows?
(/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
end
def OS.mac?
(/darwin/ =~ RUBY_PLATFORM) != nil
@hungtrinh
hungtrinh / pre-commit
Created March 7, 2017 10:23 — forked from mklabs/pre-commit
run jshint as git/hg hooks, NO COMMIT IF NO LINT FREE.
#!/usr/bin/env node
// todo: try to require jshint here, instead of spawning process, then fallback to spawning process.
var jshint = nrequire('jshint');
if (jshint) return process.exit(0);
// jshint not installed locally, spawn process instead.
// basically the same, but less pretty.
var exec = require('child_process').exec;
@hungtrinh
hungtrinh / bitwise-permission-checking.php
Created April 10, 2018 07:00
Check permission using bitwise operators
Initially, I found bitmasking to be a confusing concept and found no use for it. So I've whipped up this code snippet in case anyone else is confused:
<?php
// The various details a vehicle can have
$hasFourWheels = 1;
$hasTwoWheels = 2;
$hasDoors = 4;
$hasRedColour = 8;
@hungtrinh
hungtrinh / launch.json
Last active August 21, 2018 09:46
xdebug remote vscode config
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug - Remote",
"type": "php",
"request": "launch",
"port": 9000,
"pathMappings": {
"/var/www/html/your_app_on_server": "${workspaceFolder}",
@hungtrinh
hungtrinh / introrx.md
Created February 26, 2019 17:32 — forked from staltz/introrx.md
The introduction to Reactive Programming you've been missing
@hungtrinh
hungtrinh / index.jsx
Created March 15, 2019 09:25 — forked from avinmathew/index.jsx
Multiple layouts with React Router v4
import React from "react"
import { Route, Switch } from "react-router-dom"
const AppRoute = ({ component: Component, layout: Layout, ...rest }) => (
<Route {...rest} render={props => (
<Layout>
<Component {...props} />
</Layout>
)} />
)
@hungtrinh
hungtrinh / CorsMiddlewareFactory.php
Created June 20, 2019 02:41 — forked from akrabat/CorsMiddlewareFactory.php
Expressive factory to use CORS middleware
<?php declare(strict_types=1);
namespace App\Factory;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Tuupola\Middleware\Cors;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\JsonResponse;
use Zend\ProblemDetails\ProblemDetailsResponseFactory;
use Zend\Stratigility\Middleware\CallableMiddlewareWrapper;
@hungtrinh
hungtrinh / Export CSV.php
Created July 24, 2019 03:01 — forked from SimonEast/Export CSV.php
PHP Example: Stream a CSV File to Browser with GZIP Compression (exporting from MySQL/PDO)
<?php
/**
* This script performs a full dump of a database query into
* CSV format and pipes it directly to the browser.
*
* - YES, the browser will save the CSV file to disk
* - YES, it should support large files without using massive amounts of memory
* - YES, it compresses the request using GZIP to reduce download time
*/
@hungtrinh
hungtrinh / discountRateByMembershipType.php
Last active November 3, 2019 14:32
Write Production Without Test
<?php
function discountRateByMembershipType($userId) {
//$date = getCurrentDateFromLocalTimeOrRemoteTimeServer(); //with remote time server, we can't fake date
//if (is_not_black_friday($date) return 0; // we can't not test CORE BUSINESS if add this spec
$membershipType = getMemberShipTypeFromDatabaseOrRemoteApi($userId);
//CORE BUSINESS need coverage by unit test or manual test 
if ('platinum' === $membershipType) return 0.15;
if ('gold' === $membershipType) return 0.1;
if ('slive' === $membershipType) return 0.05;
throw new UnknowMembershipTypeException('invalid membership type');
@hungtrinh
hungtrinh / test_with_platinum_memeber_will_discount_fifteen_percent.php
Last active November 3, 2019 15:24
First specification, first unittest coe, first consume code
<?php
function test_with_platinum_member_will_discount_fifteen_percent() {
if ( 0.15 === discountRateByMembershipType('platinum') {
echo 'Test Done';
} else {
echo 'Test Fail';
}
}