Skip to content

Instantly share code, notes, and snippets.

View sokil's full-sized avatar
🇺🇦
sapere aude

sokil

🇺🇦
sapere aude
View GitHub Profile
@sokil
sokil / I18n.js
Last active January 3, 2016 04:39
var i18n = function(messages) {
this._messages = messages || {};
this.addMessages = function(messages) {
this._messages = $.extend({}, this._messages, messages);
};
this.getMessage = function(code) {
return this._messages[code] || code;
};
};
var TemplateEngine = function() {
this.compile = function(template) {
return {
render: function(scope) {
var rendered = template;
for(var key in scope) {
rendered = rendered.replace(new RegExp('\{\{' + key + '\}\}', 'g'), scope[key]);
}
return rendered;
}
@sokil
sokil / variables.sh
Last active November 12, 2019 20:51
Bash / Sh / Console / Cli arguments
#!/bin/sh
echo 'Number of arguments: '$#
echo 'All arguments: '$*
echo 'All arguments: '$@
echo 'Shell parameters: '$-
echo 'Exit code of last app: '$?
echo 'Shell PID: '$$
ls -lah > /dev/null &
echo 'Last daemonized(&) PID: '$!
<?php
$request = $guzzleClient->get('/');
$request->addSubscriber(new \Guzzle\Plugin\Mock\MockPlugin(array(
new \Guzzle\Http\Message\Response(200, null, json_encode(array(
'error' => 0,
)))
)));
$response = $request->send();
@sokil
sokil / newsyslog.conf
Created May 19, 2014 11:53
Log rotation for nginx+php-fpm stack on FreeBSD
# log rotation for nginx+php-fpm stack on FreeBSD
# File: /etc/newsyslog.conf
/var/log/nginx/*.log 644 15 1000 @T00 JBCG /var/run/nginx.pid 30
/var/log/php_errors.log www:www 644 15 1000 @T00 JBCG /var/run/php-fpm.pid 30
<?php
/**
* @link http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.3.1
*/
function timestampToHttpDate($timestamp) {
return gmdate('D, d M Y H:i:s', $timestamp) . ' GMT';
}
@sokil
sokil / redis.queue.consumer.php
Last active August 29, 2015 14:05
Redis queue
<?php
$client = new \Predis\Client([
'read_write_timeout' => 0,
]);
while(true) {
$message = $client->blpop('queue1', 0);
}
@sokil
sokil / spl.observer.php
Last active November 23, 2017 22:00
PHP SPL Observer pattern
<?php
class Observable implements \SplSubject
{
private $_observers = array();
public function notify()
{
foreach($this->_observers as $observer) {
$observer->update($this);
<?php
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
// connect
$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
// create channel
$channel = $connection->channel();
<?php
use PhpAmqpLib\Connection\AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;
// connect
$connection = new AMQPConnection('localhost', 5672, 'guest', 'guest');
// create channel
$channel = $connection->channel();