belyashdima-test-tasks/sitemap-lib-test

此测试包包含将网站地图保存为3种格式的工具:xml、json和csv

v1.0.0 2024-09-07 19:14 UTC

This package is auto-updated.

Last update: 2024-09-07 20:06:58 UTC


README

测试任务链接: https://docs.google.com/document/d/115MIjgY5AvGc1bmOYcTBOVYK8SZBS1LrSfvo1agsdWI/edit

程序启动

库连接

  1. 创建一个空项目,并添加文件 composer.json。

  2. 运行以下命令: composer run install

  3. 在项目中创建一个文件,例如命名为: StartPoint.php

  4. 在连接库之前,需要从 Packagist 站点获取它: composer require belyashdima-test-tasks/sitemap-lib-test

  5. 更新 composer: composer update

  6. 在文件 composer.json 中应该是这样的: composer.json

  7. <?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

CSV-файл

JSON

JSON-файл

XML

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