本文整理汇总了PHP中Drupal\Component\Utility\Unicode::convertToUtf8方法的典型用法代码示例。如果您正苦于以下问题:PHP Unicode::convertToUtf8方法的具体用法?PHP Unicode::convertToUtf8怎么用?PHP Unicode::convertToUtf8使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Component\Utility\Unicode
的用法示例。
在下文中一共展示了Unicode::convertToUtf8方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: optimize
/**
* {@inheritdoc}
*/
public function optimize(array $js_asset)
{
if ($js_asset['type'] !== 'file') {
throw new \Exception('Only file JavaScript assets can be optimized.');
}
if ($js_asset['type'] === 'file' && !$js_asset['preprocess']) {
throw new \Exception('Only file JavaScript assets with preprocessing enabled can be optimized.');
}
// If a BOM is found, convert the file to UTF-8, then use substr() to
// remove the BOM from the result.
$data = file_get_contents($js_asset['data']);
if ($encoding = Unicode::encodingFromBOM($data)) {
$data = Unicode::substr(Unicode::convertToUtf8($data, $encoding), 1);
} elseif (isset($js_asset['attributes']['charset'])) {
$data = Unicode::convertToUtf8($data, $js_asset['attributes']['charset']);
}
// No-op optimizer: no optimizations are applied to JavaScript assets.
return $data;
}
示例2: testConvertToUtf8
/**
* Tests UTF-8 conversion.
*
* @dataProvider providerTestConvertToUtf8
* @covers ::convertToUtf8
*
* @param string $data
* The data to be converted.
* @param string $encoding
* The encoding the data is in.
* @param string|bool $expected
* The expected result.
*/
public function testConvertToUtf8($data, $encoding, $expected)
{
$this->assertEquals($expected, Unicode::convertToUtf8($data, $encoding));
}
示例3: loadFile
/**
* Loads the stylesheet and resolves all @import commands.
*
* Loads a stylesheet and replaces @import commands with the contents of the
* imported file. Use this instead of file_get_contents when processing
* stylesheets.
*
* The returned contents are compressed removing white space and comments only
* when CSS aggregation is enabled. This optimization will not apply for
* color.module enabled themes with CSS aggregation turned off.
*
* Note: the only reason this method is public is so color.module can call it;
* it is not on the AssetOptimizerInterface, so future refactorings can make
* it protected.
*
* @param $file
* Name of the stylesheet to be processed.
* @param $optimize
* Defines if CSS contents should be compressed or not.
* @param $reset_basepath
* Used internally to facilitate recursive resolution of @import commands.
*
* @return
* Contents of the stylesheet, including any resolved @import commands.
*/
public function loadFile($file, $optimize = NULL, $reset_basepath = TRUE)
{
// These statics are not cache variables, so we don't use drupal_static().
static $_optimize, $basepath;
if ($reset_basepath) {
$basepath = '';
}
// Store the value of $optimize for preg_replace_callback with nested
// @import loops.
if (isset($optimize)) {
$_optimize = $optimize;
}
// Stylesheets are relative one to each other. Start by adding a base path
// prefix provided by the parent stylesheet (if necessary).
if ($basepath && !file_uri_scheme($file)) {
$file = $basepath . '/' . $file;
}
// Store the parent base path to restore it later.
$parent_base_path = $basepath;
// Set the current base path to process possible child imports.
$basepath = dirname($file);
// Load the CSS stylesheet. We suppress errors because themes may specify
// stylesheets in their .info.yml file that don't exist in the theme's path,
// but are merely there to disable certain module CSS files.
$content = '';
if ($contents = @file_get_contents($file)) {
// If a BOM is found, convert the file to UTF-8, then use substr() to
// remove the BOM from the result.
if ($encoding = Unicode::encodingFromBOM($contents)) {
$contents = Unicode::substr(Unicode::convertToUtf8($contents, $encoding), 1);
} elseif (preg_match('/^@charset "([^"]+)";/', $contents, $matches)) {
if ($matches[1] !== 'utf-8' && $matches[1] !== 'UTF-8') {
$contents = substr($contents, strlen($matches[0]));
$contents = Unicode::convertToUtf8($contents, $matches[1]);
}
}
// Return the processed stylesheet.
$content = $this->processCss($contents, $_optimize);
}
// Restore the parent base path as the file and its children are processed.
$basepath = $parent_base_path;
return $content;
}