simphotonics/node 问题修复 & 功能扩展

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

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

simphotonics/node

Composer 安装命令:

composer require simphotonics/node

包简介

Create, edit, output XML nodes and documents.

README 文档

README

PHP Composer

Simphotonics nodes can be used to to create, edit, search, and output HTML nodes. The library contains classes that help with the creation of navigators, input elements, and HTML tables.

Node make composing HTML documents easier by removing the need for structured text and enabling reuse of HTML nodes. Most web sites use a fixed page layout that is then filled with the page content: text, anchors, images, etc. The section web page template shows how to use nodes to create a simple two column empty web page prototype.

Simphotonics Node also includes a rudimentary HTML parser, a DTD parser, and a Node Renderer. For more information visit https://github.com/simphotonics/node/tree/master/src/Parser.

Installation

From a terminal issue the command:

composer require simphotonics/node

Alternatively, add simphotonics/node to the list of required libraries in your composer.json file:

{
    "require": {
        "simphotonics/node": ">=1.0.0"
    }
}

Usage

To create nodes, an array with optional entries *kind, attr, content, and childNodes is passed to the constructor:

<?php
use Simphotonics\Node\HtmlNode;
use Simphotonics\Node\HtmlLeaf;

$img = new HtmlLeaf(
    kind:  'img',
    // Element attributes are specified in array format!
    attributes:  [
        'id' => 'logo',
        'src' => 'assets/images/logo.jpg',
        'alt' => 'Logo'
    ]
);

// All input array entries are optional. If no element kind is specified it defaults to div.
$div = new HtmlNode();

// Attributes and content can be added later.
$div->setAttr(['id' => 'demo-div'])->setCont('Text within the div element.');

$p = new HtmlNode(
    kind:  'p',
    attributes:  [
        'class' => 'demo-paragraph'
    ],
    // The (string) content of an element.
    content:  'This is the paragraph text.',
    // An array of child nodes.
    childNodes:  [$img,$div]
);

Note that the element kind refers to the HTML element tag name. The HTML paragraph in the example above is of kind p, whereas the HTML image is of kind img. To render the nodes in the example above, we use:

<?php
print $p;

The statement above returns the following string (whitespace has been added to highlight the structure of the html source code):

<p class="demo-paragraph">This is the paragraph text.
    <img id="logo" src="assets/images/logo.jpg" alt="Logo"/>
    <div id="demo-div">Text within the div element.</div>
</p>

Web Page Template

The following example shows how to quickly generate a simple web page layout using nodes. It can be used as a prototype empty HTML document that is later filled with actual web page content.

use Simphotonics\Node\HtmlLeaf;
use Simphotonics\Node\HtmlNode;
use Simphotonics\Node\HtmlCssLink;
use Simphotonics\Node\HtmlTitle;

// DTD
$dtd = new HtmlLeaf(
    kind:  '!DOCTYPE',
    content:  'html'
);

// HTML document
$doc = new HtmlNode(
    kind:  'html',
    attributes:  [
        'xml:lang' => "en-GB",
        'lang' => "en-GB"
    ]
);

// Web page title
// The title is set dynamically depending on the current URI.
// Example: www.samplesite.com/about-us => Title: My Site - About Us
$title = new HtmlTitle('My Site');

$encoding = new HtmlLeaf(
    kind:  'meta',
    attributes:  [
        'http-equiv' => 'Content-Type',
        'content' => 'text/html',
        'charset'=>'utf-8'
    ]
);

$icon = new HtmlLeaf(
    kind:  'link',
    attributes:  [
        'rel' => 'shortcut icon',
        'href' => asset('favicon.ico')
    ]
);

// The input path tells the class HtmlCssLink that style files are located in '/style'.
// If the current URI is www.samplesite.com/about-us,
//    the style file is assumed to be /style/AboutUs.css.
$css = new HtmlCssLink('/style');

// Head
$head = new HtmlNode(
  kind:  'head',
  attributes:  ['id' => 'head'],
  childNodes:  [$encoding, $title, $icon, $css]
  );

$body = new HtmlNode(
    kind:  'body',
    attributes:  ['id' => 'body']
);

// We are using a two column layout.
$col1 = new HtmlNode(
    kind:  'div',
    attributes:  ['id' => 'col1']
);

// This demonstrates cloning of nodes.
$col2 = clone $col1;
$col2->setAttr(['id' => 'col2']);

$footer = new HtmlNode(
    kind:  'div',
    attributes:  ['id' => 'footer']
);

// Compose emtpy template
$body->append([$col1,$col2,$footer]);
$doc->append([$head,$body]);

Let's assume that the PHP source code above was saved to the file layouts/emptyDocument.php. We now use the empty document layout to create the page AboutUs.php. If you are using a framework this could be the view loaded when routing to /about-us.

<?php
// Load empty document
require 'layouts/emptyDocument.php';

// Compose content
$info = new HtmlLeaf(
    kind:  'p',
    content:  'Information about www.samplesite.com.'
);

$imgAboutUs = new HtmlLeaf(
    kind:  'img',
    attributes:  [
        'id' => 'img-about-us',
        'src' => 'assets/images/aboutUs.jpg',
        'alt' => 'About Us'
    ]
);

// Add content to the empty document

// Add the info paragraph to column 1.
$col1->appendChild($info);

// Note that HtmlNode implements the array access interface.
// $col1 can also be accessed using array notation.
// Example: $doc[0] === $head, $doc[1] === $body.
//          $doc[1][0] === $col1, $doc[1][1] === $col2.

// The image is added to column 2.
$col2->appendChild($imgAboutUs);

// Render html document
print $dtd;
print $doc;

Web Page Navigator - HtmlNavigator

The class HtmlNavigator can be used to create a PHP/CSS driven web page navigator. The class searches all descendant nodes for anchors pointing to the current uri. The parent node of the anchor is then added to the CSS class 'here' (to enable styling).

A web page navigator typically consists of an unordered list where the list items are the navigator buttons and contain the navigator anchors (links). The following example illustrates how to create a simple navigator with just two entries - Home and Services.

<?php
// Anchor template
$a = new HtmlLeaf(
kind:  'a'
);

// Navigator button template
$b = new HtmlNode(
kind:  'li',
childNodes:  [$a]
);

// Create entry for home
$b_home = clone $b;
$b_home[0]->setAttr(['href' => '/'])->setCont('HOME');

// Services
$b_services = clone $b;
$b_services[0]->setAttr(['href' => '/services'])->setCont('SERVICES');

$menu = new HtmlNode(
kind:  'ul',
attributes:  ['id' => 'mainMenu'],
'child'=> [$b_home, $b_services]
);

$nav =  new HtmlNavigator(
kind:  'div',
attributes:  ['id' => 'nav','class' => 'has-shadow'],
childNodes:  [$menu]
);

Let's assume that the current relative uri is /services, then rendering $nav from within PHP yields the string:

<div id="nav" class="has-shadow">
    <ul id="mainMenu">
        <li>
            <a href="/">HOME</a>
        </li>
        <li class="here">
            <a href="/services">SERVICES</a>
        </li>
    </ul>
</div>

For completeness I include a rudimentary CSS file showing the basic styling of the navigator components. Notice the styling of the class li.here that will highlight the navigator button pointing to the current page.

#nav {
  position: relative;
  margin: auto;
  margin-bottom: 1em;
}

#mainMenu {
  top: 0;
  list-style: none;
  width: 100%;
  height: 100%;
}

#mainMenu li.here {
  background-color: #133557;
  border-left-color: #234567;
  border-right-color: #002142;
}

Html Tables Made Easy - HtmlTable

The class HtmlTable can be used to create and manipulate HTML tables. The usage is demonstrated below:

<?php
use Simphotonics\Node\HtmlTable;

\\ Table data
for ($i=1; $i < 9; $i++) {
            $data[] = 'Data'.$i;
}
\\ Construct table
$table = new HtmlTable(
    $data,   // Input data (could also be nodes)
    3,       // Set table layout to 3 columns
    HtmlTable::SET_TABLE_HEADERS, // Enable table headers
    2,       // Each 2nd row will have the style attribute class="alt"
    1        // Omit styling of the first row.
);

$print $table;

The code above will render the following html table:

Data1 Data2 Data3
Data4 Data5 Data6
Data7 Data8 Data9

Alternative rows can be styled using the CSS class alt. Table input other than nodes are wrapped in an node of kind span. The HTML source code is shown below:

<table>
    <tr>
        <th class="col1"><span>Data1</span></th>
        <th class="col2"><span>Data2</span></th>
        <th class="col3"><span>Data3</span></th>
    </tr>
    <tr class="alt">
        <td class="col1"><span>Data4</span></td>
        <td class="col2"><span>Data5</span></td>
        <td class="col3"><span>Data6</span></td>
    </tr>
    <tr>
        <td class="col1"><span>Data7</span></td>
        <td class="col2"><span>Data8</span></td>
        <td class="col3"><span>Data9</span></td>
    </tr>
</table>

The class HtmlTable contains methods that allow changing the table layout:

<?php
// Set number of columns
$table->setNumberOfColumns(4);

// Append data to last row
$table->appendToLastRow(['Data10','Data11']);

// Append new row
$table->appendRow(['Data12','Data13']);

// Delete individual row (note count starts from 0).
$table->deleteRow(1);

// Delete column (count starts from 0).
$table->deleteColumn(2);

simphotonics/node 适用场景与选型建议

simphotonics/node 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 07 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「tree」 「dom」 「markup」 「xhtml」 「node」 「ml」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 simphotonics/node 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-14