/
BaseX.php
170 lines (145 loc) · 3.29 KB
/
BaseX.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace BaseXMS;
use BaseXClient\Session;
use BaseXMS\Stdlib\DOMDocument;
use BaseXMS\Accumulator;
use BaseXMS\Stdlib\SimpleXMLElement as SimpleXMLElement;
use BaseXMS\Mvc\BaseXMSService;
/**
* @author pek
*
* Wrapper class around the basex provided lib. It's not a singleton because I hope
* that I can add an instance to a service manager and request that instance from there
* if needed.
*
* It should handle the connection details, accumulator, error handling
*
*/
class BaseX extends BaseXMSService
{
private $session;
/**
* @var array
*/
private $connectionDetails = array(
'host' => 'localhost',
'port' => 1984,
'user' => 'admin',
'pass' => 'admin',
'db' => 'base'
);
/**
* @throws Exception
* @return boolean
*/
public function getSession( $openDb = true )
{
if( !$this->session )
{
// merge config with default config
$config = $this->serviceManager->get( 'config' );
if( !empty( $config[ 'BaseX' ] ) )
{
$this->connectionDetails = array_merge( $this->connectionDetails, $config[ 'BaseX' ] );
}
try
{
$this->session = new Session( $this->connectionDetails[ 'host' ],
$this->connectionDetails[ 'port' ],
$this->connectionDetails[ 'user' ],
$this->connectionDetails[ 'pass' ]
);
// select a DB
if( $openDb )
{
$this->openDb( $this->connectionDetails[ 'db' ] );
}
}
catch( \Exception $e )
{
throw $e;
}
}
return $this->session;
}
/**
* @param string $name
*/
public function openDb( $name )
{
if( $name )
{
return $this->session->execute( 'OPEN ' . $name );
}
}
/**
* Return formats are: text, xml, simplexml
*
* @param string $query
* @param string $returnFormat
* @param string $command
* @param boolean $openDb
* @throws Exception
* @return Ambigous <string, unknown_type>
*/
public function execute(
$query,
$returnFormat = 'text',
$command = 'XQUERY',
$openDb = true
)
{
// build query
$query = $command . ' ' . $query;
$this->serviceManager->get( 'log' )->debug( $query );
if( $this->serviceManager->has( 'accumulator' ) ) $this->serviceManager->get( 'accumulator' )->start( 'baseX' );
try
{
$resultText = $this->getSession( $openDb )->execute( $query );
}
catch( \Exception $e )
{
$this->serviceManager->get( 'log' )->err( $e->getMessage() );
throw $e;
}
if( $this->serviceManager->has( 'accumulator' ) ) $this->serviceManager->get( 'accumulator' )->stop( 'baseX' );
return $this->transformToFormat( $resultText, $returnFormat );
}
/**
* @param unknown_type $resultText
* @param unknown_type $returnFormat
* @return string
*/
public function transformToFormat( $resultText, $returnFormat = 'text' )
{
$return = '';
if( $resultText )
{
switch( $returnFormat )
{
case 'xml':
{
$return = new DOMDocument();
$return->loadXML( $resultText );
}
break;
case 'simplexml':
{
$return = new SimpleXMLElement( $resultText );
}
break;
case 'json':
{
}
break;
// case text
default:
{
$return = $resultText;
}
}
}
return $return;
}
}
?>