Skip to content

Instantly share code, notes, and snippets.

@mrself
mrself / index.php
Created October 21, 2018 22:59
PHP make combination array
function getCombination($input, $carry, &$result, $maxLength = null)
{
$maxLength = $maxLength ?: count($input);
$oldCarry = $carry;
foreach ($input as $index => $i) {
$carry[] = $i;
if (count($carry) <= $maxLength) {
$result[] = $carry;
}
getCombination(array_slice($input, $index + 1), $carry, $result, $maxLength);
@mrself
mrself / start.sh
Last active May 28, 2018 06:26
Set phpstorm debugging mode in console for windows
set XDEBUG_CONFIG="idekey=phpstorm"
@mrself
mrself / BaseEntity.php
Last active April 22, 2018 14:01
Set doctrine association
<?php
abstract class BaseEntity {
public function setAssoc($entityName) {
$inflector = ICanBoogie\Inflector::get();
$localProp = $inflector->pluralize($entityName);
foreach ($this->$localProp as $localAssocProp) {
$inParam = null;
foreach ($newAssocProps as $index => $item) {
@mrself
mrself / TestCase1.php
Last active March 29, 2018 10:44
Track errors in symfony when testing with phpunit when using WebTestCase
public function testSmth() {
$this->client = static::createClient();
$crawler = $this->client->request('GET', '/my-test-page');
if ($this->client->getResponse()->getStatusCode() !== 200) {
dump($crawler->filter('.text-exception')->first()->text());
dump($crawler->filter('.traces > li')->eq(0)->text());
dump($crawler->filter('.traces > li')->eq(1)->text());
throw new \Exception('Request to /my-test-page was not successfull');
}
@mrself
mrself / run.sh
Created March 27, 2018 16:06
Create sublime link for windows
echo "c:\Program Files\Sublime Text 3\sublime_text.exe" %1 > %systemroot%\system32\subl.bat
@mrself
mrself / nightwatch.sh
Last active March 26, 2018 09:51
Run nightwatch tests with manually started selenium server
#!/bin/bash
# It supposes you have bin directory in root project dir with `geckodriver` and `selenium-server-standalone-3.9.1.jar` files
# Create a log file for selenium
logfile="logs/selenium.log"
# Ensure a log file exists
if [ ! -e "$logfile" ] ; then
touch "$logfile"
fi
// If you need chains
array.reduce(function(def, arrayItem) {
return def.then(function() {
// Access to arrayItem
// return $.get, $.post, $.ajax or another deferred
});
}, $.when()).done(function(){
// all deferred are completed
});
(function($) {
$.parseQueryString = function(query) {
query = query || document.location.search;
if (!query) return null;
query = query.replace('?', '').split('&');
var i, part, property, val, params = {};
for (i = 0; i < query.length; i++) {
part = query[i].split('=');
property = part[0];
val = part[1];
@mrself
mrself / gulpfile.js
Last active August 29, 2015 14:20 — forked from ktmud/gulpfile.js
var BatchStream = require('batch-stream2')
var gulp = require('gulp')
var coffee = require('gulp-coffee')
var uglify = require('gulp-uglify')
var cssmin = require('gulp-minify-css')
var bower = require('gulp-bower-files')
var stylus = require('gulp-stylus')
var livereload = require('gulp-livereload')
var include = require('gulp-include')
var concat = require('gulp-concat')

Better local require() paths for Node.js

Problem

When the directory structure of your Node.js application (not library!) has some depth, you end up with a lot of annoying relative paths in your require calls like:

var Article = require('../../../models/article');

Those suck for maintenance and they're ugly.

Possible solutions