Skip to content

Instantly share code, notes, and snippets.

View stojg's full-sized avatar

Stig Lindqvist stojg

View GitHub Profile
@stojg
stojg / Daemon.php
Last active August 29, 2015 13:56
A silverstripe type daemon example
<?php
/**
* This is an example of daemon that runs for a set period of time and the quits. It only allows one task at time
* by using a PID file
*
* The scripts have a few timers that might need explaining
*
* First there are the $this->anotherInstanceRunning($this->timeout) that checks if a process haven't updated the
* pidfile in $this->timeout seconds, then we declare it as dead and start the task, otherwise it quits.
@stojg
stojg / happy.php
Created February 28, 2014 07:57
happy.php
<?php
interface Text {
public function __toString();
}
class HappyBirthday implements Text {
private $restOfLine = '';
<?php
class MemberAutocompleteField extends TextField {
private static $allowed_actions = array('suggest');
public $maxSuggestionsNum = 50;
public function Field($properties = array()) {
Requirements::javascript("mysite/javascript/MemberAutocompleteField.js");
@stojg
stojg / bad.php
Created March 21, 2014 10:25
petpeve
<?php
class Model {
public function saveSomething(Member $member, $data) {
if($member &&
$member instanceof Member &&
$member->exists() &&
Permissions::check($member, 'CMS_MAIN')) {
// dataobject banana
@stojg
stojg / CreateSomething.php
Created March 23, 2014 20:11
Example Create a bunch of dataobjects in a SilverStripe task
<?php
/**
* CreateSomething
*
* Create a bunch of dataobjects, in this case 10 Members.
*
*/
class CreateSomething extends BuildTask {
@stojg
stojg / myconfig.yaml
Last active August 29, 2015 14:01
Obfuscated DI
## _config/myconfig.yml
name: OrderController
---
OrderController:
dependencies:
mailerService: %$MailerService
Injector:
MailerService:
factory: MailerFactory
MailerFactory:
@stojg
stojg / session_timeout.php
Created May 27, 2014 22:05
session timeout
<?php
function start_session($timeout = 1800)
{
session_cache_limiter(false);
session_start();
ini_set("session.gc_maxlifetime", $timeout);
if (isset($_SESSION['lastSeen'])) {
$heartbeatAgo = time() - $_SESSION['lastSeen'];
$timeLeft = $timeout - $heartbeatAgo;
#!/bin/bash
ps -ylC apache2 | awk '{x += $8;y += 1} END {print "Apache Memory Usage (MB): "x/1024; print "Average Proccess Size (MB): "x/((y-1)*1024)}'
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "PHP Blue-Green Deployment environment template",
"Parameters" : {
"KeyName" : {
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instances",
"Type" : "String",
"MinLength" : "1",
"MaxLength" : "64",
"AllowedPattern" : "[-_ a-zA-Z0-9]*",
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Parameters" : {
"InstanceType" : {
"Description" : "WebServer EC2 instance type",
"Type" : "String",
"Default" : "m1.small"
}
"KeyName" : {