Skip to content

Commit

Permalink
Rewokr to support Di with a ConnectionSettings object & interface
Browse files Browse the repository at this point in the history
  • Loading branch information
twalder-docnet committed Jun 19, 2015
1 parent 42f26e7 commit ae9d93f
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 48 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ You're gonna need to connect to a DB before you can do anything else...

```php
<?php
$db = new \Docnet\DB('127.0.0.1', 'root', 'password', 'dbname');
$settings = new \Docnet\DB\ConnectionSettings('127.0.0.1', 'root', 'password', 'dbname');
$db = new \Docnet\DB($settings);
```

For the following examples, we'll assume there's an active DB object.
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"autoload": {
"classmap": [
"src/"
]
],
"psr-4": {"Docnet\\": "src/Docnet/"}
},
"include-path": ["src/"]
}
9 changes: 4 additions & 5 deletions examples/db.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2014 Docnet
* Copyright 2015 Docnet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,11 +15,9 @@
* limitations under the License.
*/

require('../src/Docnet/DB.php');
require('../src/Docnet/DB/Statement.php');
require('../src/Docnet/DB/Model.php');
require('../vendor/autoload.php');

$obj_db = new \Docnet\DB('127.0.0.1', 'root', 'letmein', 'dbCluster');
$obj_db = new \Docnet\DB(new \Docnet\DB\ConnectionSettings('127.0.0.1', 'root', 'letmein', 'dbCluster'));
$arr_objects = array();

echo round(memory_get_usage()/1024/1024, 2) . "MB\n";
Expand All @@ -28,6 +26,7 @@
* Simple - no parameters
*/
print_r($obj_db->fetchOne("SELECT * FROM tblServer"));

/**
* Statement/bind Type 1 - indexed parameters
*/
Expand Down
7 changes: 2 additions & 5 deletions examples/perf.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,9 @@
* limitations under the License.
*/

require('../src/Docnet/DB.php');
require('../src/Docnet/DB/Statement.php');
require('../src/Docnet/DB/Model.php');

$obj_db = new \Docnet\DB('127.0.0.1', 'root', 'letmein', 'dbCluster');
require('../vendor/autoload.php');

$obj_db = new \Docnet\DB(new \Docnet\DB\ConnectionSettings('127.0.0.1', 'root', 'letmein', 'dbCluster'));
$pad = 40;

$_SERVER['db'] = $obj_db;
Expand Down
12 changes: 8 additions & 4 deletions src/Docnet/DB.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright 2014 Docnet
* Copyright 2015 Docnet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,7 @@
*/

namespace Docnet;
use Docnet\DB\ConnectionSettingsInterface;

/**
* DB stuff
Expand All @@ -37,18 +38,21 @@ class DB
private $obj_db = NULL;

/**
* Are we in a transaction?
*
* @var bool
*/
private $bol_in_transaction = false;

/**
* Connect on construction
* Connection settings required on construction
*
* @param DB\ConnectionSettingsInterface $obj_settings
* @throws \Exception
*/
public function __construct($str_host, $str_user, $str_pass, $str_db = NULL, $int_port = NULL, $int_socket = NULL)
public function __construct(ConnectionSettingsInterface $obj_settings)
{
$this->obj_db = new \mysqli($str_host, $str_user, $str_pass, $str_db, $int_port, $int_socket);
$this->obj_db = new \mysqli($obj_settings->getHost(), $obj_settings->getUser(), $obj_settings->getPass(), $obj_settings->getDbName(), $obj_settings->getPort(), $obj_settings->getSocket());
if ($this->obj_db->connect_error) {
throw new \Exception('Connect Error (' . $this->obj_db->connect_errno . ') ' . $this->obj_db->connect_error);
}
Expand Down
223 changes: 223 additions & 0 deletions src/Docnet/DB/ConnectionSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
<?php
/**
* Copyright 2015 Docnet
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Docnet\DB;

/**
* Represents details needed to connect to a MySQL database
*
* Note to self: This ~200 line class basically represents an array ;-(
*
* @author Tom Walder <[email protected]>
*/
class ConnectionSettings implements ConnectionSettingsInterface {

/**
* DB host
*
* @var string|null
*/
private $str_host = NULL;

/**
* DB user
*
* @var string|null
*/
private $str_user = NULL;

/**
* DB pass
*
* @var string|null
*/
private $str_pass = NULL;

/**
* DB name
*
* @var string|null
*/
private $str_db = NULL;

/**
* DB port
*
* @var string|null
*/
private $str_port = NULL;

/**
* DB socket
*
* @var string|null
*/
private $str_socket = NULL;

/**
* Optionally Configure on construction
*
* @param string|null $str_host
* @param string|null $str_user
* @param string|null $str_pass
* @param string|null $str_db
* @param string|null $str_port
* @param string|null $str_socket
*/
public function __construct($str_host = NULL, $str_user = NULL, $str_pass = NULL, $str_db = NULL, $str_port = NULL, $str_socket = NULL)
{
$this->str_host = $str_host;
$this->str_user = $str_user;
$this->str_pass = $str_pass;
$this->str_db = $str_db;
$this->str_port = $str_port;
$this->str_socket = $str_socket;
}

/**
* Get the DB host
*
* @return string|null
*/
public function getHost()
{
return $this->str_host;
}

/**
* Get the DB user
*
* @return string|null
*/
public function getUser()
{
return $this->str_user;
}

/**
* Get the password
*
* @return string|null
*/
public function getPass()
{
return $this->str_pass;
}

/**
* Get the database name
*
* @return string|null
*/
public function getDbName()
{
return $this->str_db;
}

/**
* Get the server port
*
* @return string|null
*/
public function getPort()
{
return $this->str_port;
}

/**
* Get the server socket
*
* @return string|null
*/
public function getSocket()
{
return $this->str_socket;
}

/**
* Set the DB host
*
* @param $str_host
* @return self
*/
public function setHost($str_host)
{
$this->str_host = $str_host;
return $this;
}

/**
* Set the DB user
*
* @param $str_user
* @return self
*/
public function setUser($str_user)
{
$this->str_user = $str_user;
return $this;
}

/**
* Set the DB pass
*
* @param $str_pass
* @return self
*/
public function setPass($str_pass)
{
$this->str_pass = $str_pass;
return $this;
}

/**
* Set the DB name
*
* @param $str_db
* @return self
*/
public function setDbName($str_db)
{
$this->str_db = $str_db;
return $this;
}

/**
* Set the DB port
*
* @param $str_port
* @return self
*/
public function setPort($str_port)
{
$this->str_port = $str_port;
return $this;
}

/**
* Set the DB socket
*
* @param $str_socket
* @return self
*/
public function setSocket($str_socket)
{
$this->str_socket = $str_socket;
return $this;
}

}
Loading

0 comments on commit ae9d93f

Please sign in to comment.