1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the DpnXmlSitemapBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Björn Fromme <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the Resources/meta/LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Dpn\XmlSitemapBundle\Command; |
13
|
|
|
|
14
|
|
|
use Dpn\XmlSitemapBundle\Sitemap\SitemapManager; |
15
|
|
|
use Symfony\Component\Console\Command\Command; |
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
use Symfony\Component\Routing\RouterInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Kevin Bond <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class DumpCommand extends Command |
26
|
|
|
{ |
27
|
|
|
private $manager; |
28
|
|
|
private $router; |
29
|
|
|
private $kernelRootDir; |
30
|
|
|
|
31
|
4 |
|
public function __construct(SitemapManager $manager, RouterInterface $router, $kernelRootDir) |
32
|
|
|
{ |
33
|
4 |
|
$this->manager = $manager; |
34
|
4 |
|
$this->router = $router; |
35
|
4 |
|
$this->kernelRootDir = $kernelRootDir; |
36
|
|
|
|
37
|
4 |
|
parent::__construct(); |
38
|
4 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
4 |
|
protected function configure() |
44
|
|
|
{ |
45
|
4 |
|
$this |
46
|
4 |
|
->setName('dpn:xml-sitemap:dump') |
47
|
4 |
|
->setDescription('Dumps your sitemap(s) to the filesystem (defaults to web/)') |
48
|
4 |
|
->addArgument('host', InputArgument::REQUIRED, 'The full hostname for your website (ie http://www.google.com).') |
49
|
4 |
|
->addOption('target', null, InputOption::VALUE_REQUIRED, 'Override the target directory to dump sitemap(s) in.') |
50
|
|
|
; |
51
|
4 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
4 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
57
|
|
|
{ |
58
|
4 |
|
$host = $input->getArgument('host'); |
59
|
4 |
|
$target = rtrim($input->getOption('target') ?: sprintf('%s/../web', $this->kernelRootDir), '/'); |
60
|
|
|
|
61
|
4 |
|
if (!is_dir($target)) { |
62
|
1 |
|
throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $target)); |
63
|
|
|
} |
64
|
|
|
|
65
|
3 |
|
if (1 !== preg_match('#^(https?)://([\w\.-]+)#', $host, $matches)) { |
66
|
1 |
|
throw new \InvalidArgumentException(sprintf('The host "%s" is invalid.', $host)); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
$scheme = $matches[1]; |
70
|
2 |
|
$host = $matches[2]; |
71
|
2 |
|
$context = $this->router->getContext(); |
72
|
2 |
|
$context->setScheme($scheme); |
73
|
2 |
|
$context->setHost($matches[2]); |
74
|
2 |
|
$totalSitemaps = $this->manager->getNumberOfSitemaps(); |
75
|
|
|
|
76
|
2 |
|
$this->outputFile( |
77
|
2 |
|
$output, |
78
|
2 |
|
sprintf('%s/sitemap_index.xml', $target), |
79
|
2 |
|
$this->manager->renderSitemapIndex(sprintf('%s://%s', $scheme, $host)) |
80
|
2 |
|
); |
81
|
|
|
|
82
|
2 |
|
if (1 === $totalSitemaps) { |
83
|
1 |
|
$this->outputFile( |
84
|
1 |
|
$output, |
85
|
1 |
|
sprintf('%s/sitemap.xml', $target), |
86
|
1 |
|
$this->manager->renderSitemap() |
87
|
1 |
|
); |
88
|
|
|
|
89
|
1 |
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
for ($i = 1; $i <= $totalSitemaps; $i++) { |
93
|
1 |
|
$this->outputFile( |
94
|
1 |
|
$output, |
95
|
1 |
|
sprintf('%s/sitemap%d.xml', $target, $i), |
96
|
1 |
|
$this->manager->renderSitemap($i) |
97
|
1 |
|
); |
98
|
1 |
|
} |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param OutputInterface $output |
103
|
|
|
* @param string $filename |
104
|
|
|
* @param string $content |
105
|
|
|
*/ |
106
|
2 |
|
private function outputFile(OutputInterface $output, $filename, $content) |
107
|
|
|
{ |
108
|
2 |
|
if (false === @file_put_contents($filename, $content)) { |
109
|
|
|
throw new \RuntimeException(sprintf('Unable to write file "%s"', $filename)); |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
$output->writeln('<info>[file+]</info> '.$filename); |
113
|
2 |
|
} |
114
|
|
|
} |
115
|
|
|
|