|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Slince/China package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Slince <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace China\Command; |
|
12
|
|
|
|
|
13
|
|
|
use China\Common\FilesystemAwareInterface; |
|
14
|
|
|
use China\Common\FilesystemAwareTrait; |
|
15
|
|
|
use Goutte\Client; |
|
16
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
17
|
|
|
use Symfony\Component\Console\Command\Command; |
|
18
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
19
|
|
|
|
|
20
|
|
|
class CrawlCommand extends Command implements CrawlerAwareInterface, FilesystemAwareInterface |
|
21
|
|
|
{ |
|
22
|
|
|
use FilesystemAwareTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Client |
|
26
|
|
|
*/ |
|
27
|
|
|
protected static $client; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* 资源目录. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
const RESOURCE_DIR = __DIR__.'/../../../resources/'; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct(Filesystem $filesystem) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->filesystem = $filesystem; |
|
39
|
|
|
parent::__construct(null); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getClient() |
|
46
|
|
|
{ |
|
47
|
|
|
if (static::$client) { |
|
48
|
|
|
return static::$client; |
|
49
|
|
|
} |
|
50
|
|
|
static::$client = new Client(); |
|
51
|
|
|
$guzzleClient = new GuzzleClient([ |
|
52
|
|
|
'timeout' => 5, |
|
53
|
|
|
'verify' => false, |
|
54
|
|
|
]); |
|
55
|
|
|
static::$client->setClient($guzzleClient); |
|
56
|
|
|
|
|
57
|
|
|
return static::$client; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* 去除空白字符. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $string |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function clearBlankCharacters($string) |
|
68
|
|
|
{ |
|
69
|
|
|
$handledString = preg_replace('/\s/', '', str_replace(' ', '', $string)); |
|
70
|
|
|
$handledString = str_replace(' ', '', $handledString); |
|
71
|
|
|
|
|
72
|
|
|
return trim($handledString); |
|
73
|
|
|
} |
|
74
|
|
|
} |