Type classes need to extend the abstract DataType class. Besides providing a means
of categorizing types, the DataType class provides the methods necessary to support
the "refid
" attribute. (All types can be given an id, and can be
referred to later using that id.)
In this example we are creating a DSN type because we have written a number of DB-related Tasks, each of which need to know how to connect to the database; instead of having database parameters for each task, we've created a DSN type so that we can identify the connection parameters once and then use it in all our db Tasks.
require_once "phing/types/DataType.php"; /** * This Type represents a DB Connection. */ class DSN extends DataType { private $url; private $username; private $password; private $persistent = false; /** * Sets the URL part: mysql://localhost/mydatabase */ public function setUrl($url) { $this->url = $url; } /** * Sets username to use in connection. */ public function setUsername($username) { $this->username = $username; } /** * Sets password to use in connection. */ public function setPassword($password) { $this->password = $password; } /** * Set whether to use persistent connection. * @param boolean $persist */ public function setPersistent($persist) { $this->persistent = (boolean) $persist; } public function getUrl(Project $p) { if ($this->isReference()) { return $this->getRef($p)->getUrl($p); } return $this->url; } public function getUsername(Project $p) { if ($this->isReference()) { return $this->getRef($p)->getUsername($p); } return $this->username; } public function getPassword(Project $p) { if ($this->isReference()) { return $this->getRef($p)->getPassword($p); } return $this->password; } public function getPersistent(Project $p) { if ($this->isReference()) { return $this->getRef($p)->getPersistent($p); } return $this->persistent; } /** * Gets a combined hash/array for DSN as used by PEAR. * @return array */ public function getPEARDSN(Project $p) { if ($this->isReference()) { return $this->getRef($p)->getPEARDSN($p); } include_once 'DB.php'; $dsninfo = DB::parseDSN($this->url); $dsninfo['username'] = $this->username; $dsninfo['password'] = $this->password; $dsninfo['persistent'] = $this->persistent; return $dsninfo; } /** * Your datatype must implement this function, which ensures that there * are no circular references and that the reference is of the correct * type (DSN in this example). * * @return DSN */ public function getRef(Project $p) { if ( !$this->checked ) { $stk = array(); array_push($stk, $this); $this->dieOnCircularReference($stk, $p); } $o = $this->ref->getReferencedObject($p); if ( !($o instanceof DSN) ) { throw new BuildException($this->ref->getRefId()." doesn't denote a DSN"); } else { return $o; } } }