yohns/legal-pages 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yohns/legal-pages

Composer 安装命令:

composer require yohns/legal-pages

包简介

Generate customizable legal pages (Privacy Policy, Terms of Service, etc.) from Markdown templates with placeholder substitution — outputs Markdown or Bootstrap 5.3.8 HTML

README 文档

README

Generate customizable legal pages (Privacy Policy, Terms of Service, etc.) from Markdown templates with placeholder substitution — outputs Markdown or Bootstrap 5.3.8 HTML.

Getting Started

1. Install via Composer:

composer require yohns/legal-pages

2. Publish starter files:

php vendor/bin/legal-pages publish

This copies a config file, a quickstart script, and an optional web UI into a legal-pages/ directory in your project root.

3. Edit your config and generate:

# Edit legal-pages/config/legal-pages.php with your company details, then:
php legal-pages/generate.php

Output files (Markdown + Bootstrap HTML) are saved to legal-pages/generated/.

Publish Options

# Publish everything (config + quickstart + web UI)
php vendor/bin/legal-pages publish

# Publish only specific parts
php vendor/bin/legal-pages publish --config
php vendor/bin/legal-pages publish --quickstart
php vendor/bin/legal-pages publish --ui

# Custom output directory
php vendor/bin/legal-pages publish --dir=my-legal/

Programmatic Usage

<?php
require 'vendor/autoload.php';

use Yohns\Gens\Legal\LegalPageGenerator;

$gen = new LegalPageGenerator('privacy-policy', 'ecommerce', [
    'company' => ['name' => 'Acme Inc', 'email' => 'legal@acme.com'],
    'website' => ['url' => 'https://acme.com', 'name' => 'Acme Store'],
]);

// Get raw Markdown
$markdown = $gen->generate();

// Get Bootstrap 5.3.8 HTML fragment (for embedding in your page)
$fragment = $gen->convertToHtml();

// Get full standalone HTML page with Bootstrap CDN
$fullPage = $gen->convertToHtml(full: true);

// Save to file
$gen->savePage($fullPage, 'privacy-policy.html', __DIR__ . '/generated');
$gen->savePage($markdown, 'privacy-policy.md', __DIR__ . '/generated');

Output Formats

Markdown (generate())

Returns raw Markdown with all placeholders replaced and conditional sections processed.

$markdown = $gen->generate();
file_put_contents('privacy-policy.md', $markdown);

Bootstrap HTML Fragment (convertToHtml())

Returns HTML with Bootstrap 5.3.8 CSS classes on elements. No <html>, <head>, or <body> tags — just the content ready to embed in your existing page.

$html = $gen->convertToHtml();
// Output example:
// <h1 class="mb-3">Privacy Policy</h1>
// <p class="mb-2">Welcome to Acme Inc...</p>

Bootstrap classes applied:

Element Classes
<h1> - <h6> mb-3
<p> mb-2
<table> table table-striped
<ul>, <ol> ps-3
<blockquote> blockquote ps-3 border-start border-4
<a> link-primary
<hr> my-4

Full HTML Page (convertToHtml(full: true))

Returns a complete HTML document with the Bootstrap 5.3.8 CDN stylesheet included. Ready to save and open in a browser.

$fullPage = $gen->convertToHtml(full: true);
file_put_contents('privacy-policy.html', $fullPage);

The page title is derived from the page type (e.g., privacy-policy becomes Privacy Policy).

Saving Files (savePage())

Save generated content to any directory. The directory is created automatically if it doesn't exist.

$gen->savePage($content, 'filename.html', '/path/to/output/');

Parameters:

  • $content — The generated content (markdown or HTML)
  • $filename — Output filename (sanitized with basename())
  • $outputDir — Directory to save to (created if missing)

Available Templates

Base Templates (all website types)

Page Type Template File
privacy-policy legal/base/privacy-policy.md
terms-of-service legal/base/terms-of-service.md
cookie-policy legal/base/cookie-policy.md
dmca-policy legal/base/dmca-policy.md
accessibility-statement legal/base/accessibility-statement.md

Website-Specific Templates

Website Type Template
personal legal/personal/blog-disclaimer.md
ecommerce legal/ecommerce/refund-policy.md
ecommerce legal/ecommerce/shipping-policy.md
social legal/social/content-policy.md

Template resolution order:

  1. legal/{websiteType}/{pageType}.md (website-specific)
  2. legal/base/{pageType}.md (fallback)

List Available Templates

$gen = new LegalPageGenerator();
$templates = $gen->getAvailableTemplates();
// ['Accessibility Statement', 'Blog Disclaimer', 'Cookie Policy', ...]

Website Types

Pass the website type as the second constructor parameter:

// Personal blog
$gen = new LegalPageGenerator('privacy-policy', 'personal', $config);

// Online store
$gen = new LegalPageGenerator('privacy-policy', 'ecommerce', $config);

// Social network
$gen = new LegalPageGenerator('privacy-policy', 'social', $config);

The website type controls:

  • Which templates are available (e.g., refund-policy only for ecommerce)
  • Which conditional sections appear ({{if:ecommerce}}...{{endif}})

Placeholders

Templates use {{category:field}} syntax. Pass values via the constructor or setter methods.

Via Constructor (nested array)

$gen = new LegalPageGenerator('privacy-policy', 'ecommerce', [
    'company' => [
        'name'  => 'Acme Inc',
        'email' => 'legal@acme.com',
        'phone' => '+1-555-0100',
    ],
    'website' => [
        'url'  => 'https://acme.com',
        'name' => 'Acme Store',
    ],
    'data' => [
        'collected' => 'name, email, payment information',
        'retention' => '24 months',
    ],
]);

Via Constructor (flat format)

$gen = new LegalPageGenerator('privacy-policy', 'ecommerce', [
    'company:name'  => 'Acme Inc',
    'company:email' => 'legal@acme.com',
    'website:url'   => 'https://acme.com',
]);

Via Setter Methods

$gen->setPlaceholder('company', 'name', 'Acme Inc');
// or
$gen->setPlaceholders([
    'company:name'  => 'Acme Inc',
    'company_name'  => 'Acme Inc',  // underscore format also works
]);

Auto-Populated Placeholders

When yohns/config is configured with config/config.php, these placeholders are filled automatically:

Placeholder Config Key
company:name company_name
company:address company_address
company:email contact_email
company:phone contact_phone
company:country company_country
website:url site_url
website:name site_name
current:date Auto (today)
current:year Auto (this year)

Common Placeholder Categories

Category Fields
company name, legal_name, email, phone, address, city, state, zip, country
website url, name, domain
data collected, cookies, retention, sharing, location
compliance gdpr, ccpa, coppa, ada, dpa_email
feature newsletter, analytics, medical_content, legal_content
ecommerce payment_processors, shipping_providers, return_period, shipping_time, refund_time
social content_policy, minimum_age, reporting, account_termination, content_rights

See legal/placeholders/cheat-sheet.md for the complete reference.

Conditional Sections

Templates support conditional blocks based on website type or feature/compliance flags:

{{if:ecommerce}}
This section only appears for e-commerce sites.
{{endif}}

{{if:gdpr}}
GDPR-specific compliance text.
{{endif}}

Enable compliance flags via placeholders:

$gen->setPlaceholders([
    'compliance:gdpr' => 'true',
    'compliance:ccpa' => 'true',
]);

Custom Template Directory

By default, templates are loaded from legal/ relative to the package root. Pass a custom path as the 4th constructor parameter:

$gen = new LegalPageGenerator(
    'privacy-policy',
    'ecommerce',
    $config,
    '/path/to/my/templates'
);

Your custom directory should follow the same structure:

my-templates/
  base/
    privacy-policy.md
    terms-of-service.md
  ecommerce/
    refund-policy.md

Config Integration

This package uses yohns/config to auto-load site defaults. Set up your config files:

config/config.php — Core site/company values (loaded first, available to other configs):

return [
    'company_name'    => 'Your Company, Inc.',
    'site_url'        => 'https://yoursite.com',
    'site_name'       => 'Your Site',
    'contact_email'   => 'contact@yoursite.com',
    'company_address' => '123 Main St',
    'contact_phone'   => '+1-555-0100',
    'company_country' => 'United States',
];

config/legal-pages.php — Legal-specific defaults, compliance flags, and page-specific placeholders:

return [
    // Compliance flags
    'compliance:gdpr' => true,
    'compliance:ccpa' => false,

    // Feature flags
    'feature:analytics'  => true,
    'feature:newsletter' => false,

    // Privacy Policy placeholders
    'data:collected' => 'name, email address, IP address',
    'data:cookies'   => 'essential, analytics',
    'data:retention' => '24 months',

    // Ecommerce placeholders
    'ecommerce:return_period' => '30 days',
    'ecommerce:refund_time'   => '7-10 business days',
    // ... see config/legal-pages.php for complete reference
];

These values are automatically used as placeholder defaults. Values passed to the constructor override config values.

Using Content Without Saving Files

You can use the generator to get content directly as PHP variables without writing any files. This is useful when you want to embed legal pages in your own templates, serve them dynamically, or store them in a database.

<?php
require 'vendor/autoload.php';

use Yohns\Gens\Legal\LegalPageGenerator;

$config = require __DIR__ . '/config/legal-pages.php';

$gen = new LegalPageGenerator('privacy-policy', 'personal', $config);

// Get content as variables — no files written
$markdown = $gen->generate();
$html     = $gen->convertToHtml(full: true);

// Use $html in your own layout
echo "<main class='legal-content'>$html</main>";

To generate multiple pages without saving files, set $outputDir to false and collect the results:

<?php
require 'vendor/autoload.php';

use Yohns\Gens\Legal\LegalPageGenerator;

$config      = require __DIR__ . '/config/legal-pages.php';
$websiteType = 'personal';
$outputDir   = false;

$pages = ['privacy-policy', 'terms-of-service', 'cookie-policy'];

$results = [];

foreach ($pages as $pageType) {
    $gen = new LegalPageGenerator($pageType, $websiteType, $config);

    $markdown = $gen->generate();
    $html     = $gen->convertToHtml(full: true);

    if ($outputDir) {
        $gen->savePage($markdown, "$pageType.md", $outputDir);
        $gen->savePage($html, "$pageType.html", $outputDir);
    }

    $results[$pageType] = [
        'markdown' => $markdown,
        'html'     => $html,
    ];
}

// Use the content directly
echo $results['privacy-policy']['html'];

Full Example: Generate All Legal Pages

<?php
require 'vendor/autoload.php';

use Yohns\Gens\Legal\LegalPageGenerator;

$outputDir = __DIR__ . '/generated/legal';
$websiteType = 'ecommerce';

$config = [
    'company:name'  => 'Acme Inc',
    'company:email' => 'legal@acme.com',
    'website:url'   => 'https://acme.com',
    'website:name'  => 'Acme Store',
    'compliance:gdpr' => true,
];

$pages = [
    'privacy-policy',
    'terms-of-service',
    'cookie-policy',
    'refund-policy',
    'shipping-policy',
];

foreach ($pages as $pageType) {
    $gen = new LegalPageGenerator($pageType, $websiteType, $config);

    // Save as Markdown
    $gen->savePage($gen->generate(), "$pageType.md", $outputDir);

    // Save as full HTML page
    $gen->savePage($gen->convertToHtml(full: true), "$pageType.html", $outputDir);

    echo "Generated: $pageType\n";
}

Testing

composer install
vendor/bin/phpunit tests/

License

MIT

yohns/legal-pages 适用场景与选型建议

yohns/legal-pages 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 yohns/legal-pages 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yohns/legal-pages 我们能提供哪些服务?
定制开发 / 二次开发

基于 yohns/legal-pages 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 33
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-13