/** * testAddItem * * @return void */ public function testAddItem() { $this->instance->addItem('http://windwalker.io'); $xml = <<<XML <?xml version="1.0" encoding="utf-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> \t<url> \t\t<loc>http://windwalker.io</loc> \t</url> </urlset> XML; $this->assertDomStringEqualsDomString($xml, $this->instance->toString()); $this->instance->addItem('http://windwalker.io/foo/bar/?flower=sakura&fly=bird', '1.0', ChangeFreq::DAILY, '2015-06-07 10:51:20'); $xml = <<<XML <?xml version="1.0" encoding="utf-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> \t<url> \t\t<loc>http://windwalker.io</loc> \t</url> \t<url> \t\t<loc>http://windwalker.io/foo/bar/?flower=sakura&fly=bird</loc> \t\t<changefreq>daily</changefreq> \t\t<priority>1.0</priority> \t\t<lastmod>2015-06-07T10:51:20+02:00</lastmod> \t</url> </urlset> XML; $this->assertDomStringEqualsDomString($xml, $this->instance->toString()); }
/** * onAfterWriteFiles * * @return void */ public function onAfterWriteFiles() { include_once __DIR__ . '/../../vendor/autoload.php'; $base = realpath(__DIR__ . '/../../..'); $items = Folder::files($base, true); $sitemap = new Sitemap(); $root = Ioc::getConfig()->get('site.root'); $sitemap->addItem($root, 1.0); foreach ($items as $item) { if (File::getExtension($item) != 'html') { continue; } $loc = str_replace('\\', '/', substr($item, strlen($base) + 1)); $sitemap->addItem($root . '/' . $loc, 0.8, ChangeFreq::WEEKLY, new \DateTime()); } $xml = $sitemap->toString(); file_put_contents($base . '/sitemap.xml', $xml); }