Examples

Script it!

at 2013-09-22 in Examples by friebe

Inspired by reading Scala's Getting started documentation, I started out to see whether it could also be possible to add a "Script it!" feature to the XP Framework, too.

Here's what we're talking about - a small shell preamble and the class' sourcecode in one file, which we'll call helloworld:

  #!/bin/sh
exec xp xp.runtime.Sh "$0" "$@"
[?php
class HelloWorld extends \lang\Object {
public
static function main($args) {
Console::writeLine
("Hello, world!");
}
}
After giving this file execute permissions, we can run it by simply invoking ./helloworld.

Here's the implementation. Not sure this will make it into the framework, but wanted to put it out into the wild nevertheless, maybe someone find this useful and creates a pull request:-)



Injection in REST handlers

at 2013-03-01 in Examples by friebe

The new REST API brings XPCLI-like @inject handling to REST handlers. Although this is not a full-featured IOC container, you can have the log categories, properties and the invocation context injected into your REST resource handler class.

To accomplish this, decorate methods with the afforementioned @inject annotation. Here's an example:

  /**
* Sets a logger category for debugging
*/
#[@inject(name = 'trace')]
public function setTrace(LogCategory $cat= NULL) {
$this->cat= $cat;
}

Some more details:
  • Properties can be injected via util.Properties and must supply a name
  • Log categories are retrieved via util.log.LogCategory and can supply a name - if omitted, the scriptlet's trace is used (this is previously set via debug="TRACE" inside web.ini).
  • The invocation context can be used for marshalling and exception mapping customizations. Its type is webservices.rest.srv.RestContext



New Package::getComment() method

at 2012-06-07 in Examples by friebe

With the implementation of the feature suggested in pull request #139, there's now a new method in the lang.reflect.Package class, public string getComment(). It returns the api doc comment of the package-info.xp file, if existant, NULL otherwise.

Example
To add package documentation (this is also picked up by the doclet API and te api doc generator!), add a file called package-info.xp alongside with the package directory. Here's an example (goes in net/xp_framework/tutorial/package-info.xp):

<?php
/* This file is part of the XP framework
*
* $Id$
*/


/**
* The XP tutorial package
*
* @see http://xp-framework.net/
*/
package net.xp_framework.tutorial
{
}
?>

Invoking Package::forName('net.xp_framework.tutorial')->getComment() will then return the string "The XP tutorial package".

One possible use could be XP Tools: Command line reflection + "-v" and "-vv".



PHP Namespaces and the XP Framework

at 2012-01-06 in RFCsPHP5Examples5.9-SERIES by friebe

With the implementation of RFC #0222, we have added optional PHP namespaces support to the XP Framework. Optional means the XP Framework itself will neither depend on PHP 5.3 (still supporting PHP 5.2.10 upward at least for the 5.9-SERIES) nor will it change any of its classes to use them. That doesn't mean you can't use them, though:-)

Here's a quick-start guide:

  • PHP namespaces use the backslash (\). These translate 1:1 to the package separator in the XP Framework, the dot (.).
  • Classes with PHP namespaces, fully-qualified and non-qualified XP classes may be mixed in one project. The XP group recommends migrating complete packages.
  • Inside classes using PHP namespaces, other XP classes need to be either addressed by their absolute fully-qualified names (e.g. \lang\Object) or imported via the use statement by their fully-qualified names; uses() may not be used there.
  • Inside classes not using PHP namespaces, namespaced classes must be added to the uses() list and addressed in their namespaced version.

As an example, if we have the following in de/thekid/tools/SQL.class.php:
  namespace de\thekid\tools;
use rdbms\DriverManager;

class SQL extends \lang\Object {
public
static function main(array $args) {
$conn= DriverManager::getConnection($args[0]);
// ...
}
}
To run this class, use xp de.thekid.tools.SQL ... as you would with a non-namespaced class.



Available database drivers

at 2011-01-16 in DatabasesExamples by friebe

Here's how to find out which database drivers are available (keys) - and what classes they're implemented by (values):

  Console::writeLine(DriverManager::getInstance()->drivers);
This will yield, depending on the PHP installation and configuration, something along the lines of:

  [
    mysql => lang.XPClass<rdbms.mysql.MySQLConnection>
    pgsql => lang.XPClass<rdbms.pgsql.PostgreSQLConnection>
    sqlite => lang.XPClass<rdbms.sqlite.SQLiteConnection>
  ]



ZIP files now support Unicode filenames

at 2011-01-16 in UnicodeExamples by friebe

The XP Framework's io.archive.zip package allows reading and creating ZIP files and is tested against zip archives created by Info-ZIP 3.0, PHP's Zip class, 7-zip, WinRAR and Windows' "compressed folders".

In SVN head, we have made a couple of adjustments to be able to support files written by Java's java.util.zip package (which is internally used by the jar command):

  • Reading the central directory to find the compressed and uncompressed lengths, Java doesn't write this into the local file header - making it impossible to stream those files
  • Supporting character set detection on extraction - up until JDK7 Build 57, the Java API was incorrectly writing entry names in UTF-8 but not setting the so-called "Language Encoding bit (EFS)".
  • Supporting to write archives with Unicode names - a JDK build downloaded yesterday still chokes on non-Unicode filenames inside ZIP archives when no charset is given (default is "CP437" here).
This way we can now create JAR files, for example.



The new lang.Closeable interface and ARM blocks

at 2010-12-31 in LanguageExamples5.8-SERIES by friebe

The new lang.Closeable interface and the io.streams classes being retrofitted to implement it may not seem very useful on the first glance, although they serve the purpose of supporting the so-called ARM blocks. These are a feature supported by XP Language and reuse the try keyword:

  try ($expression[, $expression[, ...]]) {
// Statements
}

Any of the expressions are expected to be instanceof lang.Closeable and their close() methods are guaranteed to be called in the declaration order regardless of whether the block raises an exception or not.


(more)

Hudson and the xml.meta API

at 2010-12-30 in Examples by friebe

Hudson LogoI'm currently working on a Hudson remoting API to be able to remotely configure our continuous integration projects. An example usecase occurred today: A co-worker had created a Hudson plugin which helps us distinguish projects with no tests at all from projects with tests exist: It display a different icon in the "weather" column and sets the build status to unstable. Now what we want to do is to enable this plugins in all projects, which would mean clicking through an odyssee of "Select Project - Configure - Enable - Save" more than 70 times in the frontend.

Hudson contains a Remote API which should be able to tackle this job.


(more)

apt-get meets the XP-Framework

at 2010-12-16 in ExamplesEditorial by friebe

Debian PackageThe XP Framework has an easy-to-use installation mechanism - simply downloading a setup script and piping it directly to PHP. What this mechanism cannot do though is to install PHP as a dependency itself, which is usually done in an operating-system dependant manner. On Debian and Ubuntu distributions, the packaging mechanism is called APT (Advanced Packaging Tool), which installs Debian packages (.deb files). In order to build such a package, we need to create a control file as well as the intended directory structure and wrap all that up using the "dpkg" tool (there's a howto over at IBM's developerworks). Unfortunately, this approach requires the Debian packaging tools to be available for the build platform - a situation we cannot rely on. This article shows a solution written in the XP Framework.


(more)

XP: Log SQL queries

at 2010-10-18 in ExamplesDatabases by friebe

Reading PHP: 62 characters to see all MySQL queries, I thought I'd post a similar use case solution here. The task is to log all SQL queries and a backtrace of where they're executed from. In Ulf's post, this is accomplished by an extension function written in C, which needs to be compiled and loaded into PHP.

Altough a nice feature, this way has several downsides, first of all, the compilation, a hurdle big enough to keep me from trying it out, and second, it's only going to work with MySQL databases (and to be precise, only those newer than 4.0, because mysqlnd doesn't support MySQL's old password protocol handshake).


(more)

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
6.0-SERIES