Examples

5.8 Feature showcase: Accessing class interna

at 2010-08-30 in Examples5.8-SERIES by friebe

NeoThe XP Framework's 5.8-SERIES contains a feature that will allow the reflection API to access private and protected members. Of course, just because you can, doesn't mean you should, as this does allow you to break the encapsulation principle.

On the other side, this feature comes in handy for serialization mechanisms, as well as for reflection-based factory or delegation patterns.


(more)

Generics of primitives

at 2010-08-10 in Examples5.8-SERIES by friebe

Like in C# (and unlike Java), primitives can be used as components of generics in the 5.8-SERIES. Given a vector util.collections.Vector<string>, the following applies:

  // OK
$v[]= "Hello";

// Throws a lang.IllegalArgumentException
$v[]= 1;

Primitives can also be used in other generic collections types, e.g. Set and Map implementations as well as Stack and Queue.



Fluent XML with the XP Framework

at 2010-06-22 in Examples by friebe

To create an XML tree programmatically, the XP Framework has offered the xml.Tree class since its beginning in 2001. Recently, we've added a fluent interface to it, allowing for the following:

  $t= create(new Tree())->withRoot(create(new Node('book'))
->withChild
(new Node('author', 'Timm'))
->withChild
(new Node('isbn', '978-0000000000'))
->withChild
(new Node('title', 'Fluent XML with the XP Framework'))
);



Merging XAR files

at 2010-01-30 in Examples by kiesel

With the 5.7.7-RELEASE there has a nitfy small feature been added to the xar command: merging multiple XAR files. XAR files are the archives in which XP Framework applications can ship their classes and/or resource files in a way comparable to Java`s .jar-files.

Combining multiple xar files into one can be handy one the one hand, because you can merge all dependencies and the application itself into one single file and only have to provide the one. On the other hand, for applications where startup or I/O performance is a concern, it can be useful: each xar file involved in an application comes with a startup time penalty - the file must be opened, the index must be read before the first file can be retrieved. Here, a single file instead of many files mean increased startup performance and less I/O calls of an application.

This is how it works:


Alex@mobile ~/dev/xp.public/trunk
$ xar cf one.xar ChangeLog

Alex@mobile ~/dev/xp.public/trunk
$ xar cf two.xar skeleton/VERSION=VERSION

Alex@mobile ~/dev/xp.public/trunk
$ xar mf merge.xar one.xar two.xar

Alex@mobile ~/dev/xp.public/trunk
$ xar tvf merge.xar
58.978 ChangeLog
10 VERSION



Reading gzip'ed websites

at 2010-01-08 in Examples by friebe

To reduce bandwidth some servers have their content zipped using solutions such as Apache's mod_deflate. This is done only if the Accept-Encoding request header contains the string gzip. Combining the XP Framework's peer.http API with the new io.streams.GzDecompressingInputStream one can accomplish this compression:

  $conn= new HttpConnection($args[0]);
$response= $conn->get(array(), array('Accept-Encoding' => 'gzip'));

if
('gzip' == $response->getHeader('Content-Encoding')) {
$in= new GzDecompressingInputStream($r->getInputStream());
} else {
$in= $r->getInputStream();
}

// Now read $in as with any other stream...



Compressing data

at 2010-01-06 in Examples by friebe

The possibly easiest way to compress data in PHP is to use the string-in, string-out functions such as gzdeflate or bzcompress. For small number of bytes being passed in, this is fast and convenient, but it doesn't do well when working with large files, for example:

  FileUtil::setContents(
$out,
gzdeflate
(FileUtil::getContents($in))
);
In one test I ran here, this piece of code used roughly 50MB peaks to compress a 26 MB input file. As of 5.7.7, the XP Framework will provide compressing output streams enabling you to compress one chunk at a time and saving more than 90% of memory:
  $is= $in->getInputStream();
$os= new DeflatingOutputStream($out->getOutputStream());
while
($is->available() > 0) {
$os->write($is->read());
}
$is->close();
$os->close();
Sure, this is more code, but it constantly uses not more than 0.5 MB while running. If you now have a look at the new io.streams.StreamTransfer class you can simplify the above code even more (and still save the memory).



Type duplication in xpcli injections

at 2009-10-14 in ExamplesAnnouncements by friebe

It's just become another bit easier to write command line classes in the XP framework ("xpclis"). When using the @inject annotation you no longer have to specify the type parameter if you use a so-called "type hint" - so instead of writing the following:

  #[@inject(type= 'rdbms.DBConnection', name= 'caffeine')]
public function setConnection(DBConnection $conn) { ... }
...you can now omit the type part - it will be inferred from the first parameter's type:
  #[@inject(name= 'caffeine')]
public function setConnection(DBConnection $conn) { ... }



New xml.Node::withChild() method

at 2009-09-21 in Examples by friebe

To easily create XML documents, the xml.Node class has been extended with a new withChild() method complementing addChild(). In contrast to the latter, withChild() returns the node the child was added to instead of the added child.


(more)

Unified runners in the web

at 2009-08-11 in ExamplesEditorialAnnouncements by kiesel

The XP framework had offered developers the power of an easy class loading setup via the new XP runners that are delivered with every release since several releases now. These runners have proven themselves very useful in day-to-day business, so we're working on porting them to the web!

With the so-called web-runners these new cool and useful features will become available for you:


  • Easier classpath setup
    Classpaths will be constructed from .pth files, in the same manner as xpcli does it.

  • No boilerplate "index.php"s any more
    you won't need the same index.php over and over in every project again; even more: you don't need any entrypoint .php any more even for SOAP- or JSON-endpoints.

  • Multiple etc/ configuration directories
    you can switch between multipe etc/, eg. one for the test server and one for production, by changing a single file

  • Overwrite arbitrary settings for special servers
    you can overwrite settings, eg. debugging settings, based on where your application runs - eg. on the development machine, you can have debugging enabled, while on production it's disabled. You don't need to have different files for them - no more mistakenly committed debug settings.

This article shows you how you can make use of the web runners in a XP application. Read on for more information!


(more)

Redirecting console output

at 2009-07-12 in Examples by friebe

To capture output written by any of the PHP functions the ouput control functions can be used (ob_start(), ob_get_contents() and friends). These functions do not affect the output made by the util.cmd.Console class as this writes directly to the standard out and error streams. To redirect these, you can use the following:

  // Save original stream in variable, change output stream to $stream
$out= Console::$out->getStream();
Console::
$out->setStream($stream);

// Write "Hello" and a newline, this will trigger $stream's write() method
Console::writeLine('Hello');

// Finally, restore original output stream
Console::$out->setStream($out);

This can be useful in unittests, when running command line applications inside a web container or inside GUIs, or whatever other usecase you can invent:-)



Subscribe

You can subscribe to the XP framework's news by using RSS syndication.


Categories

News
General
PHP5
Announcements
RFCs
Further reading
Examples
Editorial
EASC
Experiments
Unittests
Databases
5.8-SERIES
Unicode
Language
5.9-SERIES