Skip to content

Commit 1e87cdd

Browse files
committed
Merge branch 'dependency-injection'
2 parents b85ae9c + 76ce600 commit 1e87cdd

17 files changed

+142
-101
lines changed

README.md

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ General API Usage
2929
-----------------
3030

3131
```php
32-
$gitlab = new \Gitlab\Client('http://git.yourdomain.com/api/v3/'); // change here
33-
$gitlab->authenticate('your_gitlab_token_here', \Gitlab\Client::AUTH_URL_TOKEN); // change here
32+
$client = new \Gitlab\Client('http://git.yourdomain.com/api/v3/'); // change here
33+
$client->authenticate('your_gitlab_token_here', \Gitlab\Client::AUTH_URL_TOKEN); // change here
3434

35-
$project = $gitlab->api('projects')->create('My Project', array(
35+
$project = $client->api('projects')->create('My Project', array(
3636
'description' => 'This is a project'
3737
'issues_enabled' => false
3838
));
@@ -42,33 +42,29 @@ $project = $gitlab->api('projects')->create('My Project', array(
4242
Model Usage
4343
-----------
4444

45-
You can also use the library in an object oriented manner.
45+
You can also use the library in an object oriented manner. This is an experimental branch used for decided how to manage dependencies within model usage.
4646

4747
```php
48-
$gitlab = new \Gitlab\Client('http://git.yourdomain.com/api/v3/'); // change here
49-
$gitlab->authenticate('your_gitlab_token_here', \
50-
Gitlab\Client::AUTH_URL_TOKEN); // change here
51-
52-
// Give the API client instance to model classes
53-
\Gitlab\Model\AbstractModel::client($gitlab);
48+
$client = new \Gitlab\Client('http://git.yourdomain.com/api/v3/'); // change here
49+
$client->authenticate('your_gitlab_token_here', \Gitlab\Client::AUTH_URL_TOKEN); // change here
5450
```
5551

5652
Creating a new project
5753

5854
```php
59-
$project = \Gitlab\Model\Project::create('My Project', array(
55+
$project = \Gitlab\Model\Project::create($client, 'My Project', array(
6056
'description' => 'This is my project',
6157
'issues_enabled' => false
6258
));
6359

6460
$project->addHook('http://mydomain.com/hook/push/1');
65-
6661
```
6762

6863
Creating a new issue
6964

7065
```php
7166
$project = new \Gitlab\Model\Project(1);
67+
$project->setClient($client);
7268
$issue = $project->createIssue('This does not work..', array(
7369
'description' => 'This doesnt work properly. Please fix',
7470
'assignee_id' => 2

lib/Gitlab/Api/Projects.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
class Projects extends AbstractApi
66
{
7-
public function all($page = 1, $per_page = static::PER_PAGE)
7+
public function all($page = 1, $per_page = self::PER_PAGE)
88
{
99
return $this->get('projects', array(
1010
'page' => $page,
@@ -56,7 +56,7 @@ public function removeMember($project_id, $user_id)
5656
return $this->delete('projects/'.urlencode($project_id).'/members/'.urldecode($user_id));
5757
}
5858

59-
public function hooks($project_id, $page = 1, $per_page = static::PER_PAGE)
59+
public function hooks($project_id, $page = 1, $per_page = self::PER_PAGE)
6060
{
6161
return $this->get('projects/'.urlencode($project_id).'/hooks', array(
6262
'page' => $page,

lib/Gitlab/Api/Users.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public function all($active = null)
99
if (!is_null($active)) {
1010
return $this->get('users', array(
1111
'active' => $active
12-
))
12+
));
1313
}
1414

1515
return $this->get('users');

lib/Gitlab/Model/AbstractModel.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,25 @@
88
abstract class AbstractModel
99
{
1010
protected static $_properties;
11-
protected static $_client;
1211

13-
public static function client(Client $client = null)
14-
{
15-
if (null !== $client) {
16-
static::$_client = $client;
17-
}
12+
protected $_data = array();
13+
protected $_client = null;
1814

19-
return static::$_client;
15+
public function getClient()
16+
{
17+
return $this->_client;
2018
}
2119

22-
protected $_data = array();
23-
2420
public function setClient(Client $client)
2521
{
26-
return static::client($client);
22+
$this->_client = $client;
23+
24+
return $this;
2725
}
2826

2927
public function api($api)
3028
{
31-
return static::client()->api($api);
29+
return $this->getClient()->api($api);
3230
}
3331

3432
public function hydrate(array $data = array())

lib/Gitlab/Model/Branch.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Gitlab\Model;
44

5+
use Gitlab\Client;
6+
57
class Branch extends AbstractModel
68
{
79
protected static $_properties = array(
@@ -11,12 +13,13 @@ class Branch extends AbstractModel
1113
'protected'
1214
);
1315

14-
public static function fromArray(Project $project, array $data)
16+
public static function fromArray(Client $client, Project $project, array $data)
1517
{
1618
$branch = new Branch($project, $data['name']);
19+
$branch->setClient($client);
1720

1821
if (isset($data['commit'])) {
19-
$data['commit'] = Commit::fromArray($project, $data['commit']);
22+
$data['commit'] = Commit::fromArray($client, $project, $data['commit']);
2023
}
2124

2225
return $branch->hydrate($data);
@@ -32,21 +35,20 @@ public function show()
3235
{
3336
$data = $this->api('repositories')->branch($this->project->id, $this->name);
3437

35-
return Branch::fromArray($this->project, $data);
38+
return Branch::fromArray($this->getClient(), $this->project, $data);
3639
}
3740

3841
public function protect()
3942
{
4043
$data = $this->api('repositories')->protectBranch($this->project->id, $this->name);
4144

42-
return Branch::fromArray($this->project, $data);
45+
return Branch::fromArray($this->getClient(), $this->project, $data);
4346
}
4447

4548
public function unprotect()
4649
{
4750
$data = $this->api('repositories')->unprotectBranch($this->project->id, $this->name);
4851

49-
return Branch::fromArray($this->project, $data);
52+
return Branch::fromArray($this->getClient(), $this->project, $data);
5053
}
51-
5254
}

lib/Gitlab/Model/Commit.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Gitlab\Model;
44

5+
use Gitlab\Client;
6+
57
class Commit extends AbstractModel
68
{
79
protected static $_properties = array(
@@ -21,25 +23,26 @@ class Commit extends AbstractModel
2123
'project'
2224
);
2325

24-
public static function fromArray(Project $project, array $data)
26+
public static function fromArray(Client $client, Project $project, array $data)
2527
{
2628
$commit = new Commit($project, $data['id']);
29+
$commit->setClient($client);
2730

2831
if (isset($data['parents'])) {
2932
$parents = array();
3033
foreach ($data['parents'] as $parent) {
31-
$parents[] = Commit::fromArray($project, $parent);
34+
$parents[] = Commit::fromArray($client, $project, $parent);
3235
}
3336

3437
$data['parents'] = $parents;
3538
}
3639

3740
if (isset($data['author'])) {
38-
$data['author'] = User::fromArray($data['author']);
41+
$data['author'] = User::fromArray($client, $data['author']);
3942
}
4043

4144
if (isset($data['committer'])) {
42-
$data['committer'] = User::fromArray($data['committer']);
45+
$data['committer'] = User::fromArray($client, $data['committer']);
4346
}
4447

4548
return $commit->hydrate($data);

lib/Gitlab/Model/Group.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Gitlab\Model;
44

5+
use Gitlab\Client;
6+
57
class Group extends AbstractModel
68
{
79
protected static $_properties = array(
@@ -12,26 +14,27 @@ class Group extends AbstractModel
1214
'projects'
1315
);
1416

15-
public static function fromArray(array $data)
17+
public static function fromArray(Client $client, array $data)
1618
{
1719
$group = new Group($data['id']);
20+
$group->setClient($client);
1821

1922
if (isset($data['projects'])) {
2023
$projects = array();
2124
foreach ($data['projects'] as $project) {
22-
$projects[] = Project::fromArray($project);
25+
$projects[] = Project::fromArray($client, $project);
2326
}
2427
$data['projects'] = $projects;
2528
}
2629

2730
return $group->hydrate($data);
2831
}
2932

30-
public static function create($name, $path)
33+
public static function create(Client $client, $name, $path)
3134
{
32-
$data = static::client()->api('groups')->create($name, $path);
35+
$data = $client->api('groups')->create($name, $path);
3336

34-
return Group::fromArray($data);
37+
return Group::fromArray($client, $data);
3538
}
3639

3740
public function __construct($id)
@@ -43,6 +46,6 @@ public function show()
4346
{
4447
$data = $this->api('groups')->show($this->id);
4548

46-
return Group::fromArray($data);
49+
return Group::fromArray($this->getClient(), $data);
4750
}
4851
}

lib/Gitlab/Model/Hook.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Gitlab\Model;
44

5+
use Gitlab\Client;
6+
57
class Hook extends AbstractModel
68
{
79
protected static $_properties = array(
@@ -10,18 +12,19 @@ class Hook extends AbstractModel
1012
'created_at'
1113
);
1214

13-
public static function fromArray(array $data)
15+
public static function fromArray(Client $client, array $data)
1416
{
1517
$hook = new Hook($data['id']);
18+
$hook->setClient($client);
1619

1720
return $hook->hydrate($data);
1821
}
1922

20-
public static function create($url)
23+
public static function create(Client $client, $url)
2124
{
22-
$data = static::client()->api('system_hooks')->create($url);
25+
$data = $client->api('system_hooks')->create($url);
2326

24-
return Hook::fromArray($data);
27+
return Hook::fromArray($client, $data);
2528
}
2629

2730
public function __construct($id)

lib/Gitlab/Model/Issue.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Gitlab\Model;
44

5+
use Gitlab\Client;
6+
57
class Issue extends AbstractModel
68
{
79
protected static $_properties = array(
@@ -20,16 +22,17 @@ class Issue extends AbstractModel
2022
'state'
2123
);
2224

23-
public static function fromArray(Project $project, array $data)
25+
public static function fromArray(Client $client, Project $project, array $data)
2426
{
2527
$issue = new Issue($project, $data['id']);
28+
$issue->setClient($client);
2629

2730
if (isset($data['author'])) {
28-
$data['author'] = User::fromArray($data['author']);
31+
$data['author'] = User::fromArray($client, $data['author']);
2932
}
3033

3134
if (isset($data['assignee'])) {
32-
$data['assignee'] = User::fromArray($data['assignee']);
35+
$data['assignee'] = User::fromArray($client, $data['assignee']);
3336
}
3437

3538
return $issue->hydrate($data);
@@ -45,14 +48,14 @@ public function show()
4548
{
4649
$data = $this->api('issues')->show($this->project->id, $this->id);
4750

48-
return Issue::fromArray($this->project, $data);
51+
return Issue::fromArray($this->getClient(), $this->project, $data);
4952
}
5053

5154
public function update(array $params)
5255
{
5356
$data = $this->api('issues')->update($this->project->id, $this->id, $params);
5457

55-
return Issue::fromArray($this->project, $data);
58+
return Issue::fromArray($this->getClient(), $this->project, $data);
5659
}
5760

5861
public function close($comment = null)
@@ -79,7 +82,7 @@ public function addComment($body)
7982
'body' => $body
8083
));
8184

82-
return Note::fromArray($this, $data);
85+
return Note::fromArray($this->getClient(), $this, $data);
8386
}
8487

8588
}

lib/Gitlab/Model/Key.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Gitlab\Model;
44

5+
use Gitlab\Client;
6+
57
class Key extends AbstractModel
68
{
79
protected static $_properties = array(
@@ -11,9 +13,10 @@ class Key extends AbstractModel
1113
'created_at'
1214
);
1315

14-
public static function fromArray(array $data)
16+
public static function fromArray(Client $client, array $data)
1517
{
1618
$key = new Key();
19+
$key->setClient($client);
1720

1821
return $key->hydrate($data);
1922
}

0 commit comments

Comments
 (0)