Skip to content

Instantly share code, notes, and snippets.

@tomzx
tomzx / gist:9354214
Created March 4, 2014 19:51
Give ALL permissions to NAME_dev on %_dev databases
GRANT CREATE ROUTINE, CREATE VIEW, ALTER, SHOW VIEW, CREATE, ALTER ROUTINE, EVENT, INSERT, SELECT, DELETE, TRIGGER, GRANT OPTION, REFERENCES, UPDATE, DROP, EXECUTE, LOCK TABLES, CREATE TEMPORARY TABLES, INDEX ON `%\_dev`.* TO 'NAME_dev'@'localhost';
FLUSH PRIVILEGES;
@tomzx
tomzx / gist:a98fd99736c656dd19b1
Created May 25, 2014 23:48
Model attribute accessors ala ruby (for Laravel)
<?php
trait ModelAttribute
{
protected $attr_reader = [];
protected $attr_writer = [];
protected $attr_accessor = [];
public function getAttribute($key)
{
@tomzx
tomzx / semver-changes-v3.0.0-beta2
Last active August 29, 2015 14:14
deployphp/deployer semver changes from v2.0.4 to v3.0.0-beta.2
Suggested semantic versioning change: MAJOR
Class (MAJOR)
+-------+---------------------------------------------------------------------------+--------------------------------------------------------+-----------------------------------------+------+
| Level | Location | Target | Reason | Code |
+-------+---------------------------------------------------------------------------+--------------------------------------------------------+-----------------------------------------+------+
| MAJOR | E:\Tom\Documents\GIT\deployer\src\Console\RunTaskCommand.php#24 | Deployer\Console\RunTaskCommand | Class was removed. | V005 |
| MAJOR | E:\Tom\Documents\GIT\deployer\src\Environment.php#13 | Deployer\Environment | Class was removed. | V005 |
| MAJ
@tomzx
tomzx / gist:abc1dc4b5860bc711e62
Created August 4, 2015 19:09
PHP Convert MySQL timezone format to PHP timezone
function convertMySQLTimezone($timezone)
{
$result = preg_match('/(?P<sign>\+|-)(?P<hours>\d{1,2}):(?P<minutes>\d{1,2})/', $timezone, $match);
if ( ! $result) {
return timezone_name_from_abbr('UTC');
}
$sign = $match['sign'] === '-' ? -1 : 1;
$hours = $match['hours'];
$minutes = $match['minutes'];
@tomzx
tomzx / gh,js
Last active September 18, 2015 00:42
gh profiling using node --prof --log-timer-events and https://v8.googlecode.com/svn/branches/bleeding_edge/tools/profviz/profviz.html
Statistical profiling result from null, (1219 ticks, 0 unaccounted, 0 excluded).
[Shared libraries]:
ticks total nonlib name
834 68.4% /lib/x86_64-linux-gnu/libc-2.19.so
268 22.0% /usr/local/bin/node
96 7.9% [vdso]
18 1.5% /lib/x86_64-linux-gnu/libpthread-2.19.so
1 0.1% /lib/x86_64-linux-gnu/ld-2.19.so
@tomzx
tomzx / gist:5663921
Last active December 17, 2015 19:58
protected function getForeignKeySqlDefinition(ForeignKey $foreignKey)
{
$def = '';
if ($foreignKey->getConstraint()) {
$def .= ' CONSTRAINT ' . $this->quoteColumnName($foreignKey->getConstraint());
}
$columnNames = array();
foreach ($foreignKey->getColumns() as $column) {
$columnNames[] = $this->quoteColumnName($column);
@tomzx
tomzx / gist:8584838
Created January 23, 2014 19:09
List all FK relations
SELECT
ke.referenced_table_name parent,
ke.table_name child,
ke.constraint_name
FROM
information_schema.KEY_COLUMN_USAGE ke
WHERE
ke.referenced_table_name IS NOT NULL
ORDER BY
ke.referenced_table_name;
@tomzx
tomzx / melody-bootstrapper.php
Last active January 21, 2016 01:45
Melody bootstrapper
<?php
<<<CONFIG
packages:
- "your/dependencies: 1.0"
CONFIG;
if ( ! file_exists('composer.phar')) {
file_put_contents('composer.phar', fopen('https://getcomposer.org/composer.phar', 'r'));
}
@tomzx
tomzx / measure.php
Last active January 21, 2016 23:51
Shorten melody-like script to install composer packages for a script
<?php
$packages = [
// Your packages here
'tomzx/file-tracker: ~0.1@dev',
];
$commands = [
['require', 'packages' => $packages, '--no-update' => true],
['install'],
@tomzx
tomzx / xml2js.php
Created February 15, 2016 04:02
PHP xml2js (attributes to $, nodeValue to _)
<?php
function xml2js($xmlnode)
{
$root = func_num_args() <= 1;
$jsnode = [];
if ($root) {
$nodename = $xmlnode->getName();
$jsnode[$nodename] = xml2js($xmlnode, true);