Ярлыки

.htaccess (4) тестирование (8) шаблоны проектирования (3) css (5) Debian (6) docker (2) Doctrine2 (6) Git (6) html (4) java (6) javascript (13) jquery (11) LFS (3) linux (23) mac os (4) mod_rewrite (2) MSSQL (4) MySQL (18) ORM Doctrine (17) patterns (3) PDO (3) perl (7) PHP (64) PHPUnit (8) Python (15) SEO (2) Silex (1) SimpleXML (1) SQL (14) ssh (4) Ubuntu (24) Yii1 (1) Zend Framework (19) ZendFramework2 (8)

понедельник, 28 сентября 2015 г.

Git. Patching.

git format-patch -1 <sha> # create patch
git apply --stat file.patch # show stats
git apply --check file.patch # check for errors before applying
git am < file.patch # apply the patch

среда, 26 августа 2015 г.

DynamoDB. AWS SDK for PHP.

Download and install DynamoDB from official site. For older versions connect with code below:
$client = \Aws\DynamoDb\DynamoDbClient::factory(array(
    'region' => 'us-west-2',
    'base_url' => 'http://localhost:8000',
    'key' => 'x',
    'secret' => 'y',
));
For 3.3.1 version:
$client = \Aws\DynamoDb\DynamoDbClient::factory(array(
    'region' => 'us-west-2',
    'endpoint' => 'http://localhost:8000',
    'credentials' => [
        'key' => 'x',
        'secret' => 'y',
    ],
    'version' => 'latest'
));
Search in nested arrays:

$client->putItem([
    'TableName' => $this->model,
    'Item' => [
        'Id' => ['S' => uniqid()],
        'Attrs' => [
            'M' => [
                'ComponentName' => ['S' => 'Beer'],
                'CAS' => [ 'S' => '3432-2-123']
            ]
        ]
    ]
]);


// search by nested key

$iterator = $client->getIterator('Scan', [
    'TableName' => $this->model,
    'FilterExpression' => 'Attrs.ComponentName = :name',
    "ExpressionAttributeValues" => [
        ":name" => ["S" => "Beer"]
    ],
]);

пятница, 21 августа 2015 г.

PHP. DOMDocument->loadHtml() and UTF-8.

PHP DOMDocument loadHTML don't encode UTF-8 correctly and here is the solution:
$profile = '

イリノイ州シカゴにて、アイルランド系の家庭に、9

'; $dom = new DOMDocument(); $dom->loadHTML(mb_convert_encoding($profile, 'HTML-ENTITIES', 'UTF-8')); echo $dom->saveHTML($dom->getElementsByTagName('div')->item(0));

четверг, 30 июля 2015 г.

Yii1. Internationalization (I18N) using DB.

Create the following tables:
CREATE TABLE SourceMessage
(
    id INTEGER PRIMARY KEY,
    category VARCHAR(32),
    message TEXT
);
CREATE TABLE Message
(
    id INTEGER,
    language VARCHAR(16),
    translation TEXT,
    PRIMARY KEY (id, language),
    CONSTRAINT FK_Message_SourceMessage FOREIGN KEY (id)
         REFERENCES SourceMessage (id) ON DELETE CASCADE ON UPDATE RESTRICT
);

INSERT INTO `SourceMessage` (`id`, `category`, `message`) VALUES
(1, 'foo', 'Hello');

INSERT INTO `Message` (`id`, `language`, `translation`) VALUES
(1, 'es', 'Hola');


Put component into the main config:
'components' => array(
        ...
        'messages'=>array(
            'class'=>'CDbMessageSource'
        ),
        ...
)
Use it everywhere:
class FooController extends CController {
    
    function actionBar() {
        
        Yii::app()->setLanguage('es');
        
        echo Yii::t('foo', 'Hello'); die; // will output 'Hola'
    }
}

суббота, 25 июля 2015 г.

JavaScript. Patterns.

// Namespace
// Global Object
var MYAPP = {};
// Constructors
MYAPP.Foo = function () {};
MYAPP.Bar = function () {};
// Variable
MYAPP.baz = 'foo';
// Container Object
MYAPP.modules = {};
// Submodules
MYAPP.modules.module1 = {};

// Dependencies
var myFunction = function () {
  var event = YAHOO.util.Event,
      dom = YAHOO.util.Dom;
  
  // use event, dom ...
};

//myFunction(); // Uncaught ReferenceError: YAHOO is not defined

// Public and Private methods
function Foo() {
  var bar = 'baz';
  this.getBar = function() {
    return bar;
  };
}

//console.log((new Foo()).getBar()); // baz
//console.log((new Foo()).bar); // undefined

var Foo = (function () {
  // Private attribute
  var bar = 'baz';
  // Private method
  var boo = function () {
    return 'boo';
  };
  return {
    // Public (Privileged) method
    getBar: function () {
      return boo() + bar;
    }
  };
})();

console.log(Foo.getBar()); // boobaz
console.log(Foo.bar); // undefined
//Foo.boo(); // Uncaught TypeError: Foo.boo is not a function

// Private methods and Prototypes

function MyFoo() {
  var bar = 'bar';
  this.getBar = function () {
    return bar;
  };
}

MyFoo.prototype = (function () {
  var baz = 'baz';
  return {
    getBaz: function () {
      return baz;
    }
  };
})();

var myFoo = new MyFoo();
console.log(myFoo.getBar()); // bar
console.log(myFoo.getBaz()); // baz

суббота, 11 апреля 2015 г.

JavaScript. Модуль.

String.prototype.deentityify = function () {
    var entity = {
        quot: '"',
        lt: '<',
        gt: '>'
    };
    return function () {
        return this.replace(/&([a-z]+)?;/g, function (a, b) {
            var r = entity[b];
            return typeof r === 'string' ? r : a;
        });
    }
}();

alert('<foobar>bazbaz</foobar>'.deentityify());