enm/external-layout

从远程加载、修改并本地存储HTML布局

1.1.2 2018-09-26 15:31 UTC

This package is auto-updated.

Last update: 2024-09-22 22:44:25 UTC


README

这个库从远程加载HTML布局,修改它们以供本地使用,并将结果存储到一个新文件中。

安装

composer require enm/external-layout

如果您想使用默认(guzzle)加载器

composer require guzzlehttp/guzzle

用法

根据您的需求,使用此库有不同的方式。

简单

最简单的使用方法是使用布局创建器工厂与默认guzzle加载器(需要 guzzlehttp/guzzle)。为了最简单的使用,您应该从配置数组构建布局定义。

$factory = \Enm\ExternalLayout\LayoutCreatorFactory::withGuzzleLoader();
$factory->enableRelativeUrlReplacement(); // adds the "UrlManipulator"
$factory->enableTwigBlocks(); // adds the "TwigManipulator" and the "WorkingTagFinisher"

$layoutCreator = $factory->create(); // create a configured instance of the LayoutCreator

$layoutCreator->createFromConfig( // load the original layout, manipulate the content and stores the modified content to the configured file
  [
      'source' => 'http://example.com', // your source url, with username and password (Basic Auth) if needed
      'destination' => __DIR__ . '/test.html.twig', // your destination file
      'blocks' => [
          'prepend' => [
              'headline' => 'body' // add block "body" as first child of html element "headline"
          ],
          'append' => [
              'stylesheets' => 'head', // add block "stylesheets" as last child of html element "head"
              'javascripts' => 'body' // add block "javascripts" as last child of html element "body"
          ],
          'replace' => [
              'title' => '%title%', // replace string "%title%" with block "title"
              'content' => '$$content$$' // replace string "$$content$$" with block "content"
          ]
      ]
  ]
);

定制

如果您想定制布局的加载、修改或完成,您也可以使用工厂并配置您自己的定制要求。

$factory = new \Enm\ExternalLayout\LayoutCreatorFactory(
    new YourLoader() // here you can set an instance of a different loader, if you don' want to use the guzzle loader
);
$factory->addManipulator(
    new YourManipulator() // it is possible to set any number of custom manipulators
);
$factory->addFinisher(
    new YourFinisher() // it is possible to set any number of custom finishers
);

$layoutCreator = $factory->create();

// ... usage the same as above

完全定制

如果您需要一个完全定制的实现(例如,用于依赖注入服务容器),您可以通过自己创建所有实例而不使用工厂。

$layoutCreator = new \Enm\ExternalLayout\LayoutCreator(
    new YourLoader(), // use your own loader or an instance of "Enm\ExternalLayout\Loader\GuzzleLoader"
    new YourManipulator(), // use your own manipulator or for example an instance of "Enm\ExternalLayout\Loader\ManipulatorChain"
    new YourFinisher() // use your own finisher or for example an instance of "Enm\ExternalLayout\Loader\FinisherChain"
);

// ... usage the same as above

定制

该库的工作方式如下

  1. 从源URL加载内容到 \DomDocument ("加载器")
  2. 通过内容替换或块(用于模板)对 \DomDocument 进行操作("操作器")
  3. 通过简单的纯文本处理完成布局("完成器")
  4. 将操作和完成的HTML内容存储到配置的文件中

加载器

加载器负责将指定URI的HTML内容加载到 \DomDocument 中。

您可以使用默认加载器(Enm\ExternalLayout\Loader\GuzzleLoader),它需要一个 GuzzleHttp\ClientInterface 实例(composer: guzzlehttp/guzzle),或者您可以实现 Enm\ExternalLayout\Loader\LoaderInterface

操作器

操作器负责操作加载的 \DomDocument。操作器可以更改内容,删除或添加新的DOM元素或插入不同模板语言的块(默认Twig)。

如果您想使用多个操作器,可以将所有操作器添加到 Enm\ExternalLayout\Manipulator\ManipulatorChain 实例中。一个操作器必须实现 Enm\ExternalLayout\Manipulator\ManipulatorInterface

可用的操作器有

  • UrlManipulator:将相对URL替换为绝对URL(因为资源通常是从原始源更远地加载的)
  • TwigManipulator:将字符串替换为twig块;在HTML元素前或后添加twig块
  • BaseUrlManipulator:删除base标签,以避免无效的本地相对路径

完成器

完成器负责清理和字符串替换,这些在 \DomDocument 中是不可能的。

如果您想使用多个完成器,可以将所有完成器添加到 Enm\ExternalLayout\Finisher\FinisherChain 实例中。一个完成器必须实现 Enm\ExternalLayout\Finisher\FinisherInterface

可用的完成器有

  • WorkingTagFinisher:删除“工作标签”,这些标签用于生成有效的XML内容,其中实际上不需要完成内容中的任何标签