phing dsn-adder

raw

asd.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
require_once 'phing/Task.php';
 
class asd extends Task {
 
    private $asd;
    protected $dsn;
 
    public function init(){
 
    }
 
    public function main(){
        var_dump(get_class($this->dsn), $this->dsn->getUrl($this->project));
    }
 
    public function addDsn(DSN $obj){
        $this->dsn = $obj;
    }
}
?>
 
raw

build.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" ?>
<project name="test" basedir="." default="main">
  <taskdef name="asd" classname="asd" />
  <typedef name="dsn" classname="dbget.DSN" />
 
  <dsn
      id="maindsn"
      url="mysql://localhost/mydatabase"
      username="root"
      password=""
      persistent="false" />
 
  <target name="main">
    <asd>
      <dsn refid="maindsn"/>
    </asd>
  </target>
 
</project>
 
raw

dbget-DSN.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
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;
        }
    }
 
}
?>
 
raw

output.txt

$ phing
Buildfile: /home/cweiske/php/test/phing-dsn/build.xml

test > main:

string(3) "DSN"
string(28) "mysql://localhost/mydatabase"

BUILD FINISHED

Total time: 0.0672 seconds
Christian Weiske Christian Weiske
owner

History