belyashdima-test-tasks / sitemap-lib-test
此测试包包含将网站地图保存为3种格式的工具:xml、json和csv
v1.0.0
2024-09-07 19:14 UTC
Requires
- php: >=8.2
- ext-xmlwriter: *
README
测试任务链接: https://docs.google.com/document/d/115MIjgY5AvGc1bmOYcTBOVYK8SZBS1LrSfvo1agsdWI/edit
程序启动
库连接
-
创建一个空项目,并添加文件 composer.json。
-
运行以下命令:
composer run install
-
在项目中创建一个文件,例如命名为:
StartPoint.php
-
在连接库之前,需要从 Packagist 站点获取它:
composer require belyashdima-test-tasks/sitemap-lib-test
-
更新 composer:
composer update
-
在
<?php
标签之后添加以下行
require_once __DIR__ . '/vendor/autoload.php'; use classes\BaseFormatter;
输入数据示例
$urls = [ [ "loc" => "https://site.ru/", "lastmod" =>"2020-12-14", "priority" =>1, "changefreq"=>"hourly" ], [ "loc" => "https://site.ru/news", "lastmod"=>"2020-12-10", "priority"=>0.5, "changefreq"=>"daily" ], [ "loc" => "https://site.ru/about", "lastmod"=>"2020-12-07", "priority"=>0.1, "changefreq"=>"weekly" ], [ "loc" => "https://site.ru/products", "lastmod"=>"2020-12-12", "priority"=>0.5, "changefreq"=>"daily" ], [ "loc" => "https://site.ru/products/ps5", "lastmod"=>"2020-12-11", "priority"=>0.1, "changefreq"=>"weekly" ], [ "loc" => "https://site.ru/products/xbox", "lastmod"=>"2020-12-12", "priority"=>0.1, "changefreq"=>"weekly" ], [ "loc" => "https://site.ru/products/wii", "lastmod"=>"2020-12-11", "priority"=>0.1, "changefreq"=>"weekly" ] ];
根据不同文件格式初始化对象的示例
// Путь для примера, можно указать любой другой $path = getenv('HOME').'/uploads/sitemap2.json'; $jsonFile = new BaseFormatter($urls,'json',$path); $path2 = getenv('HOME').'/uploads/sitemap2.сsv'; $csvFile = new BaseFormatter($urls,'csv',$path2); $path3 = getenv('HOME').'/uploads/sitemap2.xml'; $xmlFile = new BaseFormatter($urls,'xml',$path3);
StartPoint.php 脚本的最终代码
<?php require_once __DIR__ . '/vendor/autoload.php'; use classes\BaseFormatter; $urls = [ [ "loc" => "https://site.ru/", "lastmod" =>"2020-12-14", "priority" =>1, "changefreq"=>"hourly" ], [ "loc" => "https://site.ru/news", "lastmod"=>"2020-12-10", "priority"=>0.5, "changefreq"=>"daily" ], [ "loc" => "https://site.ru/about", "lastmod"=>"2020-12-07", "priority"=>0.1, "changefreq"=>"weekly" ], [ "loc" => "https://site.ru/products", "lastmod"=>"2020-12-12", "priority"=>0.5, "changefreq"=>"daily" ], [ "loc" => "https://site.ru/products/ps5", "lastmod"=>"2020-12-11", "priority"=>0.1, "changefreq"=>"weekly" ], [ "loc" => "https://site.ru/products/xbox", "lastmod"=>"2020-12-12", "priority"=>0.1, "changefreq"=>"weekly" ], [ "loc" => "https://site.ru/products/wii", "lastmod"=>"2020-12-11", "priority"=>0.1, "changefreq"=>"weekly" ] ]; // Путь для примера, можно указать любой другой $path = getenv('HOME').'/uploads/sitemap3.json'; $jsonFile = new BaseFormatter($urls,'json',$path); $path2 = getenv('HOME').'/uploads/sitemap3.сsv'; $csvFile = new BaseFormatter($urls,'csv',$path2); $path3 = getenv('HOME').'/uploads/sitemap3.xml'; $xmlFile = new BaseFormatter($urls,'xml',$path3); ?>
生成的文件示例
CSV
JSON
XML
异常示例
$path4 = getenv('HOME').'/uploads/sitemap3.txt'; $txtFile = new BaseFormatter($urls,'txt',$path3);
/bin/php /home/belyashdima/PhpstormProjects/untitled2/StartPoint.php
PHP Fatal error: Uncaught Exception: Неподдерживаемый формат файла in /home/belyashdima/PhpstormProjects/untitled2/vendor/belyashdima-test-tasks/sitemap-lib-test/classes/BaseFormatter.php:44
Stack trace:
#0 /home/belyashdima/PhpstormProjects/untitled2/vendor/belyashdima-test-tasks/sitemap-lib-test/classes/BaseFormatter.php(28): classes\BaseFormatter->setFormatter()
#1 /home/belyashdima/PhpstormProjects/untitled2/StartPoint.php(61): classes\BaseFormatter->__construct()
#2 {main}
thrown in /home/belyashdima/PhpstormProjects/untitled2/vendor/belyashdima-test-tasks/sitemap-lib-test/classes/BaseFormatter.php on line 44
Process finished with exit code 255