crell / htmlmodel
用于建模HTML页面的域值对象
1.0.0
2016-11-27 22:28 UTC
Requires
- php: >=5.5.0
- fig/link-util: dev-master
- psr/link: ^1.0
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- phpunit/phpunit: 4.*
This package is auto-updated.
Last update: 2024-08-25 13:12:04 UTC
README
HtmlModel正如其名。它是一系列值对象,旨在建模HTML页面。它并不试图建模HTML中的每个元素(那将是荒谬的),只是其关键方面。在某种意义上,它试图提供一个HTML等价的RESTful域模型,如HAL或Atom。
受PSR-7的启发,所有对象都是不可变的。它们可以通过with*()方法进行操作,这些方法返回新的值对象实例。链接处理使用PSR-13超链接规范。
这种方法受Drupal 8开发期间存在的类似代码的启发,后来被删除。
安装
通过Composer
$ composer require crell/htmlmodel
用法
如果你使用过PSR-7的请求或响应对象,HtmlModel应该相当相似。大多数时候,你会与HtmlFragment或HtmlPage进行交互。
// Create an HtmlFragment object. $fragment = new HtmlFragment(); // Set the fragment body, which is simply an arbitrary HTML string. $fragment = $fragment->withContent('<aside>Immutable objects are easier than you think.</aside>'); // Populate its metadata; the metadata won't be rendered with this // fragment but can be transferred to an aggregating page, or a JSON // instruction in response to an Ajax call. $fragment = $fragment ->withHeadElement(new MetaRefreshElement(3, 'http://www.google/com')) ->withHeadElement(new LinkElement('canonical', 'http://www.example.com/')) ->withStyleLink(new StyleLinkElement('css.css')) ;
// Create an HtmlPage object. $html = new HtmlPage(); // Populate it with various elements and body contents. $html = $html ->withTitle('Test page') ->withHtmlAttribute('manifest', 'example.appcache') ->withBodyAttribute('foo', 'bar') ->withBase(new BaseElement('http://www.example.com/')) ->withHeadElement(new MetaRefreshElement(3, 'http://www.google.com')) ->withHeadElement(new LinkElement('canonical', 'http://www.example.com/')) ->withScript(new ScriptElement('header.js')) ->withScript(new ScriptElement('footer.js'), 'footer') ->withStyleLink(new StyleLinkElement('css.css')) ->withInlineStyle(new StyleElement('CSS here')) ->withContent('Body here') ; // Simply casting the page object to a string will produce the corresponding markup. $output = (string)$html;
HtmlPage甚至可以包含HTTP状态码。尽管它不会被渲染,但它可以被转移到响应对象中,允许页面创建者通过简单的域对象指定响应类型。
然而,真正酷的部分是当你可以将多个片段干净地聚合到一个页面中时。这让你可以并行构建页面的多个部分,甚至是异步的,甚至缓存页面的某些部分但不缓存其他部分,然后将它们合并成一个页面。
// Create an HtmlFragment (or return one from some lower-level routine) $src = new HtmlFragment(); $src = $src ->withHeadElement(new MetaRefreshElement(3, 'http://www.google.com')) ->withHeadElement(new LinkElement('canonical', 'http://www.example.com/')) ->withScript(new ScriptElement('js.js')) ->withScript(new ScriptElement('footer.js'), 'footer') ->withScript($inline_script) ->withStyleLink(new StyleLinkElement('css.css')) ->withInlineStyle(new StyleElement('CSS here')) ->withContent('Body here') ; // Now make an HtmlPage. $dest = new HtmlPage(); // Now shuffle the metadata from the fragment to the page. $transferer = new AggregateMetadataTransferer([ StyleContainerInterface::class => new StyleTransferer(), ScriptContainerInterface::class => new ScriptTransferer(), StatusCodeContainerInterface::class => new StatusCodeTransferer(), HeadElementContainerInterface::class => new HeadElementTransferer(), ]); $html = $transferer->transfer($src, $dest); // Now take other fragments and transfer their metadata to the page, aggregating them together!
变更日志
请参阅CHANGELOG以获取更多关于最近更改的信息。
测试
$ composer test
贡献
请参阅CONTRIBUTING以获取详细信息。
安全
如果你发现任何与安全相关的问题,请通过电子邮件联系作者,而不是使用问题跟踪器。
鸣谢
许可证
GNU通用公共许可证,版本3或,根据你的选择,任何后续版本。请参阅许可证文件以获取更多信息。