Skip to content

Commit c1744d3

Browse files
author
zng
committed
使用命名空间,自动加载类
1 parent 844372a commit c1744d3

File tree

8 files changed

+39
-17
lines changed

8 files changed

+39
-17
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
2+
namespace Seefan;
23

3-
require __DIR__.'/Http.php';
4-
require __DIR__.'/service/service.php';
54
class ConsulClient
65
{
76
private $http;
@@ -27,9 +26,17 @@ public function __get($name)
2726
public function get($name)
2827
{
2928
if (empty($this->service[$name])) {
30-
require __DIR__ . '/service/' . $name . '.php';
31-
$this->service[$name] = new $name($this->base_url, $this->http);
29+
$class_name = "Seefan\\Service\\$name";
30+
$this->service[$name] = new $class_name($this->base_url, $this->http);
3231
}
3332
return $this->service[$name];
3433
}
35-
}
34+
35+
}
36+
spl_autoload_register(function ($class) {
37+
$file = $class . '.php';
38+
if (is_file($file)) {
39+
require($file);
40+
}
41+
}
42+
);

Http.php renamed to Seefan/Http.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
2+
namespace Seefan;
33
class Http
44
{
55
/**
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
2-
3-
class agent extends service
2+
namespace Seefan\Service;
3+
class Agent extends Service
44
{
55
public function __construct($base_url, $http)
66
{
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
2-
3-
class catalog extends service
2+
namespace Seefan\Service;
3+
class Catalog extends Service
44
{
55
public function __construct($base_url, $http)
66
{

service/kv.php renamed to Seefan/Service/KV.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
2-
3-
class kv extends service
2+
namespace Seefan\Service;
3+
class KV extends Service
44
{
55
public function __construct($base_url, $http)
66
{
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
2+
namespace Seefan\Service;
3+
use Seefan\Http;
24

3-
class service
5+
class Service
46
{
57
/**
68
* @var Http

test.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
* Date: 2017/4/1
66
* Time: 10:22
77
*/
8-
require 'ConsulClient.php';
9-
$cc = new ConsulClient(['host' => '10.20.28.51:8500']);
8+
require 'Seefan\ConsulClient.php';
9+
$cc = new Seefan\ConsulClient(['host' => '10.20.28.51:8500']);
1010
//var_dump( $cc->catalog->services());
1111
//var_dump($cc->kv->get('service/0000'));
1212
//var_dump($cc->agent->self());
@@ -25,7 +25,7 @@
2525
}';
2626
//$param = json_decode($str, true);
2727
//var_dump($cc->agent->service('register', $param));
28-
//$cc->agent->service('deregister','redis1');
28+
//$rsp=$cc->agent->service('deregister','redis1');
2929
//$rsp=$cc->catalog->service('XE');
3030

3131
$json_str = '{
@@ -59,6 +59,13 @@
5959
"ServiceID": "redis1"
6060
}
6161
}';
62+
//$json = json_decode($json_str, true);
63+
//$rsp = $cc->catalog->register($json);
64+
65+
$json_str='{
66+
"Datacenter": "dc1",
67+
"Node": "foobar"
68+
}';
6269
$json = json_decode($json_str, true);
63-
$rsp = $cc->catalog->register($json);
70+
$rsp = $cc->catalog->deregister($json);
6471
var_dump($rsp);

0 commit comments

Comments
 (0)