Skip to content

Commit 66237a3

Browse files
author
Eugene Tupikov
committed
Refactoring
1 parent b56dbba commit 66237a3

18 files changed

+167
-111
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
"require": {
44
"react/react": "*",
55
"phroute/phroute": "*",
6-
"guzzlehttp/guzzle": "~6.0"
6+
"guzzlehttp/guzzle": "~6.0",
7+
"arvenil/ninja-mutex": "*"
78

89
},
910
"autoload": {
1011
"psr-4": {
11-
"unclead\\phpcluster\\": "src/"
12+
"PhpCluster\\": "src/"
1213
}
1314
}
1415
}

composer.lock

Lines changed: 64 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require __DIR__ . '/vendor/autoload.php';
44

5-
use unclead\phpcluster\Application;
5+
use PhpCluster\Application;
66

77
$app = new Application($argv);
88
$app->run();

src/Application.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace unclead\phpcluster;
3+
namespace PhpCluster;
44

55
use Exception;
66
use React\EventLoop\LoopInterface;
@@ -11,9 +11,7 @@
1111
use React\Http\Server as HttpServer;
1212
use Phroute\Phroute\RouteCollector;
1313
use Phroute\Phroute\Dispatcher;
14-
use unclead\phpcluster\collections\CmdCollection;
15-
use unclead\phpcluster\commands\InitCollectionCommand;
16-
use unclead\phpcluster\controllers\CmdController;
14+
use PhpCluster\Command\PopulateCollectionCommand;
1715
use Phroute\Phroute\Exception\HttpMethodNotAllowedException;
1816
use Phroute\Phroute\Exception\HttpRouteNotFoundException;
1917

@@ -91,9 +89,10 @@ private function initHttpServer()
9189

9290
private function afterInit()
9391
{
94-
$this->getLogger()->info('Sync collection with other instances in cluster');
95-
$command = new InitCollectionCommand($this->getCmdCollection(), $this->getConfig());
92+
$this->getLogger()->debug('Sync collection with other instances in cluster');
93+
$command = new PopulateCollectionCommand($this->getCmdCollection(), $this->getConfig());
9694
$command->execute();
95+
$this->getLogger()->debug('Current collection state: ' . $this->getCmdCollection());
9796
}
9897

9998
/**
@@ -110,14 +109,14 @@ private function initRouter()
110109
{
111110
$this->router = new RouteCollector();
112111
$this->router->get('/cmd{id:\d+}', function($id) {
113-
$controller = new CmdController($this);
112+
$controller = new Controller($this);
114113

115114
$content = $controller->actionIncrement($id);
116115
return $content;
117116
});
118117

119118
$this->router->get('/cmd', function() {
120-
$controller = new CmdController($this);
119+
$controller = new Controller($this);
121120
$this->responseContentType = 'application/json';
122121
$content = $controller->actionSummary();
123122
return $content;
@@ -126,7 +125,7 @@ private function initRouter()
126125
$this->router->post('/cmd{id:\d+}', function($id) {
127126
$this->request->on('data', function($data) use ($id){
128127
parse_str($data, $data);
129-
$controller = new CmdController($this);
128+
$controller = new Controller($this);
130129
$content = $controller->actionUpdate($id, $data);
131130
return $content;
132131
});

src/models/Cmd.php renamed to src/Cmd.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
namespace unclead\phpcluster\models;
3+
namespace PhpCluster;
44

55
/**
66
* Class Cmd
7-
* @package unclead\phpcluster\models
7+
* @package PhpCluster
88
*/
99
class Cmd {
1010

@@ -13,16 +13,31 @@ class Cmd {
1313
*/
1414
private $count = 0;
1515

16+
/**
17+
* Returns current value of counter.
18+
*
19+
* @return int
20+
*/
1621
public function getCount ()
1722
{
1823
return $this->count;
1924
}
2025

26+
/**
27+
* Set current value of counter.
28+
*
29+
* @param $v
30+
*/
2131
public function setCount($v)
2232
{
2333
$this->count = $v;
2434
}
2535

36+
/**
37+
* Performs increment operation and returns the result.
38+
*
39+
* @return int
40+
*/
2641
public function increment ()
2742
{
2843
return ++$this->count;

src/collections/CmdCollection.php renamed to src/CmdCollection.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
<?php
22

3-
namespace unclead\phpcluster\collections;
4-
5-
use unclead\phpcluster\models\Cmd;
3+
namespace PhpCluster;
64

75
/**
86
* Class CmdCollection
9-
* @package unclead\phpcluster\collections
107
*/
118
class CmdCollection
129
{
@@ -65,4 +62,13 @@ public function isEmpty()
6562
{
6663
return count($this->data) == 0;
6764
}
65+
66+
public function __toString()
67+
{
68+
$result = [];
69+
foreach ($this->all() as $id => $entity) {
70+
$result[$id] = $entity->getCount();
71+
}
72+
return json_encode($result);
73+
}
6874
}

src/Command/Command.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace PhpCluster\Command;
4+
5+
/**
6+
* Interface BaseCommand
7+
* @package PhpCluster\Command
8+
*/
9+
interface Command
10+
{
11+
public function execute();
12+
}

src/commands/IncrementCommand.php renamed to src/Command/IncrementCountCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

3-
namespace unclead\phpcluster\commands;
3+
namespace PhpCluster\Command;
44

5-
use unclead\phpcluster\models\Cmd;
5+
use PhpCluster\Cmd;
66

77
/**
88
* Class IncrementCommand
9-
* @package unclead\phpcluster\commands
9+
* @package PhpCluster\Command
1010
*/
11-
class IncrementCommand extends BaseCommand
11+
class IncrementCountCommand implements Command
1212
{
1313
/**
1414
* @var Cmd

src/commands/InitCollectionCommand.php renamed to src/Command/PopulateCollectionCommand.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<?php
22

3-
namespace unclead\phpcluster\commands;
3+
namespace PhpCluster\Command;
44

55
use GuzzleHttp\Client;
6-
use unclead\phpcluster\collections\CmdCollection;
7-
use unclead\phpcluster\Config;
8-
use unclead\phpcluster\models\Cmd;
6+
use PhpCluster\CmdCollection;
7+
use PhpCluster\Config;
8+
use PhpCluster\Cmd;
99

1010
/**
1111
* Class PopulateCollectionCommand
12-
* @package unclead\phpcluster\commands
12+
* @package PhpCluster\Command
1313
*/
14-
class InitCollectionCommand extends BaseCommand
14+
class PopulateCollectionCommand implements Command
1515
{
1616
/**
1717
* @var CmdCollection

src/commands/SyncCommand.php renamed to src/Command/SyncCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<?php
22

3-
namespace unclead\phpcluster\commands;
3+
namespace PhpCluster\Command;
44

55
use GuzzleHttp\Client;
66

77
/**
88
* Class SyncCommand
9-
* @package unclead\phpcluster\commands
9+
* @package PhpCluster\Command
1010
*/
11-
class SyncCommand extends BaseCommand
11+
class SyncCommand implements Command
1212
{
1313
/**
1414
* @var int

0 commit comments

Comments
 (0)