Blog: Archive

2008-08-01 - 2008-08-31

PHP5 events class

Posted on 2008-08-04 09:42:01 UTC in 60° 43.992 N 24° 46.302 E Riihimäki, FI to .

While I was building Duct Frameworks server side with PHP5 I realized I needed some kind of event support for it.
So I created similar approach that ActionScript uses. You can download the class with example from here.

Hopefully this comes in handy for someone else also. 

Here is simple example also:

 

<?php

class mother extends events_observable
{
public function __construct()
{
parent::__construct();

$this->addEventListener(events_event::ONCONSTRUCT, events::delegate($this, 'on_object_constructed'));
}

public function on_object_constructed($target, $args)
{
echo "{$target->name} has born.";
}
}

class child extends events_observable
{
public $name;

public function __construct($name)
{
parent::__construct();

$this->name = $name;

$this->dispatchEvent(new events_event(events_event::ONCONSTRUCT, $this));
}
}

$mother = new mother();
$child = new child('Jack');

?>

PHP5 xmlObject

Posted on 2008-08-05 15:37:23 UTC in 60° 43.992 N 24° 46.302 E Riihimäki, FI to .

UPDATE [08/06/2008]: Version update. Did some fixing and made it work better with larger structures.

Here's another simple little helper I developed for Duct framework.
I use this one to convert my arrays to XML. As Duct automatically scaffolds objects from XML/YAML to database
and uses the same methods to render the database objects to html forms for editing I needed some quick way to
transform this data to and from XML. Class end examples are here

Usage example:

<?php
$resource = new xmlObjectItem();
$resource->items = array(
'id' => 100,
'name' => "Jerry Jalava",
'person_id' => 1
);
$resource->attributes = array(
'person_id' => array(
'is_assoc' => true,
'foreign_model' => 'person',
'foreign_key' => 'id'
)
);

$resource_set = new xmlObjectSet();
$resource_set->wrapper_id = 'resource';
$resource_set->items[] = $resource;

$projects = array();
for ($i=1; $i <= 1; $i++)
{
$object = new xmlObjectItem();
$object->items = array(
'id' => $i,
'name' => "Project {$i}",
'manager_id' => $i+1,
'resources' => $resource_set
);

$projects[] = $object;
}

$project_set = new xmlObjectSet();
$project_set->wrapper_id = 'project';
$project_set->items = $projects;

$clients = array();
for ($i=1; $i <= 1; $i++)
{
$client = new xmlObjectItem();
$client->items = array(
'id' => $i,
'name' => "Client {$i}",
'contact_id' => $i+1,
'team-leader' => null,
'projects' => $project_set
);
$client->attributes = array(
'contact_id' => array(
'is_assoc' => true,
'foreign_model' => 'person',
'foreign_key' => 'id'
)
);

$clients[] = $client;
}
$client_set = new xmlObjectSet();
$client_set->wrapper_id = 'client';
$client_set->items = $clients;

echo "complex set:\n";
var_dump($client_set);

echo "\n--------toXml---------\n";

$xmlSet_clients = xmlObjects::toXml($client_set, 'clients');
echo "complex:\n";
echo $xmlSet_clients;

echo "\n--------fromXml---------\n";

$client_set = xmlObjects::fromXml($xmlSet_clients);
echo "client_set root: {$client_set->root}\n";
var_dump($client_set);

echo "\n--------back toXml---------\n";

$xmlSet_clients2 = xmlObjects::toXml($client_set);
echo "complex:\n";
echo $xmlSet_clients2;

if ($xmlSet_clients == $xmlSet_clients2) {
echo "\n\nVALID SET CONVERSION\n\n";
}

?>

Midgard Textmate bundle

Posted on 2008-08-07 18:01:38 UTC in 60° 43.992 N 24° 46.302 E Riihimäki, FI to .

For quite a while Joonas and I have planned to do a Textmate bundle for Midgard CMS to fasten and ease the code development and work as a reference to the thousands of methods MidCOM and Midgard has.

Finally last night we just decided to put the project on properly and uploaded it to GitHub.
Today I added (and adding right now) quite alot of the basic methods.

Here's a screenshot of shortcuts for the Query Builder.

with these methods even complex queries can be built lightning fast. We will be adding more stuff as time goes by.

Back