定制 germania-kg/twigserviceprovider 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

germania-kg/twigserviceprovider

Composer 安装命令:

composer require germania-kg/twigserviceprovider

包简介

Pimple Service Provider for Twig templating engine

README 文档

README

Pimple Service Provider for Twig templating engine

Packagist PHP version Build Status Scrutinizer Code Quality Code Coverage Build Status

Installation with Composer

$ composer require germania-kg/twigserviceprovider

Alternatively, add this package directly to your composer.json:

"require": {
    "germania-kg/twigserviceprovider": "^1.0|^2.0"
}

The v2 release requires PHP 7.3+ and Twig from v2 and up.

Setup

Have your Pimple dependency container at hand and register the TwigServiceProvider:

<?php
use Germania\TwigServiceProvider\TwigServiceProvider;
use Pimple\Container;

$pimple = new Container;
$pimple->register( new TwigServiceProvider );

Usage

Once you've registered the TwigServiceProvider, you can grab and use your Twig_Environment like this:

<?php
$twig_environment = $pimple['Twig'];
echo $twig_environment->render('template', [ 'foo' => 'bar']);

From v2 of this package, with Twig v2 and up, this is also possible:

$twig_environment = $pimple[ \Twig\Environment::class ];

…There are more services, see Services section

Configuration

The default options

<?php
$options = array(
	// For Twig's Filesystem Loader (string or array)
	'templates' => '/path/to/templates',

	// The most important Twig Environment options
	'debug' => false,
	'cache' => '/path/to/cache',
	'auto_reload' => true,
	'autoescape'  => false,
	'strict_variables' => false	
);

You can refine these options by either passing those you like to override to the constructor or extending the Twig.Config service at runtime:

Override on instantiation

<?php
use Germania\TwigServiceProvider\TwigServiceProvider;

$custom_options = [
	'templates' => [ 
		'/another/path/to/templates',
		__DIR__ . '/vendor/foo/bar/templates'
	],
    'strict_variables' => true
];

$pimple->register( new TwigServiceProvider( $custom_options ));

Override at runtime

<?php
$pimple->register( new TwigServiceProvider );

$pimple->extend('Twig.Config', function( $defaults, $pimple) {
	return array_merge( $defaults, [
		'templates'  => $pimple['custom.templates'],
		'strict_variables' => getenv('STRICT_ONLY')
	]);
});

Other Services

Twig.Options

Per default, the Twig_Environment instance is built with the “most important” options defined configuration—see Configuration section.. You may add other options, like so:

// @return array
$pimple->extend('Twig.Options', function($options, $pimple) {
	return array_merge($options, [
		'charset' => 'iso-8859-1',
		'optimizations' => 0
	]);
});

Twig.CachePath

// @return string
$pimple->extend('Twig.CachePath', function($old, $pimple) {
	return __DIR__ . '/var/cache';
});

Twig.TemplatePaths

// @return array
$pimple->extend('Twig.TemplatePaths', function($paths, $pimple) {
	return array_merge($paths, [
		'another/one',
		'vendor/name/package/templates'
	]);
});

Twig.Loaders

This service per default contains only the Twig_Loader_Filesystem instance; To add one or more others, add them to the $loaders array:

// @return array
$pimple->extend('Twig.Loaders', function($loaders, $pimple) {
	return array_merge($loaders, [
			new Twig_Loader_Array( [ ... ] )
	]);
});

All loaders in $loaders will automatically be chained via Twig's Twig_Loader_Chain.

See Twig developer docs for full description.

Other services

All these services return an (empty) array you may extend with custom data. They all will be added into the Twig_Environment.

// @return array
$pimple->extend( 'Twig.Globals', ... );
$pimple->extend( 'Twig.Filters', ... );
$pimple->extend( 'Twig.Tests', ... );
$pimple->extend( 'Twig.Functions', ... );
$pimple->extend( 'Twig.Extensions', ... );

Development

$ git clone https://github.com/GermaniaKG/TwigServiceProvider.git
$ cd TwigServiceProvider
$ composer install

Unit tests

Either copy phpunit.xml.dist to phpunit.xml and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:

$ composer test
# or
$ vendor/bi

germania-kg/twigserviceprovider 适用场景与选型建议

germania-kg/twigserviceprovider 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 319 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 11 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 germania-kg/twigserviceprovider 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-16