Skip to content

Instantly share code, notes, and snippets.

Dave James Miller davejamesmiller

Block or report user

Report or block davejamesmiller

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@davejamesmiller
davejamesmiller / Laravel-Container.md
Last active Aug 18, 2019
Laravel's Dependency Injection Container in Depth
View Laravel-Container.md

Laravel's Dependency Injection Container in Depth

Translations: Korean (by Yongwoo Lee)

Laravel has a powerful Inversion of Control (IoC) / Dependency Injection (DI) Container. Unfortunately the official documentation doesn't cover all of the available functionality, so I decided to experiment with it and document it for myself. The following is based on Laravel 5.4.26 - other versions may vary.

Introduction to Dependency Injection

I won't attempt to explain the principles behind DI / IoC here - if you're not familiar with them you might want to read What is Dependency Injection? by Fabien Potencier (creator of the Symfony framework).

@davejamesmiller
davejamesmiller / monitor-off.ahk
Last active Aug 11, 2019
Turn monitor off with a keyboard shortcut with AutoHotKey
View monitor-off.ahk
; This is part of my AutoHotkey [1] script that turns off my monitor when I
; press Win+\.
; It also turns the screensaver on at the same time, so that Windows Live
; Messenger (and any other programs) know I am away.
; I don't have a password on my screensaver, so there is a second version
; (Win+Shift+\) that locks the PC as well.
; Note: Sometimes the monitor comes back on after a second and I have to
@davejamesmiller
davejamesmiller / sleep.ahk
Last active Aug 11, 2019
Sleep/suspend/hibernate computer with a keyboard shortcut using AutoHotKey
View sleep.ahk
; This is part of my AutoHotkey [1] script that puts my computer to sleep when I
; press Win+F12.
; I chose that keyboard shortcut because it's very similar to the Fn+F12
; keyboard shortcut on my laptop.
; I don't have my PC set to require a password to resume, so I also have a
; second version (Win+Shift+F12) in case I want to lock the PC first.
; [1]: http://www.autohotkey.com/
@davejamesmiller
davejamesmiller / run-program.ahk
Last active Aug 8, 2019
Run a program with a keyboard shortcut with AutoHotKey
View run-program.ahk
; This is a simple and pretty generic example of an AutoHotkey script to run a
; program when you press a keyboard shortcut. Add as many of these as you want
; to a .ahk file, and set that to be run at startup.
; See the Hotkeys reference [1] for details of the modifiers and keys available.
; [1]: http://www.autohotkey.com/docs/Hotkeys.htm
; Win+Alt+G - Open Gmail in Chrome
@davejamesmiller
davejamesmiller / ask.sh
Last active Aug 7, 2019
Bash General-Purpose Yes/No Prompt Function ("ask")
View ask.sh
# This is a general-purpose function to ask Yes/No questions in Bash, either
# with or without a default answer. It keeps repeating the question until it
# gets a valid answer.
ask() {
# https://gist.github.com/davejamesmiller/1965569
local prompt default reply
if [ "${2:-}" = "Y" ]; then
prompt="Y/n"
@davejamesmiller
davejamesmiller / wp-config.php
Last active Jul 31, 2019
Set WordPress site URL in the config file instead of the database
View wp-config.php
<?php
// WordPress stores the site URL in the database by default (which I have never
// understood), and it's a pain to have to type out the UPDATE SQL or search in
// phpMyAdmin to change it. This is a simple way to put the URL into
// wp-config.php instead.
// Note that you will still need to update any URLs that appear in the content,
// especially when you copy a database from a development site to production:
// https://gist.github.com/davejamesmiller/a8733a3fbb17e0ff0fb5
@davejamesmiller
davejamesmiller / README.md
Last active Jul 31, 2019
HTML to PDF conversion in PHP using wkhtmltopdf (and optionally Smarty)
View README.md

Recently I was asked to generate PDF invoices for an online shop. I looked at various PHP PDF generators, but wasn't particularly impressed with any of them.

Then I found (via Stack Overflow) a command-line HTML-to-PDF convertor called wkhtmltopdf, which uses WebKit (the same layout engine as Safari and Google Chrome) and therefore is very accurate.

There is a class for PHP integration on the Wiki, but I found it overly complicated and it uses temp files which aren't necessary. This is the code I wrote instead.

I used Smarty for generating the HTML for the PDF, but you can use any template engine, or pure PHP if you prefer.

Note: I originally tried to install wkhtmltopdf from source, but it's much easier to use the static binary instead.

@davejamesmiller
davejamesmiller / .htaccess
Last active Jul 14, 2019
Resize an image on the fly with PHP and cache it (like Drupal's ImageCache)
View .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+)/([^/]*)$ resize.php?size=$1&file=$2
@davejamesmiller
davejamesmiller / ._ Loading variables from .env files in Ansible.md
Last active Jul 11, 2019
Loading variables from .env files in Ansible
View ._ Loading variables from .env files in Ansible.md

Loading variables from .env files in Ansible

Ansible has various ways of looking up data from outside sources, including plain text password files, CSV files and INI files. But it doesn't seem to have a lookup for .env files, as used in Laravel projects, also available for PHP, Ruby, Node.js, Python and others.

One option is to launch Ansible with the Ruby dotenv command line script... But that requires Ruby, which seems like overkill to me.

So here is a simpler solution that I use. It consists of:

  1. The .env file itself
  2. A small shell script that loads the .env file into environment variables - ansible-playbook.sh
@davejamesmiller
davejamesmiller / paste-as-plain-text.ahk
Last active Jun 20, 2019
Paste clipboard contents as plain text with AutoHotKey
View paste-as-plain-text.ahk
; This is part of my AutoHotkey [1] script that pastes the contents of the
; clipboard as plain text when I press Ctrl+Alt+Shift+V.
; [1]: http://www.autohotkey.com/
; Ctrl+Alt+Shift+V
^!+v::
Clip0 = %ClipBoardAll%
ClipBoard = %ClipBoard% ; Convert to plain text
You can’t perform that action at this time.