I’m using phing to automate a deploy of a php web application to production, phing is a really powerful tool (and easy to use), AdhocTaskdefTask allow to implement custom tags with input and output parameters, for example, I needed to adjust some relative path in production, there’s a simple adhoc task to get the new paths:
<!--
Usage:
<relative-path from="${from}" to="${to}"
output="project_property" />
-->
<adhoc-task name="relative-path"><![CDATA[
class RelativePath extends Task {
private $from;
private $to;
private $output;
function setFrom($from) {
$this->from = $from;
}
function setTo($to) {
$this->to = $to;
}
function setOutput($output=null) {
$this->output = $output;
}
function main() {
$relpath = $this->find_relative_path($this->from, $this->to);
$this->log("relpath = " . $relpath);
if ($this->output != null) {
$this->project->setProperty($this->output, $relpath);
}
}
/**
* credits: https://gist.github.com/ohaal/2936041
* Find the relative file system path between two file system paths
*
* @param string $frompath Path to start from
* @param string $topath Path we want to end up in
*
* @return string Path leading from $frompath to $topath
*/
function find_relative_path ( $frompath, $topath ) {
$from = explode( DIRECTORY_SEPARATOR, $frompath ); // Folders/File
$to = explode( DIRECTORY_SEPARATOR, $topath ); // Folders/File
$relpath = '';
$i = 0;
// Find how far the path is the same
while ( isset($from[$i]) && isset($to[$i]) ) {
if ( $from[$i] != $to[$i] ) break;
$i++;
}
$j = count( $from ) - 1;
// Add '..' until the path is the same
while ( $i <= $j ) {
if ( !empty($from[$j]) ) $relpath .= '..'.DIRECTORY_SEPARATOR;
$j--;
}
// Go to folder from where it starts differing
while ( isset($to[$i]) ) {
if ( !empty($to[$i]) ) $relpath .= $to[$i].DIRECTORY_SEPARATOR;
$i++;
}
// Strip last separator
return substr($relpath, 0, -1);
}
}
]]></adhoc-task>
PhpStorm versions older than 9.5 may have some trouble running phpunit tests:
PHP Fatal error: Call to undefined method PHP_CodeCoverage_Filter::addFileToBlacklist()
so:
composer require --dev "phpunit/phpunit=4.*" # to install phpunit as dev dependency then...
# not working with phpunit 5
composer require --dev phpunit/php-code-coverage dev-master#b8436b000263f6d72fbad1d36890e247ce84857e
Setting timezone for apache/php/mysql (logs timestamp also)
# apache2 on debian (it get system tz)
dpkg-reconfigure tzdata
# this doesn't work (maybe useful for .htaccess/vhosts?)
echo "SetEnv TZ Europe/Rome" > /etc/apache2/conf-available/tz-rome.conf
service apache2 reload
# php
echo 'date.timezone = "Europe/Rome"' > /etc/php5/apache2/conf.d/30-tz-rome.ini
# mysql (untested)
default-time-zone = "Europe/Rome"
Test shell script using curl
#!/bin/sh
url=http://host/ci/index.php/xmlrpc_server
data=@-
cat <<EOF | curl -i -H "Content-Type: text/xml;" -d $data $url
<?xml version="1.0" ?>
<methodCall>
<methodName>Greetings</methodName>
<params>
<param>
<value><string>test string</string></value>
</param>
</params>
</methodCall>
EOF
~root:
apt-get install lighttpd mysql-server php5-common php5-cgi php5 php5-mysql
lighty-enable-mod fastcgi-php
service lighttpd force-reload
chown www-data:www-data /var/www
chmod 775 /var/www
usermod -a -G www-data pi
~pi:
newgrp www-data # needed only once after usermod
echo '<?php phpinfo(); ' > /var/www/index.php
based on this article: Dependency resolving algorithm and the python snippet below:
def dep_resolve(node, resolved, unresolved):
unresolved.append(node)
for edge in node.edges:
if edge not in resolved:
if edge in unresolved:
raise Exception('Circular reference detected: %s -> %s' % (node.name, edge.name))
dep_resolve(edge, resolved, unresolved)
resolved.append(node)
unresolved.remove(node)
a Php implementation to use with CodeIgniter to enqueue scripts like WordPress:
class DepsNode {
public $name;
public $edges = array();
function __construct($name) {
$this->name = $name;
}
function addEdge($edge) {
array_push($this->edges, $edge);
}
}
class JsDepNode extends DepsNode {
public $src;
public $deps;
public $version;
function __construct($name, $src, $deps = array(), $version = '') {
parent::__construct($name);
$this->src = $src;
$this->deps = $deps;
$this->version = $version;
}
}
class Jsdeps {
public $scripts = array();
public $resolved = array();
public function enqueue($name, $src, $deps = array(), $version = '') {
$script = new JsDepNode($name, $src, $deps, $version);
foreach($deps as $dep) {
$script->addEdge($dep);
}
$this->scripts[$name] = $script;
}
public function render() {
$unresolved = array();
foreach($this->scripts as $script) {
$this->dep_resolve($script, $this->resolved, $unresolved);
}
foreach ($this->resolved as $script_name) {
$script = $this->scripts[$script_name];
$ver = $script->version !== '' ? '?'.$script->version : '';
echo "<script src=\"{$script->src}{$ver}\"></script>\n";
}
}
/*
public function test() {
$a = new JsDepNodex("a");
$this->scripts["a"] = $a;
$b = new JsDepNodex("b");
$this->scripts["b"] = $b;
$c = new JsDepNodex("c");
$this->scripts["c"] = $c;
$d = new JsDepNodex("d");
$this->scripts["d"] = $d;
$e = new JsDepNodex("e");
$this->scripts["e"] = $e;
$a->addEdge($b->name); # a depends on b
$a->addEdge($d->name); # a depends on d
$b->addEdge($c->name); # b depends on c
$b->addEdge($e->name); # b depends on e
$c->addEdge($d->name); # c depends on d
$c->addEdge($e->name);
//$d->addEdge($b->name);
$unresolved = array();
$this->dep_resolve($a, $this->resolved, $unresolved);
foreach($this->resolved as $node) {
echo "- $node\n";
}
}*/
public function dep_resolve($node, &$resolved, &$unresolved) {
if(in_array($node->name, $resolved))
return;
array_push($unresolved, $node->name);
foreach($node->edges as $edge) {
if(!in_array($edge, $resolved)) {
if(in_array($edge, $unresolved)) {
log_message('error', "Jsdeps: circular reference: {$node->name} -> {$edge}");
return;
}
$this->dep_resolve($this->scripts[$edge], $resolved, $unresolved);
}
}
array_push($resolved, $node->name);
$k = array_search($node->name,$unresolved);
if($k!==false) {
unset($unresolved[$k]);
}
}
}
I thought would be nice to have a template engine with the same syntax for both Php and Javascript, I’m thinking about a website which could work on Php only then scale with Javascript, below a list of something that already exist:
So, mustache seems to be most interesting, I’ll give a try sooner or later, in the while I been tempted (still) to write my own, inspired by something simple like JavaScript template engine in just 20 lines and JavaScript Micro-Templating. The target would be a template engine with the same exact syntax for both Php and Javascript, a generator, script or command, which given a template would create a function for both the languages (eventually any language introducing a output backend concept).
Doing some fast test immediately shown an horrific implementation design, Javascript’s (json) objects are unsorted (arrays are sorted).