-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClient.php
59 lines (50 loc) · 1.75 KB
/
Client.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
<?php
namespace resource;
class Client
{
protected $process;
public function run()
{
$router = new Router();
$task = ucfirst($router->getTask());
$action = $router->getAction();
$this->process = $router->getProcess();
$namespace = $this->process == 'work' ? 'worker' : 'crawler';
$class = $namespace.'\\'.$task;
$class = new $class();
try{
if(method_exists($class, $action)){
if ($this->process == 'crawl') {
$dir_name = strpos($task, '_') ? end(explode('_', $task)) : lcfirst($task);
$cache_dir = CACHE_PATH.$dir_name.'/';
$cache = $cache_dir.$action.'/';
if(! file_exists( $cache_dir)) mkdir($cache_dir);
if(! file_exists( $cache )) mkdir($cache);
if(method_exists($class, 'setCache')) $class->setCache($cache);
}
if (method_exists($class, 'init')) $class->init();
if (method_exists($class, 'beforeRun')) $class->beforeRun();
if(isset($router->body['loop'])){
$loop = 0;
while ($loop < intval($router->body['loop'])){
$class->$action();
$loop ++;
}
}else{
$class->$action();
}
if (method_exists($class, 'afterRun')) $class->afterRun();
}else{
exit('action is not found in \''.$task.'\'.');
}
}catch (\Exception $e){
echo $e->getMessage().EOL;
}
}
public function crawlHandler()
{
}
public function workHandler()
{
}
}