Compare commits

..

No commits in common. "main" and "main" have entirely different histories.
main ... main

141 changed files with 2772 additions and 3210 deletions

View File

@ -4,16 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## 2.0.1 - 2023-07-08 ## Unreleased
### Added ### Added
* Indonesian localization * Indonesian localization
* Basque localization
### Changed
* Update dependencies
## 2.0.0 - 2022-06-07 ## 2.0.0 - 2022-06-07
@ -28,7 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* Output generated QR code with `data:` URI * Output generated QR code with `data:` URI
* Change the QR code generation library * Change the QR code generation library
* Use less.php instead of lesserphp * Use less.php instead of lesserphp
* Use the prefers-color-scheme CSS feature to let the client choose its preferred theme (dark/light) * Use the prefers-color-scheme CSS feature to let the client choose its prefered theme (dark/light)
### Removed ### Removed

View File

@ -10,7 +10,7 @@ A LibreQR instance is available at <https://qr.antopie.org>.
### Generic ### Generic
Just place this source code in a Web server with PHP8.0+, extensions `gd`, `mbstring` and `iconv`, and writing rights on the `css/` directory. Just place this source code in a Web server with PHP7.4+, extensions `gd`, `mbstring` and `iconv`, and writing rights on the `css/` directory.
#### Security hardening #### Security hardening
@ -33,6 +33,7 @@ There is [a package](https://code.antopie.org/miraty/qr_ynh/) for [YunoHost](htt
For historical reasons, LibreQR is technically named `qr` in YunoHost. For historical reasons, LibreQR is technically named `qr` in YunoHost.
You can install it from the WebAdmin or with this command : You can install it from the WebAdmin or with this command :
``` ```
sudo yunohost app install qr sudo yunohost app install qr
``` ```

View File

@ -10,7 +10,7 @@ use Endroid\QrCode\Color\Color;
require "config.inc.php"; require "config.inc.php";
require "vendor/autoload.php"; require "vendor/autoload.php";
define("LIBREQR_VERSION", "2.0.1"); define("LIBREQR_VERSION", "2.0.0");
// Defines the locale to be used // Defines the locale to be used
$locale = DEFAULT_LOCALE; $locale = DEFAULT_LOCALE;

View File

@ -14,7 +14,7 @@ $loc = array(
'help_content' => " 'help_content' => "
<p>You can encode whatever text you want.</p> <p>You can encode whatever text you want.</p>
<p>Software decoding these QR codes could suggest to open them with dedicated software, depending on their <a href='https://en.wikipedia.org/wiki/List_of_URI_schemes' hreflang='en' rel='help external noreferrer'>URI scheme</a>.</p> <p>Software which decodes these QR codes could suggest to open them with dedicated software, depending on their <a href='https://en.wikipedia.org/wiki/List_of_URI_schemes' hreflang='en' rel='help external noreferrer'>URI scheme</a>.</p>
<p>For instance, to open a webpage: <code>https://www.example/</code></p> <p>For instance, to open a webpage: <code>https://www.example/</code></p>
<p>To send an email: <code>mailto:contact@email.example</code></p> <p>To send an email: <code>mailto:contact@email.example</code></p>
<p>To share geographic coordinates: <code>geo:48.867564,2.364057</code></p> <p>To share geographic coordinates: <code>geo:48.867564,2.364057</code></p>
@ -33,10 +33,10 @@ $loc = array(
'metaText_qr' => " 'metaText_qr' => "
<h3>What's a QR code?</h3> <h3>What's a QR code?</h3>
A QR code is a 2 dimensional barcode in which text is written in binary. It can be decoded with a device equipped with a photo sensor and adequate software. A QR code is a 2 dimensional barcode in which text is written in binary. It can be decoded with a device equipped with a photo sensor and an adequate software.
<a href='https://en.wikipedia.org/wiki/QR_code' hreflang='en' rel='help external noreferrer'>QR code on Wikipedia</a>. <a href='https://en.wikipedia.org/wiki/QR_code' hreflang='en' rel='help external noreferrer'>QR code on Wikipedia</a>.
", ",
'metaText_legal' => "LibreQR " . LIBREQR_VERSION . " is free software whose <a href='https://code.antopie.org/miraty/libreqr/' rel='external noreferrer'>source code</a> is available under the terms of the <abbr title='GNU Affero General Public License version 3 or any later version'><a href='LICENSE.html' hreflang='en' rel='license'>AGPLv3</a>+</abbr>.", 'metaText_legal' => "LibreQR " . LIBREQR_VERSION . " is a free software whose <a href='https://code.antopie.org/miraty/libreqr/' rel='external noreferrer'>source code</a> is available under the terms of the <abbr title='GNU Affero General Public License version 3 or any later version'><a href='LICENSE.html' hreflang='en' rel='license'>AGPLv3</a>+</abbr>.",
'error_generation' => "An error occurred while generating the QR code. Try with different parameters.", 'error_generation' => "An error occurred while generating the QR code. Try with different parameters.",
); );

19
vendor/autoload.php vendored
View File

@ -3,23 +3,10 @@
// autoload.php @generated by Composer // autoload.php @generated by Composer
if (PHP_VERSION_ID < 50600) { if (PHP_VERSION_ID < 50600) {
if (!headers_sent()) { echo 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
header('HTTP/1.1 500 Internal Server Error'); exit(1);
}
$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
if (!ini_get('display_errors')) {
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
fwrite(STDERR, $err);
} elseif (!headers_sent()) {
echo $err;
}
}
trigger_error(
$err,
E_USER_ERROR
);
} }
require_once __DIR__ . '/composer/autoload_real.php'; require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInitd4957e04ba4dff37d0ecbf0a4b59e14a::getLoader(); return ComposerAutoloaderInit6bb82695b2f28e8ee61f74ae2d5c5202::getLoader();

View File

@ -34,11 +34,5 @@
"allow-plugins": { "allow-plugins": {
"ocramius/package-versions": true "ocramius/package-versions": true
} }
},
"archive": {
"exclude": [
"/test",
"/phpunit.xml.dist"
]
} }
} }

View File

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="BaconQrCode Tests">
<directory>./test</directory>
</testsuite>
</testsuites>
</phpunit>

View File

@ -89,9 +89,6 @@ final class CharacterSetEci extends AbstractEnum
*/ */
private static $nameToEci; private static $nameToEci;
/**
* @param int[] $values
*/
public function __construct(array $values, string ...$otherEncodingNames) public function __construct(array $values, string ...$otherEncodingNames)
{ {
$this->values = $values; $this->values = $values;

View File

@ -62,7 +62,7 @@ class FormatInformation
/** /**
* Offset i holds the number of 1 bits in the binary representation of i. * Offset i holds the number of 1 bits in the binary representation of i.
* *
* @var int[] * @var array
*/ */
private const BITS_SET_IN_HALF_BYTE = [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4]; private const BITS_SET_IN_HALF_BYTE = [0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4];

View File

@ -42,9 +42,6 @@ final class Mode extends AbstractEnum
*/ */
private $bits; private $bits;
/**
* @param int[] $characterCountBitsForVersions
*/
protected function __construct(array $characterCountBitsForVersions, int $bits) protected function __construct(array $characterCountBitsForVersions, int $bits)
{ {
$this->characterCountBitsForVersions = $characterCountBitsForVersions; $this->characterCountBitsForVersions = $characterCountBitsForVersions;

View File

@ -37,7 +37,7 @@ final class Encoder
/** /**
* Codec cache. * Codec cache.
* *
* @var array<string,ReedSolomonCodec> * @var array
*/ */
private static $codecs = []; private static $codecs = [];

View File

@ -334,7 +334,7 @@ final class SvgImageBackEnd implements ImageBackEndInterface
$this->xmlWriter->writeAttribute('stop-color', $this->getColorString($startColor)); $this->xmlWriter->writeAttribute('stop-color', $this->getColorString($startColor));
if ($startColor instanceof Alpha) { if ($startColor instanceof Alpha) {
$this->xmlWriter->writeAttribute('stop-opacity', (string) $startColor->getAlpha()); $this->xmlWriter->writeAttribute('stop-opacity', $startColor->getAlpha());
} }
$this->xmlWriter->endElement(); $this->xmlWriter->endElement();
@ -344,7 +344,7 @@ final class SvgImageBackEnd implements ImageBackEndInterface
$this->xmlWriter->writeAttribute('stop-color', $this->getColorString($endColor)); $this->xmlWriter->writeAttribute('stop-color', $this->getColorString($endColor));
if ($endColor instanceof Alpha) { if ($endColor instanceof Alpha) {
$this->xmlWriter->writeAttribute('stop-opacity', (string) $endColor->getAlpha()); $this->xmlWriter->writeAttribute('stop-opacity', $endColor->getAlpha());
} }
$this->xmlWriter->endElement(); $this->xmlWriter->endElement();

View File

@ -41,7 +41,7 @@ final class EdgeIterator implements IteratorAggregate
} }
/** /**
* @return Traversable<Edge> * @return Edge[]
*/ */
public function getIterator() : Traversable public function getIterator() : Traversable
{ {

View File

@ -136,7 +136,7 @@ final class EllipticArc implements OperationInterface
/** /**
* @return Curve[] * @return Curve[]
*/ */
private function createCurves(float $fromX, float $fromY) : array private function createCurves(float $fromX, $fromY) : array
{ {
$xAngle = deg2rad($this->xAxisAngle); $xAngle = deg2rad($this->xAxisAngle);
list($centerX, $centerY, $radiusX, $radiusY, $startAngle, $deltaAngle) = list($centerX, $centerY, $radiusX, $radiusY, $startAngle, $deltaAngle) =

View File

@ -17,16 +17,10 @@ use BaconQrCode\Writer;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Spatie\Snapshots\MatchesSnapshots; use Spatie\Snapshots\MatchesSnapshots;
/**
* @group integration
*/
final class ImagickRenderingTest extends TestCase final class ImagickRenderingTest extends TestCase
{ {
use MatchesSnapshots; use MatchesSnapshots;
/**
* @requires extension imagick
*/
public function testGenericQrCode() : void public function testGenericQrCode() : void
{ {
$renderer = new ImageRenderer( $renderer = new ImageRenderer(
@ -41,9 +35,6 @@ final class ImagickRenderingTest extends TestCase
unlink($tempName); unlink($tempName);
} }
/**
* @requires extension imagick
*/
public function testIssue79() : void public function testIssue79() : void
{ {
$eye = SquareEye::instance(); $eye = SquareEye::instance();

10
vendor/bin/lessc vendored
View File

@ -108,12 +108,10 @@ if (PHP_VERSION_ID < 80000) {
} }
} }
if ( if (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper')) {
(function_exists('stream_get_wrappers') && in_array('phpvfscomposer', stream_get_wrappers(), true)) include("phpvfscomposer://" . __DIR__ . '/..'.'/wikimedia/less.php/bin/lessc');
|| (function_exists('stream_wrapper_register') && stream_wrapper_register('phpvfscomposer', 'Composer\BinProxyWrapper')) exit(0);
) {
return include("phpvfscomposer://" . __DIR__ . '/..'.'/wikimedia/less.php/bin/lessc');
} }
} }
return include __DIR__ . '/..'.'/wikimedia/less.php/bin/lessc'; include __DIR__ . '/..'.'/wikimedia/less.php/bin/lessc';

View File

@ -42,37 +42,35 @@ namespace Composer\Autoload;
*/ */
class ClassLoader class ClassLoader
{ {
/** @var \Closure(string):void */ /** @var ?string */
private static $includeFile;
/** @var string|null */
private $vendorDir; private $vendorDir;
// PSR-4 // PSR-4
/** /**
* @var array<string, array<string, int>> * @var array[]
* @psalm-var array<string, array<string, int>>
*/ */
private $prefixLengthsPsr4 = array(); private $prefixLengthsPsr4 = array();
/** /**
* @var array<string, list<string>> * @var array[]
* @psalm-var array<string, array<int, string>>
*/ */
private $prefixDirsPsr4 = array(); private $prefixDirsPsr4 = array();
/** /**
* @var list<string> * @var array[]
* @psalm-var array<string, string>
*/ */
private $fallbackDirsPsr4 = array(); private $fallbackDirsPsr4 = array();
// PSR-0 // PSR-0
/** /**
* List of PSR-0 prefixes * @var array[]
* * @psalm-var array<string, array<string, string[]>>
* Structured as array('F (first letter)' => array('Foo\Bar (full prefix)' => array('path', 'path2')))
*
* @var array<string, array<string, list<string>>>
*/ */
private $prefixesPsr0 = array(); private $prefixesPsr0 = array();
/** /**
* @var list<string> * @var array[]
* @psalm-var array<string, string>
*/ */
private $fallbackDirsPsr0 = array(); private $fallbackDirsPsr0 = array();
@ -80,7 +78,8 @@ class ClassLoader
private $useIncludePath = false; private $useIncludePath = false;
/** /**
* @var array<string, string> * @var string[]
* @psalm-var array<string, string>
*/ */
private $classMap = array(); private $classMap = array();
@ -88,29 +87,29 @@ class ClassLoader
private $classMapAuthoritative = false; private $classMapAuthoritative = false;
/** /**
* @var array<string, bool> * @var bool[]
* @psalm-var array<string, bool>
*/ */
private $missingClasses = array(); private $missingClasses = array();
/** @var string|null */ /** @var ?string */
private $apcuPrefix; private $apcuPrefix;
/** /**
* @var array<string, self> * @var self[]
*/ */
private static $registeredLoaders = array(); private static $registeredLoaders = array();
/** /**
* @param string|null $vendorDir * @param ?string $vendorDir
*/ */
public function __construct($vendorDir = null) public function __construct($vendorDir = null)
{ {
$this->vendorDir = $vendorDir; $this->vendorDir = $vendorDir;
self::initializeIncludeClosure();
} }
/** /**
* @return array<string, list<string>> * @return string[]
*/ */
public function getPrefixes() public function getPrefixes()
{ {
@ -122,7 +121,8 @@ class ClassLoader
} }
/** /**
* @return array<string, list<string>> * @return array[]
* @psalm-return array<string, array<int, string>>
*/ */
public function getPrefixesPsr4() public function getPrefixesPsr4()
{ {
@ -130,7 +130,8 @@ class ClassLoader
} }
/** /**
* @return list<string> * @return array[]
* @psalm-return array<string, string>
*/ */
public function getFallbackDirs() public function getFallbackDirs()
{ {
@ -138,7 +139,8 @@ class ClassLoader
} }
/** /**
* @return list<string> * @return array[]
* @psalm-return array<string, string>
*/ */
public function getFallbackDirsPsr4() public function getFallbackDirsPsr4()
{ {
@ -146,7 +148,8 @@ class ClassLoader
} }
/** /**
* @return array<string, string> Array of classname => path * @return string[] Array of classname => path
* @psalm-return array<string, string>
*/ */
public function getClassMap() public function getClassMap()
{ {
@ -154,7 +157,8 @@ class ClassLoader
} }
/** /**
* @param array<string, string> $classMap Class to filename map * @param string[] $classMap Class to filename map
* @psalm-param array<string, string> $classMap
* *
* @return void * @return void
*/ */
@ -172,24 +176,23 @@ class ClassLoader
* appending or prepending to the ones previously set for this prefix. * appending or prepending to the ones previously set for this prefix.
* *
* @param string $prefix The prefix * @param string $prefix The prefix
* @param list<string>|string $paths The PSR-0 root directories * @param string[]|string $paths The PSR-0 root directories
* @param bool $prepend Whether to prepend the directories * @param bool $prepend Whether to prepend the directories
* *
* @return void * @return void
*/ */
public function add($prefix, $paths, $prepend = false) public function add($prefix, $paths, $prepend = false)
{ {
$paths = (array) $paths;
if (!$prefix) { if (!$prefix) {
if ($prepend) { if ($prepend) {
$this->fallbackDirsPsr0 = array_merge( $this->fallbackDirsPsr0 = array_merge(
$paths, (array) $paths,
$this->fallbackDirsPsr0 $this->fallbackDirsPsr0
); );
} else { } else {
$this->fallbackDirsPsr0 = array_merge( $this->fallbackDirsPsr0 = array_merge(
$this->fallbackDirsPsr0, $this->fallbackDirsPsr0,
$paths (array) $paths
); );
} }
@ -198,19 +201,19 @@ class ClassLoader
$first = $prefix[0]; $first = $prefix[0];
if (!isset($this->prefixesPsr0[$first][$prefix])) { if (!isset($this->prefixesPsr0[$first][$prefix])) {
$this->prefixesPsr0[$first][$prefix] = $paths; $this->prefixesPsr0[$first][$prefix] = (array) $paths;
return; return;
} }
if ($prepend) { if ($prepend) {
$this->prefixesPsr0[$first][$prefix] = array_merge( $this->prefixesPsr0[$first][$prefix] = array_merge(
$paths, (array) $paths,
$this->prefixesPsr0[$first][$prefix] $this->prefixesPsr0[$first][$prefix]
); );
} else { } else {
$this->prefixesPsr0[$first][$prefix] = array_merge( $this->prefixesPsr0[$first][$prefix] = array_merge(
$this->prefixesPsr0[$first][$prefix], $this->prefixesPsr0[$first][$prefix],
$paths (array) $paths
); );
} }
} }
@ -220,7 +223,7 @@ class ClassLoader
* appending or prepending to the ones previously set for this namespace. * appending or prepending to the ones previously set for this namespace.
* *
* @param string $prefix The prefix/namespace, with trailing '\\' * @param string $prefix The prefix/namespace, with trailing '\\'
* @param list<string>|string $paths The PSR-4 base directories * @param string[]|string $paths The PSR-4 base directories
* @param bool $prepend Whether to prepend the directories * @param bool $prepend Whether to prepend the directories
* *
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
@ -229,18 +232,17 @@ class ClassLoader
*/ */
public function addPsr4($prefix, $paths, $prepend = false) public function addPsr4($prefix, $paths, $prepend = false)
{ {
$paths = (array) $paths;
if (!$prefix) { if (!$prefix) {
// Register directories for the root namespace. // Register directories for the root namespace.
if ($prepend) { if ($prepend) {
$this->fallbackDirsPsr4 = array_merge( $this->fallbackDirsPsr4 = array_merge(
$paths, (array) $paths,
$this->fallbackDirsPsr4 $this->fallbackDirsPsr4
); );
} else { } else {
$this->fallbackDirsPsr4 = array_merge( $this->fallbackDirsPsr4 = array_merge(
$this->fallbackDirsPsr4, $this->fallbackDirsPsr4,
$paths (array) $paths
); );
} }
} elseif (!isset($this->prefixDirsPsr4[$prefix])) { } elseif (!isset($this->prefixDirsPsr4[$prefix])) {
@ -250,18 +252,18 @@ class ClassLoader
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
} }
$this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
$this->prefixDirsPsr4[$prefix] = $paths; $this->prefixDirsPsr4[$prefix] = (array) $paths;
} elseif ($prepend) { } elseif ($prepend) {
// Prepend directories for an already registered namespace. // Prepend directories for an already registered namespace.
$this->prefixDirsPsr4[$prefix] = array_merge( $this->prefixDirsPsr4[$prefix] = array_merge(
$paths, (array) $paths,
$this->prefixDirsPsr4[$prefix] $this->prefixDirsPsr4[$prefix]
); );
} else { } else {
// Append directories for an already registered namespace. // Append directories for an already registered namespace.
$this->prefixDirsPsr4[$prefix] = array_merge( $this->prefixDirsPsr4[$prefix] = array_merge(
$this->prefixDirsPsr4[$prefix], $this->prefixDirsPsr4[$prefix],
$paths (array) $paths
); );
} }
} }
@ -271,7 +273,7 @@ class ClassLoader
* replacing any others previously set for this prefix. * replacing any others previously set for this prefix.
* *
* @param string $prefix The prefix * @param string $prefix The prefix
* @param list<string>|string $paths The PSR-0 base directories * @param string[]|string $paths The PSR-0 base directories
* *
* @return void * @return void
*/ */
@ -289,7 +291,7 @@ class ClassLoader
* replacing any others previously set for this namespace. * replacing any others previously set for this namespace.
* *
* @param string $prefix The prefix/namespace, with trailing '\\' * @param string $prefix The prefix/namespace, with trailing '\\'
* @param list<string>|string $paths The PSR-4 base directories * @param string[]|string $paths The PSR-4 base directories
* *
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
* *
@ -423,8 +425,7 @@ class ClassLoader
public function loadClass($class) public function loadClass($class)
{ {
if ($file = $this->findFile($class)) { if ($file = $this->findFile($class)) {
$includeFile = self::$includeFile; includeFile($file);
$includeFile($file);
return true; return true;
} }
@ -475,9 +476,9 @@ class ClassLoader
} }
/** /**
* Returns the currently registered loaders keyed by their corresponding vendor directories. * Returns the currently registered loaders indexed by their corresponding vendor directories.
* *
* @return array<string, self> * @return self[]
*/ */
public static function getRegisteredLoaders() public static function getRegisteredLoaders()
{ {
@ -554,14 +555,6 @@ class ClassLoader
return false; return false;
} }
/**
* @return void
*/
private static function initializeIncludeClosure()
{
if (self::$includeFile !== null) {
return;
} }
/** /**
@ -571,9 +564,9 @@ class ClassLoader
* *
* @param string $file * @param string $file
* @return void * @return void
* @private
*/ */
self::$includeFile = \Closure::bind(static function($file) { function includeFile($file)
{
include $file; include $file;
}, null, null);
}
} }

View File

@ -98,7 +98,7 @@ class InstalledVersions
{ {
foreach (self::getInstalled() as $installed) { foreach (self::getInstalled() as $installed) {
if (isset($installed['versions'][$packageName])) { if (isset($installed['versions'][$packageName])) {
return $includeDevRequirements || !isset($installed['versions'][$packageName]['dev_requirement']) || $installed['versions'][$packageName]['dev_requirement'] === false; return $includeDevRequirements || empty($installed['versions'][$packageName]['dev_requirement']);
} }
} }
@ -119,7 +119,7 @@ class InstalledVersions
*/ */
public static function satisfies(VersionParser $parser, $packageName, $constraint) public static function satisfies(VersionParser $parser, $packageName, $constraint)
{ {
$constraint = $parser->parseConstraints((string) $constraint); $constraint = $parser->parseConstraints($constraint);
$provided = $parser->parseConstraints(self::getVersionRanges($packageName)); $provided = $parser->parseConstraints(self::getVersionRanges($packageName));
return $provided->matches($constraint); return $provided->matches($constraint);
@ -328,9 +328,7 @@ class InstalledVersions
if (isset(self::$installedByVendor[$vendorDir])) { if (isset(self::$installedByVendor[$vendorDir])) {
$installed[] = self::$installedByVendor[$vendorDir]; $installed[] = self::$installedByVendor[$vendorDir];
} elseif (is_file($vendorDir.'/composer/installed.php')) { } elseif (is_file($vendorDir.'/composer/installed.php')) {
/** @var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $required */ $installed[] = self::$installedByVendor[$vendorDir] = require $vendorDir.'/composer/installed.php';
$required = require $vendorDir.'/composer/installed.php';
$installed[] = self::$installedByVendor[$vendorDir] = $required;
if (null === self::$installed && strtr($vendorDir.'/composer', '\\', '/') === strtr(__DIR__, '\\', '/')) { if (null === self::$installed && strtr($vendorDir.'/composer', '\\', '/') === strtr(__DIR__, '\\', '/')) {
self::$installed = $installed[count($installed) - 1]; self::$installed = $installed[count($installed) - 1];
} }
@ -342,17 +340,12 @@ class InstalledVersions
// only require the installed.php file if this file is loaded from its dumped location, // only require the installed.php file if this file is loaded from its dumped location,
// and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937 // and not from its source location in the composer/composer package, see https://github.com/composer/composer/issues/9937
if (substr(__DIR__, -8, 1) !== 'C') { if (substr(__DIR__, -8, 1) !== 'C') {
/** @var array{root: array{name: string, pretty_version: string, version: string, reference: string|null, type: string, install_path: string, aliases: string[], dev: bool}, versions: array<string, array{pretty_version?: string, version?: string, reference?: string|null, type?: string, install_path?: string, aliases?: string[], dev_requirement: bool, replaced?: string[], provided?: string[]}>} $required */ self::$installed = require __DIR__ . '/installed.php';
$required = require __DIR__ . '/installed.php';
self::$installed = $required;
} else { } else {
self::$installed = array(); self::$installed = array();
} }
} }
if (self::$installed !== array()) {
$installed[] = self::$installed; $installed[] = self::$installed;
}
return $installed; return $installed;
} }

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer // autoload_real.php @generated by Composer
class ComposerAutoloaderInitd4957e04ba4dff37d0ecbf0a4b59e14a class ComposerAutoloaderInit6bb82695b2f28e8ee61f74ae2d5c5202
{ {
private static $loader; private static $loader;
@ -24,12 +24,12 @@ class ComposerAutoloaderInitd4957e04ba4dff37d0ecbf0a4b59e14a
require __DIR__ . '/platform_check.php'; require __DIR__ . '/platform_check.php';
spl_autoload_register(array('ComposerAutoloaderInitd4957e04ba4dff37d0ecbf0a4b59e14a', 'loadClassLoader'), true, true); spl_autoload_register(array('ComposerAutoloaderInit6bb82695b2f28e8ee61f74ae2d5c5202', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitd4957e04ba4dff37d0ecbf0a4b59e14a', 'loadClassLoader')); spl_autoload_unregister(array('ComposerAutoloaderInit6bb82695b2f28e8ee61f74ae2d5c5202', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php'; require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitd4957e04ba4dff37d0ecbf0a4b59e14a::getInitializer($loader)); call_user_func(\Composer\Autoload\ComposerStaticInit6bb82695b2f28e8ee61f74ae2d5c5202::getInitializer($loader));
$loader->register(true); $loader->register(true);

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload; namespace Composer\Autoload;
class ComposerStaticInitd4957e04ba4dff37d0ecbf0a4b59e14a class ComposerStaticInit6bb82695b2f28e8ee61f74ae2d5c5202
{ {
public static $prefixLengthsPsr4 = array ( public static $prefixLengthsPsr4 = array (
'E' => 'E' =>
@ -54,10 +54,10 @@ class ComposerStaticInitd4957e04ba4dff37d0ecbf0a4b59e14a
public static function getInitializer(ClassLoader $loader) public static function getInitializer(ClassLoader $loader)
{ {
return \Closure::bind(function () use ($loader) { return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitd4957e04ba4dff37d0ecbf0a4b59e14a::$prefixLengthsPsr4; $loader->prefixLengthsPsr4 = ComposerStaticInit6bb82695b2f28e8ee61f74ae2d5c5202::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitd4957e04ba4dff37d0ecbf0a4b59e14a::$prefixDirsPsr4; $loader->prefixDirsPsr4 = ComposerStaticInit6bb82695b2f28e8ee61f74ae2d5c5202::$prefixDirsPsr4;
$loader->prefixesPsr0 = ComposerStaticInitd4957e04ba4dff37d0ecbf0a4b59e14a::$prefixesPsr0; $loader->prefixesPsr0 = ComposerStaticInit6bb82695b2f28e8ee61f74ae2d5c5202::$prefixesPsr0;
$loader->classMap = ComposerStaticInitd4957e04ba4dff37d0ecbf0a4b59e14a::$classMap; $loader->classMap = ComposerStaticInit6bb82695b2f28e8ee61f74ae2d5c5202::$classMap;
}, null, ClassLoader::class); }, null, ClassLoader::class);
} }

View File

@ -2,17 +2,17 @@
"packages": [ "packages": [
{ {
"name": "bacon/bacon-qr-code", "name": "bacon/bacon-qr-code",
"version": "2.0.8", "version": "2.0.7",
"version_normalized": "2.0.8.0", "version_normalized": "2.0.7.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/Bacon/BaconQrCode.git", "url": "https://github.com/Bacon/BaconQrCode.git",
"reference": "8674e51bb65af933a5ffaf1c308a660387c35c22" "reference": "d70c840f68657ce49094b8d91f9ee0cc07fbf66c"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/Bacon/BaconQrCode/zipball/8674e51bb65af933a5ffaf1c308a660387c35c22", "url": "https://api.github.com/repos/Bacon/BaconQrCode/zipball/d70c840f68657ce49094b8d91f9ee0cc07fbf66c",
"reference": "8674e51bb65af933a5ffaf1c308a660387c35c22", "reference": "d70c840f68657ce49094b8d91f9ee0cc07fbf66c",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -29,7 +29,7 @@
"suggest": { "suggest": {
"ext-imagick": "to generate QR code images" "ext-imagick": "to generate QR code images"
}, },
"time": "2022-12-07T17:46:57+00:00", "time": "2022-03-14T02:02:36+00:00",
"type": "library", "type": "library",
"installation-source": "dist", "installation-source": "dist",
"autoload": { "autoload": {
@ -53,33 +53,30 @@
"homepage": "https://github.com/Bacon/BaconQrCode", "homepage": "https://github.com/Bacon/BaconQrCode",
"support": { "support": {
"issues": "https://github.com/Bacon/BaconQrCode/issues", "issues": "https://github.com/Bacon/BaconQrCode/issues",
"source": "https://github.com/Bacon/BaconQrCode/tree/2.0.8" "source": "https://github.com/Bacon/BaconQrCode/tree/2.0.7"
}, },
"install-path": "../bacon/bacon-qr-code" "install-path": "../bacon/bacon-qr-code"
}, },
{ {
"name": "dasprid/enum", "name": "dasprid/enum",
"version": "1.0.4", "version": "1.0.3",
"version_normalized": "1.0.4.0", "version_normalized": "1.0.3.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/DASPRiD/Enum.git", "url": "https://github.com/DASPRiD/Enum.git",
"reference": "8e6b6ea76eabbf19ea2bf5b67b98e1860474012f" "reference": "5abf82f213618696dda8e3bf6f64dd042d8542b2"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/DASPRiD/Enum/zipball/8e6b6ea76eabbf19ea2bf5b67b98e1860474012f", "url": "https://api.github.com/repos/DASPRiD/Enum/zipball/5abf82f213618696dda8e3bf6f64dd042d8542b2",
"reference": "8e6b6ea76eabbf19ea2bf5b67b98e1860474012f", "reference": "5abf82f213618696dda8e3bf6f64dd042d8542b2",
"shasum": "" "shasum": ""
}, },
"require": {
"php": ">=7.1 <9.0"
},
"require-dev": { "require-dev": {
"phpunit/phpunit": "^7 | ^8 | ^9", "phpunit/phpunit": "^7 | ^8 | ^9",
"squizlabs/php_codesniffer": "*" "squizlabs/php_codesniffer": "^3.4"
}, },
"time": "2023-03-01T18:44:03+00:00", "time": "2020-10-02T16:03:48+00:00",
"type": "library", "type": "library",
"installation-source": "dist", "installation-source": "dist",
"autoload": { "autoload": {
@ -106,36 +103,33 @@
], ],
"support": { "support": {
"issues": "https://github.com/DASPRiD/Enum/issues", "issues": "https://github.com/DASPRiD/Enum/issues",
"source": "https://github.com/DASPRiD/Enum/tree/1.0.4" "source": "https://github.com/DASPRiD/Enum/tree/1.0.3"
}, },
"install-path": "../dasprid/enum" "install-path": "../dasprid/enum"
}, },
{ {
"name": "endroid/qr-code", "name": "endroid/qr-code",
"version": "4.8.2", "version": "4.4.9",
"version_normalized": "4.8.2.0", "version_normalized": "4.4.9.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/endroid/qr-code.git", "url": "https://github.com/endroid/qr-code.git",
"reference": "2436c2333a3931c95e2b96eb82f16f53143d6bba" "reference": "bf087fa1e93a1b7310e2d94d187e26ae51db199d"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/endroid/qr-code/zipball/2436c2333a3931c95e2b96eb82f16f53143d6bba", "url": "https://api.github.com/repos/endroid/qr-code/zipball/bf087fa1e93a1b7310e2d94d187e26ae51db199d",
"reference": "2436c2333a3931c95e2b96eb82f16f53143d6bba", "reference": "bf087fa1e93a1b7310e2d94d187e26ae51db199d",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"bacon/bacon-qr-code": "^2.0.5", "bacon/bacon-qr-code": "^2.0.5",
"php": "^8.0" "php": "^7.4||^8.0"
},
"conflict": {
"khanamiryan/qrcode-detector-decoder": "^1.0.6"
}, },
"require-dev": { "require-dev": {
"endroid/quality": "dev-master", "endroid/quality": "dev-master",
"ext-gd": "*", "ext-gd": "*",
"khanamiryan/qrcode-detector-decoder": "^1.0.4||^2.0.2", "khanamiryan/qrcode-detector-decoder": "^1.0.4",
"setasign/fpdf": "^1.8.2" "setasign/fpdf": "^1.8.2"
}, },
"suggest": { "suggest": {
@ -144,7 +138,7 @@
"roave/security-advisories": "Makes sure package versions with known security issues are not installed", "roave/security-advisories": "Makes sure package versions with known security issues are not installed",
"setasign/fpdf": "Enables you to use the PDF writer" "setasign/fpdf": "Enables you to use the PDF writer"
}, },
"time": "2023-03-30T18:46:02+00:00", "time": "2022-05-10T07:25:08+00:00",
"type": "library", "type": "library",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
@ -178,7 +172,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/endroid/qr-code/issues", "issues": "https://github.com/endroid/qr-code/issues",
"source": "https://github.com/endroid/qr-code/tree/4.8.2" "source": "https://github.com/endroid/qr-code/tree/4.4.9"
}, },
"funding": [ "funding": [
{ {
@ -190,31 +184,30 @@
}, },
{ {
"name": "wikimedia/less.php", "name": "wikimedia/less.php",
"version": "v3.2.1", "version": "v3.1.0",
"version_normalized": "3.2.1.0", "version_normalized": "3.1.0.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/wikimedia/less.php.git", "url": "https://github.com/wikimedia/less.php.git",
"reference": "0d5b30ba792bdbf8991a646fc9c30561b38a5559" "reference": "a486d78b9bd16b72f237fc6093aa56d69ce8bd13"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/wikimedia/less.php/zipball/0d5b30ba792bdbf8991a646fc9c30561b38a5559", "url": "https://api.github.com/repos/wikimedia/less.php/zipball/a486d78b9bd16b72f237fc6093aa56d69ce8bd13",
"reference": "0d5b30ba792bdbf8991a646fc9c30561b38a5559", "reference": "a486d78b9bd16b72f237fc6093aa56d69ce8bd13",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": ">=7.2.9" "php": ">=7.2.9"
}, },
"require-dev": { "require-dev": {
"mediawiki/mediawiki-codesniffer": "40.0.1", "mediawiki/mediawiki-codesniffer": "34.0.0",
"mediawiki/mediawiki-phan-config": "0.12.0", "mediawiki/minus-x": "1.0.0",
"mediawiki/minus-x": "1.1.1", "php-parallel-lint/php-console-highlighter": "0.5.0",
"php-parallel-lint/php-console-highlighter": "1.0.0", "php-parallel-lint/php-parallel-lint": "1.2.0",
"php-parallel-lint/php-parallel-lint": "1.3.2",
"phpunit/phpunit": "^8.5" "phpunit/phpunit": "^8.5"
}, },
"time": "2023-02-03T06:43:41+00:00", "time": "2020-12-11T19:33:31+00:00",
"bin": [ "bin": [
"bin/lessc" "bin/lessc"
], ],
@ -233,10 +226,6 @@
"Apache-2.0" "Apache-2.0"
], ],
"authors": [ "authors": [
{
"name": "Timo Tijhof",
"homepage": "https://timotijhof.net"
},
{ {
"name": "Josh Schmidt", "name": "Josh Schmidt",
"homepage": "https://github.com/oyejorge" "homepage": "https://github.com/oyejorge"
@ -250,8 +239,7 @@
"homepage": "https://github.com/Mordred" "homepage": "https://github.com/Mordred"
} }
], ],
"description": "PHP port of the LESS processor", "description": "PHP port of the Javascript version of LESS http://lesscss.org (Originally maintained by Josh Schmidt)",
"homepage": "https://gerrit.wikimedia.org/g/mediawiki/libs/less.php",
"keywords": [ "keywords": [
"css", "css",
"less", "less",
@ -262,7 +250,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/wikimedia/less.php/issues", "issues": "https://github.com/wikimedia/less.php/issues",
"source": "https://github.com/wikimedia/less.php/tree/v3.2.1" "source": "https://github.com/wikimedia/less.php/tree/v3.1.0"
}, },
"install-path": "../wikimedia/less.php" "install-path": "../wikimedia/less.php"
} }

View File

@ -3,7 +3,7 @@
'name' => '__root__', 'name' => '__root__',
'pretty_version' => 'dev-main', 'pretty_version' => 'dev-main',
'version' => 'dev-main', 'version' => 'dev-main',
'reference' => 'b4ef98673f4ec96aac0c1ff7bda444f72101c479', 'reference' => '5e455c1499d4fba2d9bb825e4db8b58a3a6a595e',
'type' => 'library', 'type' => 'library',
'install_path' => __DIR__ . '/../../', 'install_path' => __DIR__ . '/../../',
'aliases' => array(), 'aliases' => array(),
@ -13,43 +13,43 @@
'__root__' => array( '__root__' => array(
'pretty_version' => 'dev-main', 'pretty_version' => 'dev-main',
'version' => 'dev-main', 'version' => 'dev-main',
'reference' => 'b4ef98673f4ec96aac0c1ff7bda444f72101c479', 'reference' => '5e455c1499d4fba2d9bb825e4db8b58a3a6a595e',
'type' => 'library', 'type' => 'library',
'install_path' => __DIR__ . '/../../', 'install_path' => __DIR__ . '/../../',
'aliases' => array(), 'aliases' => array(),
'dev_requirement' => false, 'dev_requirement' => false,
), ),
'bacon/bacon-qr-code' => array( 'bacon/bacon-qr-code' => array(
'pretty_version' => '2.0.8', 'pretty_version' => '2.0.7',
'version' => '2.0.8.0', 'version' => '2.0.7.0',
'reference' => '8674e51bb65af933a5ffaf1c308a660387c35c22', 'reference' => 'd70c840f68657ce49094b8d91f9ee0cc07fbf66c',
'type' => 'library', 'type' => 'library',
'install_path' => __DIR__ . '/../bacon/bacon-qr-code', 'install_path' => __DIR__ . '/../bacon/bacon-qr-code',
'aliases' => array(), 'aliases' => array(),
'dev_requirement' => false, 'dev_requirement' => false,
), ),
'dasprid/enum' => array( 'dasprid/enum' => array(
'pretty_version' => '1.0.4', 'pretty_version' => '1.0.3',
'version' => '1.0.4.0', 'version' => '1.0.3.0',
'reference' => '8e6b6ea76eabbf19ea2bf5b67b98e1860474012f', 'reference' => '5abf82f213618696dda8e3bf6f64dd042d8542b2',
'type' => 'library', 'type' => 'library',
'install_path' => __DIR__ . '/../dasprid/enum', 'install_path' => __DIR__ . '/../dasprid/enum',
'aliases' => array(), 'aliases' => array(),
'dev_requirement' => false, 'dev_requirement' => false,
), ),
'endroid/qr-code' => array( 'endroid/qr-code' => array(
'pretty_version' => '4.8.2', 'pretty_version' => '4.4.9',
'version' => '4.8.2.0', 'version' => '4.4.9.0',
'reference' => '2436c2333a3931c95e2b96eb82f16f53143d6bba', 'reference' => 'bf087fa1e93a1b7310e2d94d187e26ae51db199d',
'type' => 'library', 'type' => 'library',
'install_path' => __DIR__ . '/../endroid/qr-code', 'install_path' => __DIR__ . '/../endroid/qr-code',
'aliases' => array(), 'aliases' => array(),
'dev_requirement' => false, 'dev_requirement' => false,
), ),
'wikimedia/less.php' => array( 'wikimedia/less.php' => array(
'pretty_version' => 'v3.2.1', 'pretty_version' => 'v3.1.0',
'version' => '3.2.1.0', 'version' => '3.1.0.0',
'reference' => '0d5b30ba792bdbf8991a646fc9c30561b38a5559', 'reference' => 'a486d78b9bd16b72f237fc6093aa56d69ce8bd13',
'type' => 'library', 'type' => 'library',
'install_path' => __DIR__ . '/../wikimedia/less.php', 'install_path' => __DIR__ . '/../wikimedia/less.php',
'aliases' => array(), 'aliases' => array(),

View File

@ -4,8 +4,8 @@
$issues = array(); $issues = array();
if (!(PHP_VERSION_ID >= 80000)) { if (!(PHP_VERSION_ID >= 70400)) {
$issues[] = 'Your Composer dependencies require a PHP version ">= 8.0.0". You are running ' . PHP_VERSION . '.'; $issues[] = 'Your Composer dependencies require a PHP version ">= 7.4.0". You are running ' . PHP_VERSION . '.';
} }
if ($issues) { if ($issues) {

View File

@ -1,47 +0,0 @@
name: Tests
on: [push, pull_request]
jobs:
php-tests:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
php: [8.2, 8.1, 8.0, 7.4, 7.3, 7.2, 7.1]
dependency-version: [prefer-stable]
os: [ubuntu-latest, windows-latest]
name: ${{ matrix.os }} - PHP${{ matrix.php }} - ${{ matrix.dependency-version }}
steps:
- name: Set git to use LF
run: |
git config --global core.autocrlf false
git config --global core.eol lf
- name: Checkout code
uses: actions/checkout@v3
- name: Cache dependencies
uses: actions/cache@v3
with:
path: ~/.composer/cache/files
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none
- name: Install dependencies
run: |
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
- name: Execute tests
run: vendor/bin/phpunit
- name: Code Sniffer
run: vendor/bin/phpcs

View File

@ -1,6 +1,6 @@
# PHP 7.1 enums # PHP 7.1 enums
[![Build Status](https://github.com/DASPRiD/Enum/actions/workflows/tests.yml/badge.svg)](https://github.com/DASPRiD/Enum/actions?query=workflow%3Atests) [![Build Status](https://travis-ci.org/DASPRiD/Enum.svg?branch=master)](https://travis-ci.org/DASPRiD/Enum)
[![Coverage Status](https://coveralls.io/repos/github/DASPRiD/Enum/badge.svg?branch=master)](https://coveralls.io/github/DASPRiD/Enum?branch=master) [![Coverage Status](https://coveralls.io/repos/github/DASPRiD/Enum/badge.svg?branch=master)](https://coveralls.io/github/DASPRiD/Enum?branch=master)
[![Latest Stable Version](https://poser.pugx.org/dasprid/enum/v/stable)](https://packagist.org/packages/dasprid/enum) [![Latest Stable Version](https://poser.pugx.org/dasprid/enum/v/stable)](https://packagist.org/packages/dasprid/enum)
[![Total Downloads](https://poser.pugx.org/dasprid/enum/downloads)](https://packagist.org/packages/dasprid/enum) [![Total Downloads](https://poser.pugx.org/dasprid/enum/downloads)](https://packagist.org/packages/dasprid/enum)

View File

@ -14,12 +14,9 @@
"enum", "enum",
"map" "map"
], ],
"require": {
"php": ">=7.1 <9.0"
},
"require-dev": { "require-dev": {
"phpunit/phpunit": "^7 | ^8 | ^9", "phpunit/phpunit": "^7 | ^8 | ^9",
"squizlabs/php_codesniffer": "*" "squizlabs/php_codesniffer": "^3.4"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View File

@ -88,31 +88,6 @@ final class EnumMap implements Serializable, IteratorAggregate
$this->values = array_fill(0, count($this->keyUniverse), null); $this->values = array_fill(0, count($this->keyUniverse), null);
} }
public function __serialize(): array
{
$values = [];
foreach ($this->values as $ordinal => $value) {
if (null === $value) {
continue;
}
$values[$ordinal] = $this->unmaskNull($value);
}
return [
'keyType' => $this->keyType,
'valueType' => $this->valueType,
'allowNullValues' => $this->allowNullValues,
'values' => $values,
];
}
public function __unserialize(array $data): void
{
$this->unserialize(serialize($data));
}
/** /**
* Checks whether the map types match the supplied ones. * Checks whether the map types match the supplied ones.
* *
@ -286,7 +261,22 @@ final class EnumMap implements Serializable, IteratorAggregate
public function serialize() : string public function serialize() : string
{ {
return serialize($this->__serialize()); $values = [];
foreach ($this->values as $ordinal => $value) {
if (null === $value) {
continue;
}
$values[$ordinal] = $this->unmaskNull($value);
}
return serialize([
'keyType' => $this->keyType,
'valueType' => $this->valueType,
'allowNullValues' => $this->allowNullValues,
'values' => $values,
]);
} }
public function unserialize($serialized) : void public function unserialize($serialized) : void

View File

@ -1,4 +1,4 @@
Copyright 2022 (c) Jeroen van den Enden Copyright 2020 (c) Jeroen van den Enden
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal

View File

@ -14,17 +14,13 @@ for validating generated QR codes. Further extended with Twig extensions, genera
Symfony bundle for easy installation and configuration. Different writers are provided to generate the QR code Symfony bundle for easy installation and configuration. Different writers are provided to generate the QR code
as PNG, SVG, EPS or in binary format. as PNG, SVG, EPS or in binary format.
## Sponsored by
[![Blackfire.io](assets/blackfire.png)](https://www.blackfire.io)
## Installation ## Installation
Use [Composer](https://getcomposer.org/) to install the library. Also make sure you have enabled and configured the Use [Composer](https://getcomposer.org/) to install the library. Also make sure you have enabled and configured the
[GD extension](https://www.php.net/manual/en/book.image.php) if you want to generate images. [GD extension](https://www.php.net/manual/en/book.image.php) if you want to generate images.
``` bash ``` bash
composer require endroid/qr-code $ composer require endroid/qr-code
``` ```
## Usage: using the builder ## Usage: using the builder
@ -51,7 +47,6 @@ $result = Builder::create()
->labelText('This is the label') ->labelText('This is the label')
->labelFont(new NotoSans(20)) ->labelFont(new NotoSans(20))
->labelAlignment(new LabelAlignmentCenter()) ->labelAlignment(new LabelAlignmentCenter())
->validateResult(false)
->build(); ->build();
``` ```
@ -66,12 +61,11 @@ use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Logo\Logo; use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter; use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\ValidationException;
$writer = new PngWriter(); $writer = new PngWriter();
// Create QR code // Create QR code
$qrCode = QrCode::create('Life is too short to be generating QR codes') $qrCode = QrCode::create('Data')
->setEncoding(new Encoding('UTF-8')) ->setEncoding(new Encoding('UTF-8'))
->setErrorCorrectionLevel(new ErrorCorrectionLevelLow()) ->setErrorCorrectionLevel(new ErrorCorrectionLevelLow())
->setSize(300) ->setSize(300)
@ -89,9 +83,6 @@ $label = Label::create('Label')
->setTextColor(new Color(255, 0, 0)); ->setTextColor(new Color(255, 0, 0));
$result = $writer->write($qrCode, $logo, $label); $result = $writer->write($qrCode, $logo, $label);
// Validate the result
$writer->validateResult($result, 'Life is too short to be generating QR codes');
``` ```
## Usage: working with results ## Usage: working with results
@ -113,32 +104,10 @@ $dataUri = $result->getDataUri();
### Writer options ### Writer options
Some writers provide writer options. Each available writer option is can be
found as a constant prefixed with WRITER_OPTION_ in the writer class.
* `PdfWriter`
* `unit`: unit of measurement (default: mm)
* `fpdf`: PDF to place the image in (default: new PDF)
* `x`: image offset (default: 0)
* `y`: image offset (default: 0)
* `PngWriter`
* `compression_level`: compression level (0-9, default: -1 = zlib default)
* `SvgWriter`
* `block_id`: id of the block element for external reference (default: block)
* `exclude_xml_declaration`: exclude XML declaration (default: false)
* `exclude_svg_width_and_height`: exclude width and height (default: false)
* `force_xlink_href`: forces xlink namespace in case of compatibility issues (default: false)
* `WebPWriter`
* `quality`: image quality (0-100, default: 80)
You can provide any writer options like this.
```php ```php
use Endroid\QrCode\Writer\SvgWriter; use Endroid\QrCode\Writer\SvgWriter;
$builder->setWriterOptions([ $builder->setWriterOptions([SvgWriter::WRITER_OPTION_EXCLUDE_XML_DECLARATION => true]);
SvgWriter::WRITER_OPTION_EXCLUDE_XML_DECLARATION => true
]);
``` ```
### Encoding ### Encoding
@ -176,14 +145,13 @@ size can result in additional padding to compensate for the rounding difference.
And finally the encoding (default UTF-8 to support large character sets) can be And finally the encoding (default UTF-8 to support large character sets) can be
set to `ISO-8859-1` if possible to improve readability. set to `ISO-8859-1` if possible to improve readability.
## Validating the generated QR code ## Built-in validation reader
If you need to be extra sure the QR code you generated is readable and contains You can enable the built-in validation reader (disabled by default) by calling
the exact data you requested you can enable the validation reader, which is setValidateResult(true). This validation reader does not guarantee that the QR
disabled by default. You can do this either via the builder or directly on any code will be readable by all readers but it helps you provide a minimum level
writer that supports validation. See the examples above. of quality. Take note that the validator can consume quite amount of additional
resources and it should be installed separately only if you use it.
Please note that validation affects performance so only use it in case of problems.
## Symfony integration ## Symfony integration

Binary file not shown.

Before

Width:  |  Height:  |  Size: 28 KiB

View File

@ -12,18 +12,15 @@
} }
], ],
"require": { "require": {
"php": "^8.0", "php": "^7.4||^8.0",
"bacon/bacon-qr-code": "^2.0.5" "bacon/bacon-qr-code": "^2.0.5"
}, },
"require-dev": { "require-dev": {
"ext-gd": "*", "ext-gd": "*",
"endroid/quality": "dev-master", "endroid/quality": "dev-master",
"khanamiryan/qrcode-detector-decoder": "^1.0.4||^2.0.2", "khanamiryan/qrcode-detector-decoder": "^1.0.4",
"setasign/fpdf": "^1.8.2" "setasign/fpdf": "^1.8.2"
}, },
"conflict": {
"khanamiryan/qrcode-detector-decoder": "^1.0.6"
},
"suggest": { "suggest": {
"ext-gd": "Enables you to write PNG images", "ext-gd": "Enables you to write PNG images",
"khanamiryan/qrcode-detector-decoder": "Enables you to use the image validator", "khanamiryan/qrcode-detector-decoder": "Enables you to use the image validator",
@ -44,9 +41,6 @@
"sort-packages": true, "sort-packages": true,
"preferred-install": { "preferred-install": {
"endroid/*": "source" "endroid/*": "source"
},
"allow-plugins": {
"endroid/installer": true
} }
}, },
"extra": { "extra": {

View File

@ -7,7 +7,6 @@ namespace Endroid\QrCode\Builder;
use Endroid\QrCode\Color\ColorInterface; use Endroid\QrCode\Color\ColorInterface;
use Endroid\QrCode\Encoding\EncodingInterface; use Endroid\QrCode\Encoding\EncodingInterface;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface; use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelInterface;
use Endroid\QrCode\Exception\ValidationException;
use Endroid\QrCode\Label\Alignment\LabelAlignmentInterface; use Endroid\QrCode\Label\Alignment\LabelAlignmentInterface;
use Endroid\QrCode\Label\Font\FontInterface; use Endroid\QrCode\Label\Font\FontInterface;
use Endroid\QrCode\Label\Label; use Endroid\QrCode\Label\Label;
@ -25,7 +24,7 @@ use Endroid\QrCode\Writer\WriterInterface;
class Builder implements BuilderInterface class Builder implements BuilderInterface
{ {
/** /**
* @var array<string, mixed>{ * @var array<mixed>{
* data: string, * data: string,
* writer: WriterInterface, * writer: WriterInterface,
* writerOptions: array, * writerOptions: array,
@ -78,7 +77,7 @@ class Builder implements BuilderInterface
return $this; return $this;
} }
/** @param array<string, mixed> $writerOptions */ /** @param array<mixed> $writerOptions */
public function writerOptions(array $writerOptions): BuilderInterface public function writerOptions(array $writerOptions): BuilderInterface
{ {
$this->options['writerOptions'] = $writerOptions; $this->options['writerOptions'] = $writerOptions;
@ -217,7 +216,7 @@ class Builder implements BuilderInterface
$writer = $this->options['writer']; $writer = $this->options['writer'];
if ($this->options['validateResult'] && !$writer instanceof ValidatingWriterInterface) { if ($this->options['validateResult'] && !$writer instanceof ValidatingWriterInterface) {
throw ValidationException::createForUnsupportedWriter(strval(get_class($writer))); throw new \Exception('Unable to validate result with '.get_class($writer));
} }
/** @var QrCode $qrCode */ /** @var QrCode $qrCode */
@ -243,7 +242,7 @@ class Builder implements BuilderInterface
* *
* @return mixed * @return mixed
*/ */
private function buildObject(string $class, string|null $optionsPrefix = null) private function buildObject(string $class, string $optionsPrefix = null)
{ {
/** @var \ReflectionClass<object> $reflectionClass */ /** @var \ReflectionClass<object> $reflectionClass */
$reflectionClass = new \ReflectionClass($class); $reflectionClass = new \ReflectionClass($class);

View File

@ -20,7 +20,7 @@ interface BuilderInterface
public function writer(WriterInterface $writer): BuilderInterface; public function writer(WriterInterface $writer): BuilderInterface;
/** @param array<string, mixed> $writerOptions */ /** @param array<mixed> $writerOptions */
public function writerOptions(array $writerOptions): BuilderInterface; public function writerOptions(array $writerOptions): BuilderInterface;
public function data(string $data): BuilderInterface; public function data(string $data): BuilderInterface;

View File

@ -6,12 +6,17 @@ namespace Endroid\QrCode\Color;
final class Color implements ColorInterface final class Color implements ColorInterface
{ {
public function __construct( private int $red;
private int $red, private int $green;
private int $green, private int $blue;
private int $blue, private int $alpha;
private int $alpha = 0
) { public function __construct(int $red, int $green, int $blue, int $alpha = 0)
{
$this->red = $red;
$this->green = $green;
$this->blue = $blue;
$this->alpha = $alpha;
} }
public function getRed(): int public function getRed(): int
@ -39,11 +44,6 @@ final class Color implements ColorInterface
return 1 - $this->alpha / 127; return 1 - $this->alpha / 127;
} }
public function getHex(): string
{
return sprintf('#%02x%02x%02x', $this->red, $this->green, $this->blue);
}
public function toArray(): array public function toArray(): array
{ {
return [ return [

View File

@ -16,8 +16,6 @@ interface ColorInterface
public function getOpacity(): float; public function getOpacity(): float;
public function getHex(): string;
/** @return array<string, int> */ /** @return array<string, int> */
public function toArray(): array; public function toArray(): array;
} }

View File

@ -6,12 +6,15 @@ namespace Endroid\QrCode\Encoding;
final class Encoding implements EncodingInterface final class Encoding implements EncodingInterface
{ {
public function __construct( private string $value;
private string $value
) { public function __construct(string $value)
{
if (!in_array($value, mb_list_encodings())) { if (!in_array($value, mb_list_encodings())) {
throw new \Exception(sprintf('Invalid encoding "%s"', $value)); throw new \Exception(sprintf('Invalid encoding "%s"', $value));
} }
$this->value = $value;
} }
public function __toString(): string public function __toString(): string

View File

@ -1,23 +0,0 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Exception;
final class ValidationException extends \Exception
{
public static function createForUnsupportedWriter(string $writerClass): self
{
return new self(sprintf('Unable to validate the result: "%s" does not support validation', $writerClass));
}
public static function createForMissingPackage(string $packageName): self
{
return new self(sprintf('Please install "%s" or disable image validation', $packageName));
}
public static function createForInvalidData(string $expectedData, string $actualData): self
{
return new self('The validation reader read "'.$actualData.'" instead of "'.$expectedData.'". Adjust your parameters to increase readability or disable validation.');
}
}

View File

@ -8,10 +8,13 @@ use Endroid\QrCode\Label\LabelInterface;
class LabelImageData class LabelImageData
{ {
private function __construct( private int $width;
private int $width, private int $height;
private int $height
) { private function __construct(int $width, int $height)
{
$this->width = $width;
$this->height = $height;
} }
public static function createForLabel(LabelInterface $label): self public static function createForLabel(LabelInterface $label): self

View File

@ -8,14 +8,31 @@ use Endroid\QrCode\Logo\LogoInterface;
class LogoImageData class LogoImageData
{ {
private string $data;
/** @var mixed */
private $image;
private string $mimeType;
private int $width;
private int $height;
private bool $punchoutBackground;
/** @param mixed $image */
private function __construct( private function __construct(
private string $data, string $data,
private \GdImage|null $image, $image,
private string $mimeType, string $mimeType,
private int $width, int $width,
private int $height, int $height,
private bool $punchoutBackground bool $punchoutBackground
) { ) {
$this->data = $data;
$this->image = $image;
$this->mimeType = $mimeType;
$this->width = $width;
$this->height = $height;
$this->punchoutBackground = $punchoutBackground;
} }
public static function createForLogo(LogoInterface $logo): self public static function createForLogo(LogoInterface $logo): self
@ -72,9 +89,10 @@ class LogoImageData
return $this->data; return $this->data;
} }
public function getImage(): \GdImage /** @return mixed */
public function getImage()
{ {
if (!$this->image instanceof \GdImage) { if (null === $this->image) {
throw new \Exception('SVG Images have no image resource'); throw new \Exception('SVG Images have no image resource');
} }
@ -108,7 +126,10 @@ class LogoImageData
private static function detectMimeTypeFromUrl(string $url): string private static function detectMimeTypeFromUrl(string $url): string
{ {
$headers = get_headers($url, true); /** @var mixed $format */
$format = PHP_VERSION_ID >= 80000 ? true : 1;
$headers = get_headers($url, $format);
if (!is_array($headers) || !isset($headers['Content-Type'])) { if (!is_array($headers) || !isset($headers['Content-Type'])) {
throw new \Exception(sprintf('Content type could not be determined for logo URL "%s"', $url)); throw new \Exception(sprintf('Content type could not be determined for logo URL "%s"', $url));

View File

@ -6,14 +6,18 @@ namespace Endroid\QrCode\Label\Font;
final class Font implements FontInterface final class Font implements FontInterface
{ {
public function __construct( private string $path;
private string $path, private int $size;
private int $size = 16
) { public function __construct(string $path, int $size = 16)
$this->assertValidPath($path); {
$this->validatePath($path);
$this->path = $path;
$this->size = $size;
} }
private function assertValidPath(string $path): void private function validatePath(string $path): void
{ {
if (!file_exists($path)) { if (!file_exists($path)) {
throw new \Exception(sprintf('Invalid font path "%s"', $path)); throw new \Exception(sprintf('Invalid font path "%s"', $path));

View File

@ -6,9 +6,11 @@ namespace Endroid\QrCode\Label\Font;
final class NotoSans implements FontInterface final class NotoSans implements FontInterface
{ {
public function __construct( private int $size;
private int $size = 16
) { public function __construct(int $size = 16)
{
$this->size = $size;
} }
public function getPath(): string public function getPath(): string

View File

@ -6,9 +6,11 @@ namespace Endroid\QrCode\Label\Font;
final class OpenSans implements FontInterface final class OpenSans implements FontInterface
{ {
public function __construct( private int $size;
private int $size = 16
) { public function __construct(int $size = 16)
{
$this->size = $size;
} }
public function getPath(): string public function getPath(): string

View File

@ -15,22 +15,24 @@ use Endroid\QrCode\Label\Margin\MarginInterface;
final class Label implements LabelInterface final class Label implements LabelInterface
{ {
private string $text;
private FontInterface $font; private FontInterface $font;
private LabelAlignmentInterface $alignment; private LabelAlignmentInterface $alignment;
private MarginInterface $margin; private MarginInterface $margin;
private ColorInterface $textColor; private ColorInterface $textColor;
public function __construct( public function __construct(
private string $text, string $text,
FontInterface|null $font = null, FontInterface $font = null,
LabelAlignmentInterface|null $alignment = null, LabelAlignmentInterface $alignment = null,
MarginInterface|null $margin = null, MarginInterface $margin = null,
ColorInterface|null $textColor = null ColorInterface $textColor = null
) { ) {
$this->font = $font ?? new Font(__DIR__.'/../../assets/noto_sans.otf', 16); $this->text = $text;
$this->alignment = $alignment ?? new LabelAlignmentCenter(); $this->font = isset($font) ? $font : new Font(__DIR__.'/../../assets/noto_sans.otf', 16);
$this->margin = $margin ?? new Margin(0, 10, 10, 10); $this->alignment = isset($alignment) ? $alignment : new LabelAlignmentCenter();
$this->textColor = $textColor ?? new Color(0, 0, 0); $this->margin = isset($margin) ? $margin : new Margin(0, 10, 10, 10);
$this->textColor = isset($textColor) ? $textColor : new Color(0, 0, 0);
} }
public static function create(string $text): self public static function create(string $text): self

View File

@ -6,12 +6,17 @@ namespace Endroid\QrCode\Label\Margin;
final class Margin implements MarginInterface final class Margin implements MarginInterface
{ {
public function __construct( private int $top;
private int $top, private int $right;
private int $right, private int $bottom;
private int $bottom, private int $left;
private int $left
) { public function __construct(int $top, int $right, int $bottom, int $left)
{
$this->top = $top;
$this->right = $right;
$this->bottom = $bottom;
$this->left = $left;
} }
public function getTop(): int public function getTop(): int

View File

@ -6,12 +6,17 @@ namespace Endroid\QrCode\Logo;
final class Logo implements LogoInterface final class Logo implements LogoInterface
{ {
public function __construct( private string $path;
private string $path, private ?int $resizeToWidth;
private int|null $resizeToWidth = null, private ?int $resizeToHeight;
private int|null $resizeToHeight = null, private bool $punchoutBackground;
private bool $punchoutBackground = false
) { public function __construct(string $path, ?int $resizeToWidth = null, ?int $resizeToHeight = null, bool $punchoutBackground = false)
{
$this->path = $path;
$this->resizeToWidth = $resizeToWidth;
$this->resizeToHeight = $resizeToHeight;
$this->punchoutBackground = $punchoutBackground;
} }
public static function create(string $path): self public static function create(string $path): self
@ -31,24 +36,24 @@ final class Logo implements LogoInterface
return $this; return $this;
} }
public function getResizeToWidth(): int|null public function getResizeToWidth(): ?int
{ {
return $this->resizeToWidth; return $this->resizeToWidth;
} }
public function setResizeToWidth(int|null $resizeToWidth): self public function setResizeToWidth(?int $resizeToWidth): self
{ {
$this->resizeToWidth = $resizeToWidth; $this->resizeToWidth = $resizeToWidth;
return $this; return $this;
} }
public function getResizeToHeight(): int|null public function getResizeToHeight(): ?int
{ {
return $this->resizeToHeight; return $this->resizeToHeight;
} }
public function setResizeToHeight(int|null $resizeToHeight): self public function setResizeToHeight(?int $resizeToHeight): self
{ {
$this->resizeToHeight = $resizeToHeight; $this->resizeToHeight = $resizeToHeight;

View File

@ -8,9 +8,9 @@ interface LogoInterface
{ {
public function getPath(): string; public function getPath(): string;
public function getResizeToWidth(): int|null; public function getResizeToWidth(): ?int;
public function getResizeToHeight(): int|null; public function getResizeToHeight(): ?int;
public function getPunchoutBackground(): bool; public function getPunchoutBackground(): bool;
} }

View File

@ -11,6 +11,9 @@ use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeShrink;
final class Matrix implements MatrixInterface final class Matrix implements MatrixInterface
{ {
/** @var array<int, array<int, int>> */
private array $blockValues = [];
private float $blockSize; private float $blockSize;
private int $innerSize; private int $innerSize;
private int $outerSize; private int $outerSize;
@ -18,12 +21,10 @@ final class Matrix implements MatrixInterface
private int $marginRight; private int $marginRight;
/** @param array<array<int>> $blockValues */ /** @param array<array<int>> $blockValues */
public function __construct( public function __construct(array $blockValues, int $size, int $margin, RoundBlockSizeModeInterface $roundBlockSizeMode)
private array $blockValues, {
int $size, $this->blockValues = $blockValues;
int $margin,
RoundBlockSizeModeInterface $roundBlockSizeMode
) {
$this->blockSize = $size / $this->getBlockCount(); $this->blockSize = $size / $this->getBlockCount();
$this->innerSize = $size; $this->innerSize = $size;
$this->outerSize = $size + 2 * $margin; $this->outerSize = $size + 2 * $margin;

View File

@ -15,24 +15,30 @@ use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
final class QrCode implements QrCodeInterface final class QrCode implements QrCodeInterface
{ {
private string $data;
private EncodingInterface $encoding; private EncodingInterface $encoding;
private ErrorCorrectionLevelInterface $errorCorrectionLevel; private ErrorCorrectionLevelInterface $errorCorrectionLevel;
private int $size;
private int $margin;
private RoundBlockSizeModeInterface $roundBlockSizeMode; private RoundBlockSizeModeInterface $roundBlockSizeMode;
private ColorInterface $foregroundColor; private ColorInterface $foregroundColor;
private ColorInterface $backgroundColor; private ColorInterface $backgroundColor;
public function __construct( public function __construct(
private string $data, string $data,
EncodingInterface|null $encoding = null, EncodingInterface $encoding = null,
ErrorCorrectionLevelInterface|null $errorCorrectionLevel = null, ErrorCorrectionLevelInterface $errorCorrectionLevel = null,
private int $size = 300, int $size = 300,
private int $margin = 10, int $margin = 10,
RoundBlockSizeModeInterface|null $roundBlockSizeMode = null, RoundBlockSizeModeInterface $roundBlockSizeMode = null,
ColorInterface|null $foregroundColor = null, ColorInterface $foregroundColor = null,
ColorInterface|null $backgroundColor = null ColorInterface $backgroundColor = null
) { ) {
$this->data = $data;
$this->encoding = $encoding ?? new Encoding('UTF-8'); $this->encoding = $encoding ?? new Encoding('UTF-8');
$this->errorCorrectionLevel = $errorCorrectionLevel ?? new ErrorCorrectionLevelLow(); $this->errorCorrectionLevel = $errorCorrectionLevel ?? new ErrorCorrectionLevelLow();
$this->size = $size;
$this->margin = $margin;
$this->roundBlockSizeMode = $roundBlockSizeMode ?? new RoundBlockSizeModeMargin(); $this->roundBlockSizeMode = $roundBlockSizeMode ?? new RoundBlockSizeModeMargin();
$this->foregroundColor = $foregroundColor ?? new Color(0, 0, 0); $this->foregroundColor = $foregroundColor ?? new Color(0, 0, 0);
$this->backgroundColor = $backgroundColor ?? new Color(255, 255, 255); $this->backgroundColor = $backgroundColor ?? new Color(255, 255, 255);

View File

@ -0,0 +1,7 @@
<?php
declare(strict_types=1);
interface WritableInterface
{
}

View File

@ -1,205 +0,0 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Writer;
use Endroid\QrCode\Bacon\MatrixFactory;
use Endroid\QrCode\Exception\ValidationException;
use Endroid\QrCode\ImageData\LabelImageData;
use Endroid\QrCode\ImageData\LogoImageData;
use Endroid\QrCode\Label\Alignment\LabelAlignmentLeft;
use Endroid\QrCode\Label\Alignment\LabelAlignmentRight;
use Endroid\QrCode\Label\LabelInterface;
use Endroid\QrCode\Logo\LogoInterface;
use Endroid\QrCode\QrCodeInterface;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeNone;
use Endroid\QrCode\Writer\Result\GdResult;
use Endroid\QrCode\Writer\Result\ResultInterface;
use Zxing\QrReader;
abstract class AbstractGdWriter implements WriterInterface, ValidatingWriterInterface
{
public function write(QrCodeInterface $qrCode, LogoInterface|null $logo = null, LabelInterface|null $label = null, array $options = []): ResultInterface
{
if (!extension_loaded('gd')) {
throw new \Exception('Unable to generate image: please check if the GD extension is enabled and configured correctly');
}
$matrixFactory = new MatrixFactory();
$matrix = $matrixFactory->create($qrCode);
$baseBlockSize = $qrCode->getRoundBlockSizeMode() instanceof RoundBlockSizeModeNone ? 10 : intval($matrix->getBlockSize());
$baseImage = imagecreatetruecolor($matrix->getBlockCount() * $baseBlockSize, $matrix->getBlockCount() * $baseBlockSize);
if (!$baseImage) {
throw new \Exception('Unable to generate image: please check if the GD extension is enabled and configured correctly');
}
/** @var int $foregroundColor */
$foregroundColor = imagecolorallocatealpha(
$baseImage,
$qrCode->getForegroundColor()->getRed(),
$qrCode->getForegroundColor()->getGreen(),
$qrCode->getForegroundColor()->getBlue(),
$qrCode->getForegroundColor()->getAlpha()
);
/** @var int $transparentColor */
$transparentColor = imagecolorallocatealpha($baseImage, 255, 255, 255, 127);
imagefill($baseImage, 0, 0, $transparentColor);
for ($rowIndex = 0; $rowIndex < $matrix->getBlockCount(); ++$rowIndex) {
for ($columnIndex = 0; $columnIndex < $matrix->getBlockCount(); ++$columnIndex) {
if (1 === $matrix->getBlockValue($rowIndex, $columnIndex)) {
imagefilledrectangle(
$baseImage,
$columnIndex * $baseBlockSize,
$rowIndex * $baseBlockSize,
($columnIndex + 1) * $baseBlockSize - 1,
($rowIndex + 1) * $baseBlockSize - 1,
$foregroundColor
);
}
}
}
$targetWidth = $matrix->getOuterSize();
$targetHeight = $matrix->getOuterSize();
if ($label instanceof LabelInterface) {
$labelImageData = LabelImageData::createForLabel($label);
$targetHeight += $labelImageData->getHeight() + $label->getMargin()->getTop() + $label->getMargin()->getBottom();
}
$targetImage = imagecreatetruecolor($targetWidth, $targetHeight);
if (!$targetImage) {
throw new \Exception('Unable to generate image: please check if the GD extension is enabled and configured correctly');
}
/** @var int $backgroundColor */
$backgroundColor = imagecolorallocatealpha(
$targetImage,
$qrCode->getBackgroundColor()->getRed(),
$qrCode->getBackgroundColor()->getGreen(),
$qrCode->getBackgroundColor()->getBlue(),
$qrCode->getBackgroundColor()->getAlpha()
);
imagefill($targetImage, 0, 0, $backgroundColor);
imagecopyresampled(
$targetImage,
$baseImage,
$matrix->getMarginLeft(),
$matrix->getMarginLeft(),
0,
0,
$matrix->getInnerSize(),
$matrix->getInnerSize(),
imagesx($baseImage),
imagesy($baseImage)
);
if ($qrCode->getBackgroundColor()->getAlpha() > 0) {
imagesavealpha($targetImage, true);
}
$result = new GdResult($matrix, $targetImage);
if ($logo instanceof LogoInterface) {
$result = $this->addLogo($logo, $result);
}
if ($label instanceof LabelInterface) {
$result = $this->addLabel($label, $result);
}
return $result;
}
private function addLogo(LogoInterface $logo, GdResult $result): GdResult
{
$logoImageData = LogoImageData::createForLogo($logo);
if ('image/svg+xml' === $logoImageData->getMimeType()) {
throw new \Exception('PNG Writer does not support SVG logo');
}
$targetImage = $result->getImage();
$matrix = $result->getMatrix();
if ($logoImageData->getPunchoutBackground()) {
/** @var int $transparent */
$transparent = imagecolorallocatealpha($targetImage, 255, 255, 255, 127);
imagealphablending($targetImage, false);
$xOffsetStart = intval($matrix->getOuterSize() / 2 - $logoImageData->getWidth() / 2);
$yOffsetStart = intval($matrix->getOuterSize() / 2 - $logoImageData->getHeight() / 2);
for ($xOffset = $xOffsetStart; $xOffset < $xOffsetStart + $logoImageData->getWidth(); ++$xOffset) {
for ($yOffset = $yOffsetStart; $yOffset < $yOffsetStart + $logoImageData->getHeight(); ++$yOffset) {
imagesetpixel($targetImage, $xOffset, $yOffset, $transparent);
}
}
}
imagecopyresampled(
$targetImage,
$logoImageData->getImage(),
intval($matrix->getOuterSize() / 2 - $logoImageData->getWidth() / 2),
intval($matrix->getOuterSize() / 2 - $logoImageData->getHeight() / 2),
0,
0,
$logoImageData->getWidth(),
$logoImageData->getHeight(),
imagesx($logoImageData->getImage()),
imagesy($logoImageData->getImage())
);
return new GdResult($matrix, $targetImage);
}
private function addLabel(LabelInterface $label, GdResult $result): GdResult
{
$targetImage = $result->getImage();
$labelImageData = LabelImageData::createForLabel($label);
/** @var int $textColor */
$textColor = imagecolorallocatealpha(
$targetImage,
$label->getTextColor()->getRed(),
$label->getTextColor()->getGreen(),
$label->getTextColor()->getBlue(),
$label->getTextColor()->getAlpha()
);
$x = intval(imagesx($targetImage) / 2 - $labelImageData->getWidth() / 2);
$y = imagesy($targetImage) - $label->getMargin()->getBottom();
if ($label->getAlignment() instanceof LabelAlignmentLeft) {
$x = $label->getMargin()->getLeft();
} elseif ($label->getAlignment() instanceof LabelAlignmentRight) {
$x = imagesx($targetImage) - $labelImageData->getWidth() - $label->getMargin()->getRight();
}
imagettftext($targetImage, $label->getFont()->getSize(), 0, $x, $y, $textColor, $label->getFont()->getPath(), $label->getText());
return new GdResult($result->getMatrix(), $targetImage);
}
public function validateResult(ResultInterface $result, string $expectedData): void
{
$string = $result->getString();
if (!class_exists(QrReader::class)) {
throw ValidationException::createForMissingPackage('khanamiryan/qrcode-detector-decoder');
}
$reader = new QrReader($string, QrReader::SOURCE_TYPE_BLOB);
if ($reader->text() !== $expectedData) {
throw ValidationException::createForInvalidData($expectedData, strval($reader->text()));
}
}
}

View File

@ -13,7 +13,7 @@ use Endroid\QrCode\Writer\Result\ResultInterface;
final class BinaryWriter implements WriterInterface final class BinaryWriter implements WriterInterface
{ {
public function write(QrCodeInterface $qrCode, LogoInterface|null $logo = null, LabelInterface|null $label = null, array $options = []): ResultInterface public function write(QrCodeInterface $qrCode, LogoInterface $logo = null, LabelInterface $label = null, array $options = []): ResultInterface
{ {
$matrixFactory = new MatrixFactory(); $matrixFactory = new MatrixFactory();
$matrix = $matrixFactory->create($qrCode); $matrix = $matrixFactory->create($qrCode);

View File

@ -1,23 +0,0 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Writer;
use Endroid\QrCode\Bacon\MatrixFactory;
use Endroid\QrCode\Label\LabelInterface;
use Endroid\QrCode\Logo\LogoInterface;
use Endroid\QrCode\QrCodeInterface;
use Endroid\QrCode\Writer\Result\ConsoleResult;
use Endroid\QrCode\Writer\Result\ResultInterface;
final class ConsoleWriter implements WriterInterface
{
public function write(QrCodeInterface $qrCode, LogoInterface|null $logo = null, LabelInterface|null $label = null, $options = []): ResultInterface
{
$matrixFactory = new MatrixFactory();
$matrix = $matrixFactory->create($qrCode);
return new ConsoleResult($matrix, $qrCode->getForegroundColor(), $qrCode->getBackgroundColor());
}
}

View File

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Endroid\QrCode\Writer; namespace Endroid\QrCode\Writer;
use Endroid\QrCode\Bacon\MatrixFactory;
use Endroid\QrCode\Label\LabelInterface; use Endroid\QrCode\Label\LabelInterface;
use Endroid\QrCode\Logo\LogoInterface; use Endroid\QrCode\Logo\LogoInterface;
use Endroid\QrCode\QrCodeInterface; use Endroid\QrCode\QrCodeInterface;
@ -13,12 +12,9 @@ use Endroid\QrCode\Writer\Result\ResultInterface;
final class DebugWriter implements WriterInterface, ValidatingWriterInterface final class DebugWriter implements WriterInterface, ValidatingWriterInterface
{ {
public function write(QrCodeInterface $qrCode, LogoInterface|null $logo = null, LabelInterface|null $label = null, array $options = []): ResultInterface public function write(QrCodeInterface $qrCode, LogoInterface $logo = null, LabelInterface $label = null, array $options = []): ResultInterface
{ {
$matrixFactory = new MatrixFactory(); return new DebugResult($qrCode, $logo, $label, $options);
$matrix = $matrixFactory->create($qrCode);
return new DebugResult($matrix, $qrCode, $logo, $label, $options);
} }
public function validateResult(ResultInterface $result, string $expectedData): void public function validateResult(ResultInterface $result, string $expectedData): void

View File

@ -15,7 +15,7 @@ final class EpsWriter implements WriterInterface
{ {
public const DECIMAL_PRECISION = 10; public const DECIMAL_PRECISION = 10;
public function write(QrCodeInterface $qrCode, LogoInterface|null $logo = null, LabelInterface|null $label = null, array $options = []): ResultInterface public function write(QrCodeInterface $qrCode, LogoInterface $logo = null, LabelInterface $label = null, array $options = []): ResultInterface
{ {
$matrixFactory = new MatrixFactory(); $matrixFactory = new MatrixFactory();
$matrix = $matrixFactory->create($qrCode); $matrix = $matrixFactory->create($qrCode);
@ -39,6 +39,6 @@ final class EpsWriter implements WriterInterface
} }
} }
return new EpsResult($matrix, $lines); return new EpsResult($lines);
} }
} }

View File

@ -1,23 +0,0 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Writer;
use Endroid\QrCode\Label\LabelInterface;
use Endroid\QrCode\Logo\LogoInterface;
use Endroid\QrCode\QrCodeInterface;
use Endroid\QrCode\Writer\Result\GdResult;
use Endroid\QrCode\Writer\Result\GifResult;
use Endroid\QrCode\Writer\Result\ResultInterface;
final class GifWriter extends AbstractGdWriter
{
public function write(QrCodeInterface $qrCode, LogoInterface|null $logo = null, LabelInterface|null $label = null, array $options = []): ResultInterface
{
/** @var GdResult $gdResult */
$gdResult = parent::write($qrCode, $logo, $label, $options);
return new GifResult($gdResult->getMatrix(), $gdResult->getImage());
}
}

View File

@ -17,9 +17,8 @@ final class PdfWriter implements WriterInterface
public const WRITER_OPTION_PDF = 'fpdf'; public const WRITER_OPTION_PDF = 'fpdf';
public const WRITER_OPTION_X = 'x'; public const WRITER_OPTION_X = 'x';
public const WRITER_OPTION_Y = 'y'; public const WRITER_OPTION_Y = 'y';
public const WRITER_OPTION_LINK = 'link';
public function write(QrCodeInterface $qrCode, LogoInterface|null $logo = null, LabelInterface|null $label = null, array $options = []): ResultInterface public function write(QrCodeInterface $qrCode, LogoInterface $logo = null, LabelInterface $label = null, array $options = []): ResultInterface
{ {
$matrixFactory = new MatrixFactory(); $matrixFactory = new MatrixFactory();
$matrix = $matrixFactory->create($qrCode); $matrix = $matrixFactory->create($qrCode);
@ -100,12 +99,7 @@ final class PdfWriter implements WriterInterface
$fpdf->Cell($matrix->getOuterSize(), 0, $label->getText(), 0, 0, 'C'); $fpdf->Cell($matrix->getOuterSize(), 0, $label->getText(), 0, 0, 'C');
} }
if (isset($options[self::WRITER_OPTION_LINK])) { return new PdfResult($fpdf);
$link = $options[self::WRITER_OPTION_LINK];
$fpdf->Link($x, $y, $x + $matrix->getOuterSize(), $y + $matrix->getOuterSize(), $link);
}
return new PdfResult($matrix, $fpdf);
} }
private function addLogo(LogoInterface $logo, \FPDF $fpdf, float $x, float $y, float $size): void private function addLogo(LogoInterface $logo, \FPDF $fpdf, float $x, float $y, float $size): void

View File

@ -4,26 +4,224 @@ declare(strict_types=1);
namespace Endroid\QrCode\Writer; namespace Endroid\QrCode\Writer;
use Endroid\QrCode\Bacon\MatrixFactory;
use Endroid\QrCode\ImageData\LabelImageData;
use Endroid\QrCode\ImageData\LogoImageData;
use Endroid\QrCode\Label\Alignment\LabelAlignmentLeft;
use Endroid\QrCode\Label\Alignment\LabelAlignmentRight;
use Endroid\QrCode\Label\LabelInterface; use Endroid\QrCode\Label\LabelInterface;
use Endroid\QrCode\Logo\LogoInterface; use Endroid\QrCode\Logo\LogoInterface;
use Endroid\QrCode\QrCodeInterface; use Endroid\QrCode\QrCodeInterface;
use Endroid\QrCode\Writer\Result\GdResult; use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeNone;
use Endroid\QrCode\Writer\Result\PngResult; use Endroid\QrCode\Writer\Result\PngResult;
use Endroid\QrCode\Writer\Result\ResultInterface; use Endroid\QrCode\Writer\Result\ResultInterface;
use Zxing\QrReader;
final class PngWriter extends AbstractGdWriter final class PngWriter implements WriterInterface, ValidatingWriterInterface
{ {
public const WRITER_OPTION_COMPRESSION_LEVEL = 'compression_level'; public function write(QrCodeInterface $qrCode, LogoInterface $logo = null, LabelInterface $label = null, array $options = []): ResultInterface
public function write(QrCodeInterface $qrCode, LogoInterface|null $logo = null, LabelInterface|null $label = null, array $options = []): ResultInterface
{ {
if (!isset($options[self::WRITER_OPTION_COMPRESSION_LEVEL])) { if (!extension_loaded('gd')) {
$options[self::WRITER_OPTION_COMPRESSION_LEVEL] = -1; throw new \Exception('Unable to generate image: please check if the GD extension is enabled and configured correctly');
} }
/** @var GdResult $gdResult */ $matrixFactory = new MatrixFactory();
$gdResult = parent::write($qrCode, $logo, $label, $options); $matrix = $matrixFactory->create($qrCode);
return new PngResult($gdResult->getMatrix(), $gdResult->getImage(), $options[self::WRITER_OPTION_COMPRESSION_LEVEL]); $baseBlockSize = $qrCode->getRoundBlockSizeMode() instanceof RoundBlockSizeModeNone ? 10 : intval($matrix->getBlockSize());
$baseImage = imagecreatetruecolor($matrix->getBlockCount() * $baseBlockSize, $matrix->getBlockCount() * $baseBlockSize);
if (!$baseImage) {
throw new \Exception('Unable to generate image: please check if the GD extension is enabled and configured correctly');
}
/** @var int $foregroundColor */
$foregroundColor = imagecolorallocatealpha(
$baseImage,
$qrCode->getForegroundColor()->getRed(),
$qrCode->getForegroundColor()->getGreen(),
$qrCode->getForegroundColor()->getBlue(),
$qrCode->getForegroundColor()->getAlpha()
);
/** @var int $transparentColor */
$transparentColor = imagecolorallocatealpha($baseImage, 255, 255, 255, 127);
imagefill($baseImage, 0, 0, $transparentColor);
for ($rowIndex = 0; $rowIndex < $matrix->getBlockCount(); ++$rowIndex) {
for ($columnIndex = 0; $columnIndex < $matrix->getBlockCount(); ++$columnIndex) {
if (1 === $matrix->getBlockValue($rowIndex, $columnIndex)) {
imagefilledrectangle(
$baseImage,
$columnIndex * $baseBlockSize,
$rowIndex * $baseBlockSize,
($columnIndex + 1) * $baseBlockSize - 1,
($rowIndex + 1) * $baseBlockSize - 1,
$foregroundColor
);
}
}
}
$targetWidth = $matrix->getOuterSize();
$targetHeight = $matrix->getOuterSize();
if ($label instanceof LabelInterface) {
$labelImageData = LabelImageData::createForLabel($label);
$targetHeight += $labelImageData->getHeight() + $label->getMargin()->getTop() + $label->getMargin()->getBottom();
}
$targetImage = imagecreatetruecolor($targetWidth, $targetHeight);
if (!$targetImage) {
throw new \Exception('Unable to generate image: please check if the GD extension is enabled and configured correctly');
}
/** @var int $backgroundColor */
$backgroundColor = imagecolorallocatealpha(
$targetImage,
$qrCode->getBackgroundColor()->getRed(),
$qrCode->getBackgroundColor()->getGreen(),
$qrCode->getBackgroundColor()->getBlue(),
$qrCode->getBackgroundColor()->getAlpha()
);
imagefill($targetImage, 0, 0, $backgroundColor);
imagecopyresampled(
$targetImage,
$baseImage,
$matrix->getMarginLeft(),
$matrix->getMarginLeft(),
0,
0,
$matrix->getInnerSize(),
$matrix->getInnerSize(),
imagesx($baseImage),
imagesy($baseImage)
);
if (PHP_VERSION_ID < 80000) {
imagedestroy($baseImage);
}
if ($qrCode->getBackgroundColor()->getAlpha() > 0) {
imagesavealpha($targetImage, true);
}
$result = new PngResult($targetImage);
if ($logo instanceof LogoInterface) {
$result = $this->addLogo($logo, $result);
}
if ($label instanceof LabelInterface) {
$result = $this->addLabel($label, $result);
}
return $result;
}
private function addLogo(LogoInterface $logo, PngResult $result): PngResult
{
$logoImageData = LogoImageData::createForLogo($logo);
if ('image/svg+xml' === $logoImageData->getMimeType()) {
throw new \Exception('PNG Writer does not support SVG logo');
}
$targetImage = $result->getImage();
if ($logoImageData->getPunchoutBackground()) {
/** @var int $transparent */
$transparent = imagecolorallocatealpha($targetImage, 255, 255, 255, 127);
imagealphablending($targetImage, false);
for (
$x_offset = intval(imagesx($targetImage) / 2 - $logoImageData->getWidth() / 2);
$x_offset < intval(imagesx($targetImage) / 2 - $logoImageData->getWidth() / 2) + $logoImageData->getWidth();
++$x_offset
) {
for (
$y_offset = intval(imagesy($targetImage) / 2 - $logoImageData->getHeight() / 2);
$y_offset < intval(imagesy($targetImage) / 2 - $logoImageData->getHeight() / 2) + $logoImageData->getHeight();
++$y_offset
) {
imagesetpixel(
$targetImage,
$x_offset,
$y_offset,
$transparent
);
}
}
}
imagecopyresampled(
$targetImage,
$logoImageData->getImage(),
intval(imagesx($targetImage) / 2 - $logoImageData->getWidth() / 2),
intval(imagesx($targetImage) / 2 - $logoImageData->getHeight() / 2),
0,
0,
$logoImageData->getWidth(),
$logoImageData->getHeight(),
imagesx($logoImageData->getImage()),
imagesy($logoImageData->getImage())
);
if (PHP_VERSION_ID < 80000) {
imagedestroy($logoImageData->getImage());
}
return new PngResult($targetImage);
}
private function addLabel(LabelInterface $label, PngResult $result): PngResult
{
$targetImage = $result->getImage();
$labelImageData = LabelImageData::createForLabel($label);
/** @var int $textColor */
$textColor = imagecolorallocatealpha(
$targetImage,
$label->getTextColor()->getRed(),
$label->getTextColor()->getGreen(),
$label->getTextColor()->getBlue(),
$label->getTextColor()->getAlpha()
);
$x = intval(imagesx($targetImage) / 2 - $labelImageData->getWidth() / 2);
$y = imagesy($targetImage) - $label->getMargin()->getBottom();
if ($label->getAlignment() instanceof LabelAlignmentLeft) {
$x = $label->getMargin()->getLeft();
} elseif ($label->getAlignment() instanceof LabelAlignmentRight) {
$x = imagesx($targetImage) - $labelImageData->getWidth() - $label->getMargin()->getRight();
}
imagettftext($targetImage, $label->getFont()->getSize(), 0, $x, $y, $textColor, $label->getFont()->getPath(), $label->getText());
return new PngResult($targetImage);
}
public function validateResult(ResultInterface $result, string $expectedData): void
{
$string = $result->getString();
if (!class_exists(QrReader::class)) {
throw new \Exception('Please install khanamiryan/qrcode-detector-decoder or disable image validation');
}
if (PHP_VERSION_ID >= 80000) {
throw new \Exception('The validator is not compatible with PHP 8 yet, see https://github.com/khanamiryan/php-qrcode-detector-decoder/pull/103');
}
$reader = new QrReader($string, QrReader::SOURCE_TYPE_BLOB);
if ($reader->text() !== $expectedData) {
throw new \Exception('Built-in validation reader read "'.$reader->text().'" instead of "'.$expectedData.'".
Adjust your parameters to increase readability or disable built-in validation.');
}
} }
} }

View File

@ -4,20 +4,8 @@ declare(strict_types=1);
namespace Endroid\QrCode\Writer\Result; namespace Endroid\QrCode\Writer\Result;
use Endroid\QrCode\Matrix\MatrixInterface;
abstract class AbstractResult implements ResultInterface abstract class AbstractResult implements ResultInterface
{ {
public function __construct(
private MatrixInterface $matrix
) {
}
public function getMatrix(): MatrixInterface
{
return $this->matrix;
}
public function getDataUri(): string public function getDataUri(): string
{ {
return 'data:'.$this->getMimeType().';base64,'.base64_encode($this->getString()); return 'data:'.$this->getMimeType().';base64,'.base64_encode($this->getString());

View File

@ -8,19 +8,19 @@ use Endroid\QrCode\Matrix\MatrixInterface;
final class BinaryResult extends AbstractResult final class BinaryResult extends AbstractResult
{ {
private MatrixInterface $matrix;
public function __construct(MatrixInterface $matrix) public function __construct(MatrixInterface $matrix)
{ {
parent::__construct($matrix); $this->matrix = $matrix;
} }
public function getString(): string public function getString(): string
{ {
$matrix = $this->getMatrix();
$binaryString = ''; $binaryString = '';
for ($rowIndex = 0; $rowIndex < $matrix->getBlockCount(); ++$rowIndex) { for ($rowIndex = 0; $rowIndex < $this->matrix->getBlockCount(); ++$rowIndex) {
for ($columnIndex = 0; $columnIndex < $matrix->getBlockCount(); ++$columnIndex) { for ($columnIndex = 0; $columnIndex < $this->matrix->getBlockCount(); ++$columnIndex) {
$binaryString .= $matrix->getBlockValue($rowIndex, $columnIndex); $binaryString .= $this->matrix->getBlockValue($rowIndex, $columnIndex);
} }
$binaryString .= "\n"; $binaryString .= "\n";
} }

View File

@ -1,69 +0,0 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Writer\Result;
use Endroid\QrCode\Color\ColorInterface;
use Endroid\QrCode\Matrix\MatrixInterface;
class ConsoleResult extends AbstractResult
{
private const TWO_BLOCKS = [
0 => ' ',
1 => "\xe2\x96\x80",
2 => "\xe2\x96\x84",
3 => "\xe2\x96\x88",
];
private string $colorEscapeCode;
public function __construct(
MatrixInterface $matrix,
ColorInterface $foreground,
ColorInterface $background
) {
parent::__construct($matrix);
$this->colorEscapeCode = sprintf(
"\e[38;2;%d;%d;%dm\e[48;2;%d;%d;%dm",
$foreground->getRed(),
$foreground->getGreen(),
$foreground->getBlue(),
$background->getRed(),
$background->getGreen(),
$background->getBlue()
);
}
public function getMimeType(): string
{
return 'text/plain';
}
public function getString(): string
{
$matrix = $this->getMatrix();
$side = $matrix->getBlockCount();
$marginLeft = $this->colorEscapeCode.self::TWO_BLOCKS[0].self::TWO_BLOCKS[0];
$marginRight = self::TWO_BLOCKS[0].self::TWO_BLOCKS[0]."\e[0m".PHP_EOL;
$marginVertical = $marginLeft.str_repeat(self::TWO_BLOCKS[0], $side).$marginRight;
$qrCodeString = $marginVertical;
for ($rowIndex = 0; $rowIndex < $side; $rowIndex += 2) {
$qrCodeString .= $marginLeft;
for ($columnIndex = 0; $columnIndex < $side; ++$columnIndex) {
$combined = $matrix->getBlockValue($rowIndex, $columnIndex);
if ($rowIndex + 1 < $side) {
$combined |= $matrix->getBlockValue($rowIndex + 1, $columnIndex) << 1;
}
$qrCodeString .= self::TWO_BLOCKS[$combined];
}
$qrCodeString .= $marginRight;
}
$qrCodeString .= $marginVertical;
return $qrCodeString;
}
}

View File

@ -6,22 +6,26 @@ namespace Endroid\QrCode\Writer\Result;
use Endroid\QrCode\Label\LabelInterface; use Endroid\QrCode\Label\LabelInterface;
use Endroid\QrCode\Logo\LogoInterface; use Endroid\QrCode\Logo\LogoInterface;
use Endroid\QrCode\Matrix\MatrixInterface;
use Endroid\QrCode\QrCodeInterface; use Endroid\QrCode\QrCodeInterface;
final class DebugResult extends AbstractResult final class DebugResult extends AbstractResult
{ {
private QrCodeInterface $qrCode;
private ?LogoInterface $logo;
private ?LabelInterface $label;
/** @var array<mixed> */
private array $options;
private bool $validateResult = false; private bool $validateResult = false;
public function __construct( /** @param array<mixed> $options */
MatrixInterface $matrix, public function __construct(QrCodeInterface $qrCode, LogoInterface $logo = null, LabelInterface $label = null, array $options = [])
private QrCodeInterface $qrCode, {
private LogoInterface|null $logo = null, $this->qrCode = $qrCode;
private LabelInterface|null $label = null, $this->logo = $logo;
/** @var array<string, mixed> $options */ $this->label = $label;
private array $options = [] $this->options = $options;
) {
parent::__construct($matrix);
} }
public function setValidateResult(bool $validateResult): void public function setValidateResult(bool $validateResult): void

View File

@ -4,16 +4,15 @@ declare(strict_types=1);
namespace Endroid\QrCode\Writer\Result; namespace Endroid\QrCode\Writer\Result;
use Endroid\QrCode\Matrix\MatrixInterface;
final class EpsResult extends AbstractResult final class EpsResult extends AbstractResult
{ {
public function __construct( /** @var array<string> */
MatrixInterface $matrix, private array $lines;
/** @var array<string> $lines */
private array $lines /** @param array<string> $lines */
) { public function __construct(array $lines)
parent::__construct($matrix); {
$this->lines = $lines;
} }
public function getString(): string public function getString(): string

View File

@ -1,32 +0,0 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Writer\Result;
use Endroid\QrCode\Matrix\MatrixInterface;
class GdResult extends AbstractResult
{
public function __construct(
MatrixInterface $matrix,
protected \GdImage $image
) {
parent::__construct($matrix);
}
public function getImage(): \GdImage
{
return $this->image;
}
public function getString(): string
{
throw new \Exception('You can only use this method in a concrete implementation');
}
public function getMimeType(): string
{
throw new \Exception('You can only use this method in a concrete implementation');
}
}

View File

@ -1,21 +0,0 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Writer\Result;
final class GifResult extends GdResult
{
public function getString(): string
{
ob_start();
imagegif($this->image);
return strval(ob_get_clean());
}
public function getMimeType(): string
{
return 'image/gif';
}
}

View File

@ -4,15 +4,13 @@ declare(strict_types=1);
namespace Endroid\QrCode\Writer\Result; namespace Endroid\QrCode\Writer\Result;
use Endroid\QrCode\Matrix\MatrixInterface;
final class PdfResult extends AbstractResult final class PdfResult extends AbstractResult
{ {
public function __construct( private \FPDF $fpdf;
MatrixInterface $matrix,
private \FPDF $fpdf public function __construct(\FPDF $fpdf)
) { {
parent::__construct($matrix); $this->fpdf = $fpdf;
} }
public function getPdf(): \FPDF public function getPdf(): \FPDF

View File

@ -4,22 +4,27 @@ declare(strict_types=1);
namespace Endroid\QrCode\Writer\Result; namespace Endroid\QrCode\Writer\Result;
use Endroid\QrCode\Matrix\MatrixInterface; final class PngResult extends AbstractResult
final class PngResult extends GdResult
{ {
private int $quality; /** @var mixed */
private $image;
public function __construct(MatrixInterface $matrix, \GdImage $image, int $quality = -1) /** @param mixed $image */
public function __construct($image)
{ {
parent::__construct($matrix, $image); $this->image = $image;
$this->quality = $quality; }
/** @return mixed */
public function getImage()
{
return $this->image;
} }
public function getString(): string public function getString(): string
{ {
ob_start(); ob_start();
imagepng($this->image, quality: $this->quality); imagepng($this->image);
return strval(ob_get_clean()); return strval(ob_get_clean());
} }

View File

@ -4,12 +4,8 @@ declare(strict_types=1);
namespace Endroid\QrCode\Writer\Result; namespace Endroid\QrCode\Writer\Result;
use Endroid\QrCode\Matrix\MatrixInterface;
interface ResultInterface interface ResultInterface
{ {
public function getMatrix(): MatrixInterface;
public function getString(): string; public function getString(): string;
public function getDataUri(): string; public function getDataUri(): string;

View File

@ -4,16 +4,15 @@ declare(strict_types=1);
namespace Endroid\QrCode\Writer\Result; namespace Endroid\QrCode\Writer\Result;
use Endroid\QrCode\Matrix\MatrixInterface;
final class SvgResult extends AbstractResult final class SvgResult extends AbstractResult
{ {
public function __construct( private \SimpleXMLElement $xml;
MatrixInterface $matrix, private bool $excludeXmlDeclaration;
private \SimpleXMLElement $xml,
private bool $excludeXmlDeclaration = false public function __construct(\SimpleXMLElement $xml, bool $excludeXmlDeclaration = false)
) { {
parent::__construct($matrix); $this->xml = $xml;
$this->excludeXmlDeclaration = $excludeXmlDeclaration;
} }
public function getXml(): \SimpleXMLElement public function getXml(): \SimpleXMLElement

View File

@ -1,35 +0,0 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Writer\Result;
use Endroid\QrCode\Matrix\MatrixInterface;
final class WebPResult extends GdResult
{
private int $quality;
public function __construct(MatrixInterface $matrix, \GdImage $image, int $quality = -1)
{
parent::__construct($matrix, $image);
$this->quality = $quality;
}
public function getString(): string
{
if (!function_exists('imagewebp')) {
throw new \Exception('WebP support is not available in your GD installation');
}
ob_start();
imagewebp($this->image, quality: $this->quality);
return strval(ob_get_clean());
}
public function getMimeType(): string
{
return 'image/webp';
}
}

View File

@ -17,10 +17,9 @@ final class SvgWriter implements WriterInterface
public const DECIMAL_PRECISION = 10; public const DECIMAL_PRECISION = 10;
public const WRITER_OPTION_BLOCK_ID = 'block_id'; public const WRITER_OPTION_BLOCK_ID = 'block_id';
public const WRITER_OPTION_EXCLUDE_XML_DECLARATION = 'exclude_xml_declaration'; public const WRITER_OPTION_EXCLUDE_XML_DECLARATION = 'exclude_xml_declaration';
public const WRITER_OPTION_EXCLUDE_SVG_WIDTH_AND_HEIGHT = 'exclude_svg_width_and_height';
public const WRITER_OPTION_FORCE_XLINK_HREF = 'force_xlink_href'; public const WRITER_OPTION_FORCE_XLINK_HREF = 'force_xlink_href';
public function write(QrCodeInterface $qrCode, LogoInterface|null $logo = null, LabelInterface|null $label = null, array $options = []): ResultInterface public function write(QrCodeInterface $qrCode, LogoInterface $logo = null, LabelInterface $label = null, array $options = []): ResultInterface
{ {
if (!isset($options[self::WRITER_OPTION_BLOCK_ID])) { if (!isset($options[self::WRITER_OPTION_BLOCK_ID])) {
$options[self::WRITER_OPTION_BLOCK_ID] = 'block'; $options[self::WRITER_OPTION_BLOCK_ID] = 'block';
@ -30,19 +29,13 @@ final class SvgWriter implements WriterInterface
$options[self::WRITER_OPTION_EXCLUDE_XML_DECLARATION] = false; $options[self::WRITER_OPTION_EXCLUDE_XML_DECLARATION] = false;
} }
if (!isset($options[self::WRITER_OPTION_EXCLUDE_SVG_WIDTH_AND_HEIGHT])) {
$options[self::WRITER_OPTION_EXCLUDE_SVG_WIDTH_AND_HEIGHT] = false;
}
$matrixFactory = new MatrixFactory(); $matrixFactory = new MatrixFactory();
$matrix = $matrixFactory->create($qrCode); $matrix = $matrixFactory->create($qrCode);
$xml = new \SimpleXMLElement('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"/>'); $xml = new \SimpleXMLElement('<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"/>');
$xml->addAttribute('version', '1.1'); $xml->addAttribute('version', '1.1');
if (!$options[self::WRITER_OPTION_EXCLUDE_SVG_WIDTH_AND_HEIGHT]) {
$xml->addAttribute('width', $matrix->getOuterSize().'px'); $xml->addAttribute('width', $matrix->getOuterSize().'px');
$xml->addAttribute('height', $matrix->getOuterSize().'px'); $xml->addAttribute('height', $matrix->getOuterSize().'px');
}
$xml->addAttribute('viewBox', '0 0 '.$matrix->getOuterSize().' '.$matrix->getOuterSize()); $xml->addAttribute('viewBox', '0 0 '.$matrix->getOuterSize().' '.$matrix->getOuterSize());
$xml->addChild('defs'); $xml->addChild('defs');
@ -72,7 +65,7 @@ final class SvgWriter implements WriterInterface
} }
} }
$result = new SvgResult($matrix, $xml, boolval($options[self::WRITER_OPTION_EXCLUDE_XML_DECLARATION])); $result = new SvgResult($xml, boolval($options[self::WRITER_OPTION_EXCLUDE_XML_DECLARATION]));
if ($logo instanceof LogoInterface) { if ($logo instanceof LogoInterface) {
$this->addLogo($logo, $result, $options); $this->addLogo($logo, $result, $options);
@ -81,7 +74,7 @@ final class SvgWriter implements WriterInterface
return $result; return $result;
} }
/** @param array<string, mixed> $options */ /** @param array<mixed> $options */
private function addLogo(LogoInterface $logo, SvgResult $result, array $options): void private function addLogo(LogoInterface $logo, SvgResult $result, array $options): void
{ {
$logoImageData = LogoImageData::createForLogo($logo); $logoImageData = LogoImageData::createForLogo($logo);

View File

@ -1,29 +0,0 @@
<?php
declare(strict_types=1);
namespace Endroid\QrCode\Writer;
use Endroid\QrCode\Label\LabelInterface;
use Endroid\QrCode\Logo\LogoInterface;
use Endroid\QrCode\QrCodeInterface;
use Endroid\QrCode\Writer\Result\GdResult;
use Endroid\QrCode\Writer\Result\ResultInterface;
use Endroid\QrCode\Writer\Result\WebPResult;
final class WebPWriter extends AbstractGdWriter
{
public const WRITER_OPTION_QUALITY = 'quality';
public function write(QrCodeInterface $qrCode, LogoInterface|null $logo = null, LabelInterface|null $label = null, array $options = []): ResultInterface
{
if (!isset($options[self::WRITER_OPTION_QUALITY])) {
$options[self::WRITER_OPTION_QUALITY] = -1;
}
/** @var GdResult $gdResult */
$gdResult = parent::write($qrCode, $logo, $label, $options);
return new WebPResult($gdResult->getMatrix(), $gdResult->getImage(), $options[self::WRITER_OPTION_QUALITY]);
}
}

View File

@ -11,6 +11,6 @@ use Endroid\QrCode\Writer\Result\ResultInterface;
interface WriterInterface interface WriterInterface
{ {
/** @param array<string, mixed> $options */ /** @param array<mixed> $options */
public function write(QrCodeInterface $qrCode, LogoInterface|null $logo = null, LabelInterface|null $label = null, array $options = []): ResultInterface; public function write(QrCodeInterface $qrCode, LogoInterface $logo = null, LabelInterface $label = null, array $options = []): ResultInterface;
} }

View File

@ -1,197 +0,0 @@
Less.php API
========
## Basic use
#### Parse strings
```php
$parser = new Less_Parser();
$parser->parse( '@color: #36c; .link { color: @color; } a { color: @color; }' );
$css = $parser->getCss();
```
#### Parse files
The `parseFile()` function takes two parameters:
* The absolute path to a `.less` file.
* The base URL for any relative image or CSS references in the `.less` file,
typically the same directory that contains the `.less` file or a public equivalent.
```php
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', 'https://example.org/mysite/' );
$css = $parser->getCss();
```
#### Handle invalid syntax
An exception will be thrown if the compiler encounters invalid LESS.
```php
try{
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', 'https://example.org/mysite/' );
$css = $parser->getCss();
} catch (Exception $e) {
echo $e->getMessage();
}
```
#### Parse multiple inputs
Less.php can parse multiple input sources (e.g. files and/or strings) and generate a single CSS output.
```php
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$parser->parse( '@color: #36c; .link { color: @color; } a { color: @color; }' );
$css = $parser->getCss();
```
#### Metadata
Less.php keeps track of which `.less` files have been parsed, i.e. the input
file(s) and any direct and indirect imports.
```php
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
$files = $parser->AllParsedFiles();
```
#### Compress output
You can tell Less.php to remove comments and whitespace to generate minified CSS.
```php
$options = [ 'compress' => true ];
$parser = new Less_Parser( $options );
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
```
#### Get variables
You can use the `getVariables()` method to get an all variables defined and
their value in an associative array. Note that the input must be compiled first
by calling `getCss()`.
```php
$parser = new Less_Parser;
$parser->parseFile( '/var/www/mysite/bootstrap.less');
$css = $parser->getCss();
$variables = $parser->getVariables();
```
#### Set variables
Use the `ModifyVars()` method to inject additional variables, i.e. custom values
computed or accessed from your PHP code.
```php
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$parser->ModifyVars( [ 'font-size-base' => '16px' ] );
$css = $parser->getCss();
```
#### Import directories
By default, Less.php will look for imported files in the directory of the file passed to `parseFile()`.
If you use `parse()`, or if need to enable additional import directories, you can specify these by
calling `SetImportDirs()`.
```php
$directories = [ '/var/www/mysite/bootstrap/' => '/mysite/bootstrap/' ];
$parser = new Less_Parser();
$parser->SetImportDirs( $directories );
$parser->parseFile( '/var/www/mysite/theme.less', '/mysite/' );
$css = $parser->getCss();
```
## Caching
Compiling LESS code into CSS can be a time-consuming process. It is recommended to cache your results.
#### Basic cache
Use the `Less_Cache` class to save and reuse the results of compiling LESS files.
This class will check the modified time and size of each LESS file (including imported files) and
either re-use or re-generate the CSS output accordingly.
The cache files are determinstically named, based on the full list of referenced LESS files and the metadata (file path, file mtime, file size) of each file. This means that each time a change is made, a different cache filename is used.
```php
$lessFiles = [ '/var/www/mysite/bootstrap.less' => '/mysite/' ];
$options = [ 'cache_dir' => '/var/www/writable_folder' ];
$cssOutputFile = Less_Cache::Get( $lessFiles, $options );
$css = file_get_contents( '/var/www/writable_folder/' . $cssOutputFile );
```
#### Caching with variables
Passing custom variables to `Less_Cache::Get()`:
```php
$lessFiles = [ '/var/www/mysite/bootstrap.less' => '/mysite/' ];
$options = [ 'cache_dir' => '/var/www/writable_folder' ];
$variables = [ 'width' => '100px' ];
$cssOutputFile = Less_Cache::Get( $lessFiles, $options, $variables );
$css = file_get_contents( '/var/www/writable_folder/' . $cssOutputFile );
```
#### Incremental caching
In addition to the whole-output caching described above, Less.php also has the ability to keep an internal cache which allows re-parses to be faster by effectively only re-compiling portions that have changed.
## Source maps
Less.php supports v3 sourcemaps.
#### Inline
The sourcemap will be appended to the generated CSS file.
```php
$options = [ 'sourceMap' => true ];
$parser = new Less_Parser($options);
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
```
#### Saving to map file
```php
$options = [
'sourceMap' => true,
'sourceMapWriteTo' => '/var/www/mysite/writable_folder/filename.map',
'sourceMapURL' => '/mysite/writable_folder/filename.map',
];
$parser = new Less_Parser($options);
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
```
## Command line
An additional script has been included to use the Less.php compiler from the command line.
In its simplest invocation, you specify an input file and the compiled CSS is written to standard out:
```
$ lessc input.less > output.css
```
By using the `-w` flag you can watch a specified input file and have it compile as needed to the output file:
```
$ lessc -w input.less output.css
```
Errors from watch mode are written to standard out.
For more information, run `lessc --help`

View File

@ -1,91 +1,70 @@
# Changelog # 3.1.0
- [All Changes](https://github.com/wikimedia/less.php/compare/v3.0.0...v3.1.0)
## 3.2.1 * PHP 8.0 support: Drop use of curly braces for sub-string eval (James D. Forrester)
* [All changes](https://gerrit.wikimedia.org/g/mediawiki/libs/less.php/+log/v3.2.1)
* Tree_Ruleset: Fix support for nested parent selectors (Timo Tijhof) [T204816](https://phabricator.wikimedia.org/T204816)
* Fix ParseError when interpolating variable after colon in selector (Timo Tijhof) [T327163](https://phabricator.wikimedia.org/T327163)
* Functions: Fix "Undefined property" warning on bad minmax arg
* Tree_Call: Include previous exception when catching functions (Robert Frunzke)
## 3.2.0
* [All changes](https://github.com/wikimedia/less.php/compare/v3.1.0...v3.2.0)
* Fix "Implicit conversion" PHP 8.1 warnings (Ayokunle Odusan)
* Fix "Creation of dynamic property" PHP 8.2 warnings (Bas Couwenberg)
* Fix "Creation of dynamic property" PHP 8.2 warnings (Rajesh Kumar)
* Tree_Url: Add support for "Url" type to `Parser::getVariables()` (ciroarcadio) [#51](https://github.com/wikimedia/less.php/pull/51)
* Tree_Import: Add support for importing URLs without file extension (Timo Tijhof) [#27](https://github.com/wikimedia/less.php/issues/27)
## 3.1.0
* [All changes](https://github.com/wikimedia/less.php/compare/v3.0.0...v3.1.0)
* Add PHP 8.0 support: Drop use of curly braces for sub-string eval (James D. Forrester)
* Make `Directive::__construct` $rules arg optional (fix PHP 7.4 warning) (Sam Reed) * Make `Directive::__construct` $rules arg optional (fix PHP 7.4 warning) (Sam Reed)
* ProcessExtends: Improve performance by using a map for selectors and parents (Andrey Legayev) * ProcessExtends: Improve performance by using a map for selectors and parents (Andrey Legayev)
* build: Run CI tests on PHP 8.0 too (James D. Forrester)
* code: Fix PSR12.Properties.ConstantVisibility.NotFound (Sam Reed)
## 3.0.0 # 3.0.0
- [All Changes](https://github.com/wikimedia/less.php/compare/v2.0.0...v3.0.0)
- Raise PHP requirement from 7.1 to 7.2.9 (James Forrester)
- build: Upgrade phpunit to ^8.5 and make pass (James Forrester)
- build: Install php-parallel-lint (James Forrester)
- build: Install minus-x and make pass (James Forrester)
* [All changes](https://github.com/wikimedia/less.php/compare/v2.0.0...v3.0.0) # 2.0.0
* Raise PHP requirement from 7.1 to 7.2.9 (James Forrester) - [All Changes](https://github.com/wikimedia/less.php/compare/1.8.2...v2.0.0)
- Relax PHP requirement down to 7.1, from 7.2.9 (Franz Liedke)
- Reflect recent breaking changes properly with the semantic versioning (James Forrester)
## 2.0.0 # 1.8.2
- [All Changes](https://github.com/wikimedia/less.php/compare/1.8.1...1.8.2)
- Require PHP 7.2.9+, up from 5.3+ (James Forrester)
- Release: Update Version.php with the current release ID (COBadger)
- Fix access array offset on value of type null (Michele Locati)
- Fixed test suite on PHP 7.4 (Sergei Morozov)
- docs: Fix 1.8.1 "All changes" link (Timo Tijhof)
* [All changes](https://github.com/wikimedia/less.php/compare/v1.8.2...v2.0.0) # 1.8.1
* Relax PHP requirement down to 7.1, from 7.2.9 (Franz Liedke) - [All Changes](https://github.com/wikimedia/less.php/compare/v1.8.0...1.8.1)
* Reflect recent breaking changes properly with the semantic versioning (James Forrester) - Another PHP 7.3 compatibility tweak
## 1.8.2 # 1.8.0
- [All Changes](https://github.com/Asenar/less.php/compare/v1.7.0.13...v1.8.0)
- Wikimedia fork
- Supports up to PHP 7.3
- No longer tested against PHP 5, though it's still remains allowed in `composer.json` for HHVM compatibility
- Switched to [semantic versioning](https://semver.org/), hence version numbers now use 3 digits
* [All changes](https://github.com/wikimedia/less.php/compare/v1.8.1...v1.8.2) # 1.7.0.13
* Require PHP 7.2.9+, up from 5.3+ (James Forrester) - [All Changes](https://github.com/Asenar/less.php/compare/v1.7.0.12...v1.7.0.13)
* release: Update Version.php with the current release ID (COBadger) - Fix composer.json (PSR-4 was invalid)
* Fix access array offset on value of type null (Michele Locati)
* Fix test suite on PHP 7.4 (Sergei Morozov)
## 1.8.1 # 1.7.0.12
- [All Changes](https://github.com/Asenar/less.php/compare/v1.7.0.11...v1.7.0.12)
- set bin/lessc bit executable
- Add 'gettingVariables' method in Less_Parser
* [All changes](https://github.com/wikimedia/less.php/compare/v1.8.0...v1.8.1) # 1.7.0.11
* Another PHP 7.3 compatibility tweak - [All Changes](https://github.com/Asenar/less.php/compare/v1.7.0.10...v1.7.0.11)
- Fix realpath issue (windows)
- Set Less_Tree_Call property back to public ( Fix 258 266 267 issues from oyejorge/less.php)
## 1.8.0 # 1.7.0.10
Library forked by Wikimedia, from [oyejorge/less.php](https://github.com/oyejorge/less.php). - [All Changes](https://github.com/oyejorge/less.php/compare/v1.7.0.9...v1.7.10)
- Add indentation option
- Add 'optional' modifier for @import
- fix $color in Exception messages
- don't use set_time_limit when running cli
- take relative-url into account when building the cache filename
- urlArgs should be string no array()
- add bug-report fixtures [#6dc898f](https://github.com/oyejorge/less.php/commit/6dc898f5d75b447464906bdf19d79c2e19d95e33)
- fix #269, missing on NameValue type [#a8dac63](https://github.com/oyejorge/less.php/commit/a8dac63d93fb941c54fb78b12588abf635747c1b)
* [All changes](https://github.com/wikimedia/less.php/compare/v1.7.0.13...v1.8.0) # 1.7.0.9
* Supports up to PHP 7.3
* No longer tested against PHP 5, though it's still remains allowed in `composer.json` for HHVM compatibility
* Switched to [semantic versioning](https://semver.org/), hence version numbers now use 3 digits
## 1.7.0.13 - [All Changes](https://github.com/oyejorge/less.php/compare/v1.7.0.8...v1.7.0.9)
- Remove space at beginning of Version.php
* [All changes](https://github.com/wikimedia/less.php/compare/v1.7.0.12...v1.7.0.13) - Revert require() paths in test interface
* Fix composer.json (PSR-4 was invalid)
## 1.7.0.12
* [All changes](https://github.com/wikimedia/less.php/compare/v1.7.0.11...v1.7.0.12)
* set bin/lessc bit executable
* Add `gettingVariables` method to `Less_Parser`
## 1.7.0.11
* [All changes](https://github.com/wikimedia/less.php/compare/v1.7.0.10...v1.7.0.11)
* Fix realpath issue (windows)
* Set Less_Tree_Call property back to public ( Fix 258 266 267 issues from oyejorge/less.php)
## 1.7.0.10
* [All changes](https://github.com/wikimedia/less.php/compare/v1.7.0.9...v1.7.10)
* Add indentation option
* Add `optional` modifier for `@import`
* Fix $color in Exception messages
* take relative-url into account when building the cache filename
* urlArgs should be string no array()
* fix missing on NameValue type [#269](https://github.com/oyejorge/less.php/issues/269)
## 1.7.0.9
* [All changes](https://github.com/wikimedia/less.php/compare/v1.7.0.8...v1.7.0.9)
* Remove space at beginning of Version.php
* Revert require() paths in test interface

View File

@ -1 +0,0 @@
The development of this software is covered by a [Code of Conduct](https://www.mediawiki.org/wiki/Special:MyLanguage/Code_of_Conduct).

View File

@ -176,27 +176,3 @@
END OF TERMS AND CONDITIONS END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@ -1,18 +0,0 @@
wikimedia/less.php. https://gerrit.wikimedia.org/g/mediawiki/libs/less.php
Copyright Matt Agar <https://github.com/agar>
Copyright Martin Jantošovič <https://github.com/Mordred>
Copyright Josh Schmidt <https://github.com/oyejorge>
Copyright Timo Tijhof <https://timotijhof.net>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

View File

@ -1,77 +1,315 @@
[![Packagist](https://img.shields.io/packagist/v/wikimedia/less.php.svg?style=flat)](https://packagist.org/packages/wikimedia/less.php) [![Continuous Integration](https://github.com/wikimedia/less.php/workflows/PHP%20Test/badge.svg)](https://github.com/wikimedia/less.php/actions)
Less.php [Less.php](http://lessphp.typesettercms.com)
======== ========
This is a PHP port of the [official LESS processor](https://lesscss.org). This is the Wikimedia fork of a PHP port of the official LESS processor <http://lesscss.org>.
## About * [About](#about)
* [Installation](#installation)
* [Basic Use](#basic-use)
* [Caching](#caching)
* [Source Maps](#source-maps)
* [Command Line](#command-line)
* [Integration with other projects](#integration-with-other-projects)
* [Transitioning from Leafo/lessphp](#transitioning-from-leafolessphp)
* [Credits](#credits)
The code structure of Less.php mirrors that of upstream Less.js to ensure compatibility and help reduce maintenance. The port is currently compatible with Less.js 2.5.3. Please note that "inline JavaScript expressions" (via eval or backticks) are not supported.
* [API § Caching](./API.md#caching), Less.php includes a file-based cache.
* [API § Source maps](./API.md#source-maps), Less.php supports v3 sourcemaps.
* [API § Command line](./API.md#command-line), the `lessc` command includes a watch mode.
## Installation About
---
The code structure of less.php mirrors that of the official processor which helps us ensure compatibility and allows for easy maintenance.
You can install the library with Composer or standalone. Please note, there are a few unsupported LESS features:
If you have [Composer](https://getcomposer.org/download/) installed: - Evaluation of JavaScript expressions within back-ticks (for obvious reasons).
- Definition of custom functions.
1. Run `composer require wikimedia/less.php`
2. Use `Less_Parser` in your code.
Or standalone: Installation
---
You can install the library with Composer or manually.
#### Composer
1. [Install Composer](https://getcomposer.org/download/)
2. Run `composer require wikimedia/less.php`
#### Manually From Release
Step 1. [Download a release](https://github.com/wikimedia/less.php/releases) and upload the PHP files to your server.
Step 2. Include the library:
1. [Download Less.php](https://gerrit.wikimedia.org/g/mediawiki/libs/less.php/+archive/HEAD.tar.gz) and upload the PHP files to your server.
2. Include the library:
```php ```php
require_once '[path to]/less.php/lib/Less/Autoloader.php'; require_once '[path to less.php]/lib/Less/Autoloader.php';
Less_Autoloader::register(); Less_Autoloader::register();
``` ```
3. Use `Less_Parser` in your code.
## Security Basic Use
---
The LESS processor language is powerful and includes features that may read or embed arbitrary files that the web server has access to, and features that may be computationally exensive if misused. #### Parsing Strings
In general you should treat LESS files as being in the same trust domain as other server-side executables, such as PHP code. In particular, it is not recommended to allow people that use your web service to provide arbitrary LESS code for server-side processing. ```php
$parser = new Less_Parser();
$parser->parse( '@color: #4D926F; #header { color: @color; } h2 { color: @color; }' );
$css = $parser->getCss();
```
_See also [SECURITY](./SECURITY.md)._
## Who uses Less.php? #### Parsing LESS Files
The parseFile() function takes two arguments:
* **[Wikipedia](https://en.wikipedia.org/wiki/MediaWiki)** and the MediaWiki platform ([docs](https://www.mediawiki.org/wiki/ResourceLoader/Architecture#Resource:_Styles)). 1. The absolute path of the .less file to be parsed
* **[Matomo](https://en.wikipedia.org/wiki/Matomo_(software))** ([docs](https://devdocs.magento.com/guides/v2.4/frontend-dev-guide/css-topics/custom_preprocess.html)). 2. The url root to prepend to any relative image or @import urls in the .less file.
* **[Magento](https://en.wikipedia.org/wiki/Magento)** as part of Adobe Commerce ([docs](https://developer.matomo.org/guides/asset-pipeline#vanilla-javascript-css-and-less-files)).
* **[Icinga](https://en.wikipedia.org/wiki/Icinga)** in Icinga Web ([docs](https://github.com/Icinga/icingaweb2)).
* **[Shopware](https://de.wikipedia.org/wiki/Shopware)** ([docs](https://developers.shopware.com/designers-guide/less/)).
## Integrations ```php
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', 'http://example.com/mysite/' );
$css = $parser->getCss();
```
Less.php has been integrated with various other projects.
#### Transitioning from Leafo/lessphp #### Handling Invalid LESS
An exception will be thrown if the compiler encounters invalid LESS.
If you're looking to transition from the [Leafo/lessphp](https://github.com/leafo/lessphp) library, use the `lessc.inc.php` adapter file that comes with Less.php. ```php
try{
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', 'http://example.com/mysite/' );
$css = $parser->getCss();
}catch(Exception $e){
$error_message = $e->getMessage();
}
```
This allows Less.php to be a drop-in replacement for Leafo/lessphp.
[Download Less.php](https://gerrit.wikimedia.org/g/mediawiki/libs/less.php/+archive/HEAD.tar.gz), unzip the files into your project, and include its `lessc.inc.php` instead. #### Parsing Multiple Sources
less.php can parse multiple sources to generate a single CSS file.
Note: The `setPreserveComments` option is ignored. Less.php already preserves CSS block comments by default, and removes LESS inline comments. ```php
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$parser->parse( '@color: #4D926F; #header { color: @color; } h2 { color: @color; }' );
$css = $parser->getCss();
```
#### Drupal #### Getting Info About The Parsed Files
less.php can tell you which .less files were imported and parsed.
Less.php can be used with [Drupal's less module](https://drupal.org/project/less) via the `lessc.inc.php` adapter. [Download Less.php](https://gerrit.wikimedia.org/g/mediawiki/libs/less.php/+archive/HEAD.tar.gz) and unzip it so that `lessc.inc.php` is located at `sites/all/libraries/lessphp/lessc.inc.php`, then install the Drupal less module as usual. ```php
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
$imported_files = $parser->allParsedFiles();
```
#### Compressing Output
You can tell less.php to remove comments and whitespace to generate minimized CSS files.
```php
$options = array( 'compress'=>true );
$parser = new Less_Parser( $options );
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
```
#### Getting Variables
You can use the getVariables() method to get an all variables defined and
their value in a php associative array. Note that LESS has to be previously
compiled.
```php
$parser = new Less_Parser;
$parser->parseFile( '/var/www/mysite/bootstrap.less');
$css = $parser->getCss();
$variables = $parser->getVariables();
```
#### Setting Variables
You can use the ModifyVars() method to customize your CSS if you have variables stored in PHP associative arrays.
```php
$parser = new Less_Parser();
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$parser->ModifyVars( array('font-size-base'=>'16px') );
$css = $parser->getCss();
```
#### Import Directories
By default, less.php will look for @imports in the directory of the file passed to parseFile().
If you're using parse() or if @imports reside in different directories, you can tell less.php where to look.
```php
$directories = array( '/var/www/mysite/bootstrap/' => '/mysite/bootstrap/' );
$parser = new Less_Parser();
$parser->SetImportDirs( $directories );
$parser->parseFile( '/var/www/mysite/theme.less', '/mysite/' );
$css = $parser->getCss();
```
Caching
---
Compiling LESS code into CSS is a time consuming process, caching your results is highly recommended.
#### Caching CSS
Use the Less_Cache class to save and reuse the results of compiled LESS files.
This method will check the modified time and size of each LESS file (including imported files) and regenerate a new CSS file when changes are found.
Note: When changes are found, this method will return a different file name for the new cached content.
```php
$less_files = array( '/var/www/mysite/bootstrap.less' => '/mysite/' );
$options = array( 'cache_dir' => '/var/www/writable_folder' );
$css_file_name = Less_Cache::Get( $less_files, $options );
$compiled = file_get_contents( '/var/www/writable_folder/'.$css_file_name );
```
#### Caching CSS With Variables
Passing options to Less_Cache::Get()
```php
$less_files = array( '/var/www/mysite/bootstrap.less' => '/mysite/' );
$options = array( 'cache_dir' => '/var/www/writable_folder' );
$variables = array( 'width' => '100px' );
$css_file_name = Less_Cache::Get( $less_files, $options, $variables );
$compiled = file_get_contents( '/var/www/writable_folder/'.$css_file_name );
```
#### Parser Caching
less.php will save serialized parser data for each .less file if a writable folder is passed to the SetCacheDir() method.
Note: This feature only caches intermediate parsing results to improve the performance of repeated CSS generation.
Your application should cache any CSS generated by less.php.
```php
$options = array('cache_dir'=>'/var/www/writable_folder');
$parser = new Less_Parser( $options );
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
```
You can specify the caching technique used by changing the ```cache_method``` option. Supported methods are:
* ```php```: Creates valid PHP files which can be included without any changes (default method).
* ```var_export```: Like "php", but using PHP's ```var_export()``` function without any optimizations.
It's recommended to use "php" instead.
* ```serialize```: Faster, but pretty memory-intense.
* ```callback```: Use custom callback functions to implement your own caching method. Give the "cache_callback_get" and
"cache_callback_set" options with callables (see PHP's ```call_user_func()``` and ```is_callable()``` functions). less.php
will pass the parser object (class ```Less_Parser```), the path to the parsed .less file ("/some/path/to/file.less") and
an identifier that will change every time the .less file is modified. The ```get``` callback must return the ruleset
(an array with ```Less_Tree``` objects) provided as fourth parameter of the ```set``` callback. If something goes wrong,
return ```NULL``` (cache doesn't exist) or ```FALSE```.
Source Maps
---
Less.php supports v3 sourcemaps
#### Inline
The sourcemap will be appended to the generated CSS file.
```php
$options = array( 'sourceMap' => true );
$parser = new Less_Parser($options);
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
```
#### Saving to Map File
```php
$options = array(
'sourceMap' => true,
'sourceMapWriteTo' => '/var/www/mysite/writable_folder/filename.map',
'sourceMapURL' => '/mysite/writable_folder/filename.map',
);
$parser = new Less_Parser($options);
$parser->parseFile( '/var/www/mysite/bootstrap.less', '/mysite/' );
$css = $parser->getCss();
```
Command line
---
An additional script has been included to use the compiler from the command line.
In the simplest invocation, you specify an input file and the compiled CSS is written to standard out:
```
$ lessc input.less > output.css
```
By using the -w flag you can watch a specified input file and have it compile as needed to the output file:
```
$ lessc -w input.less output.css
```
Errors from watch mode are written to standard out.
For more help, run `lessc --help`
Integration with other projects
---
#### Drupal 7
This library can be used as drop-in replacement of lessphp to work with [Drupal 7 less module](https://drupal.org/project/less).
How to install:
1. [Download the less.php source code](https://github.com/wikimedia/less.php/archive/master.zip) and unzip it so that 'lessc.inc.php' is located at 'sites/all/libraries/lessphp/lessc.inc.php'.
2. Download and install [Drupal 7 less module](https://drupal.org/project/less) as usual.
3. That's it :)
#### JBST WordPress theme
JBST has a built-in LESS compiler based on lessphp. Customize your WordPress theme with LESS.
How to use / install:
1. [Download the latest release](https://github.com/bassjobsen/jamedo-bootstrap-start-theme) copy the files to your {wordpress/}wp-content/themes folder and activate it.
2. Find the compiler under Appearance > LESS Compiler in your WordPress dashboard
3. Enter your LESS code in the text area and press (re)compile
Use the built-in compiler to:
- set any [Bootstrap](http://getbootstrap.com/customize/) variable or use Bootstrap's mixins:
-`@navbar-default-color: blue;`
- create a custom button: `.btn-custom {
.button-variant(white; red; blue);
}`
- set any built-in LESS variable: for example `@footer_bg_color: black;` sets the background color of the footer to black
- use built-in mixins: - add a custom font: `.include-custom-font(@family: arial,@font-path, @path: @custom-font-dir, @weight: normal, @style: normal);`
The compiler can also be downloaded as [plugin](http://wordpress.org/plugins/wp-less-to-css/)
#### WordPress #### WordPress
* [wp_enqueue_less](https://github.com/Ed-ITSolutions/wp_enqueue_less) is a Composer package for use in WordPress themes and plugins. It provides a `wp_enqueue_less()` function to automatically manage caching and compilation on-demand, and loads the compressed CSS on the page. This simple plugin will simply make the library available to other plugins and themes and can be used as a dependency using the [TGM Library](http://tgmpluginactivation.com/)
* [JBST framework](https://github.com/bassjobsen/jamedo-bootstrap-start-theme) bundles a copy of Less.php.
* The [lessphp plugin](https://wordpress.org/plugins/lessphp/) bundles a copy of Less.php for use in other plugins or themes. This dependency can also be combined with the [TGM Library](http://tgmpluginactivation.com/).
## Credits How to install:
1. Install the plugin from your WordPress Dashboard: http://wordpress.org/plugins/lessphp/
2. That's it :)
Transitioning from Leafo/lessphp
---
Projects looking for an easy transition from leafo/lessphp can use the lessc.inc.php adapter. To use, [Download the less.php source code](https://github.com/wikimedia/less.php/archive/master.zip) and unzip the files into your project so that the new 'lessc.inc.php' replaces the existing 'lessc.inc.php'.
Note, the 'setPreserveComments' will no longer have any effect on the compiled LESS.
Credits
---
less.php was originally ported to PHP by [Matt Agar](https://github.com/agar) and then updated by [Martin Jantošovič](https://github.com/Mordred). This Wikimedia-maintained fork was split off from [Josh Schmidt's version](https://github.com/oyejorge/less.php).
Less.php was originally ported to PHP in 2011 by [Matt Agar](https://github.com/agar) and then updated by [Martin Jantošovič](https://github.com/Mordred) in 2012. From 2013 to 2017, [Josh Schmidt](https://github.com/oyejorge) lead development of the library. Since 2019, the library is maintained by Wikimedia Foundation.

View File

@ -1,5 +0,0 @@
# Security policy
Wikimedia takes security seriously. If you believe you have found a
security issue, see <https://www.mediawiki.org/wiki/Reporting_security_bugs>
for information on how to responsibly report it.

View File

@ -1,7 +1,7 @@
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
require_once __DIR__ . '/../lib/Less/Autoloader.php'; require_once dirname(__FILE__) . '/../lib/Less/Autoloader.php';
Less_Autoloader::register(); Less_Autoloader::register();
// Create our environment // Create our environment

49
vendor/wikimedia/less.php/composer.json vendored Normal file
View File

@ -0,0 +1,49 @@
{
"name": "wikimedia/less.php",
"description": "PHP port of the Javascript version of LESS http://lesscss.org (Originally maintained by Josh Schmidt)",
"keywords": [ "less", "css", "php", "stylesheet", "less.js", "lesscss" ],
"license": "Apache-2.0",
"authors": [
{
"name": "Josh Schmidt",
"homepage": "https://github.com/oyejorge"
},
{
"name": "Matt Agar",
"homepage": "https://github.com/agar"
},
{
"name": "Martin Jantošovič",
"homepage": "https://github.com/Mordred"
}
],
"require": {
"php": ">=7.2.9"
},
"require-dev": {
"mediawiki/mediawiki-codesniffer": "34.0.0",
"mediawiki/minus-x": "1.0.0",
"php-parallel-lint/php-console-highlighter": "0.5.0",
"php-parallel-lint/php-parallel-lint": "1.2.0",
"phpunit/phpunit": "^8.5"
},
"scripts": {
"test": [
"parallel-lint . --exclude vendor",
"phpcs -sp",
"phpunit",
"minus-x check ."
],
"fix": [
"minus-x fix .",
"phpcbf"
]
},
"autoload": {
"psr-0": { "Less": "lib/" },
"classmap": ["lessc.inc.php"]
},
"bin": [
"bin/lessc"
]
}

View File

@ -14,14 +14,14 @@ if ( !class_exists( 'Less_Parser' ) ) {
class lessc { class lessc {
public static $VERSION = Less_Version::less_version; static public $VERSION = Less_Version::less_version;
public $importDir = ''; public $importDir = '';
protected $allParsedFiles = []; protected $allParsedFiles = array();
protected $libFunctions = []; protected $libFunctions = array();
protected $registeredVars = []; protected $registeredVars = array();
private $formatterName; private $formatterName;
private $options = []; private $options = array();
public function __construct( $lessc = null, $sourceName = null ) { public function __construct( $lessc = null, $sourceName = null ) {
} }
@ -74,7 +74,7 @@ class lessc {
$this->options[$name] = $value; $this->options[$name] = $value;
} }
public function parse( $buffer, $presets = [] ) { public function parse( $buffer, $presets = array() ) {
$this->setVariables( $presets ); $this->setVariables( $presets );
$parser = new Less_Parser( $this->getOptions() ); $parser = new Less_Parser( $this->getOptions() );
@ -91,7 +91,7 @@ class lessc {
} }
protected function getOptions() { protected function getOptions() {
$options = [ 'relativeUrls' => false ]; $options = array( 'relativeUrls' => false );
switch ( $this->formatterName ) { switch ( $this->formatterName ) {
case 'compressed': case 'compressed':
$options['compress'] = true; $options['compress'] = true;
@ -105,7 +105,7 @@ class lessc {
protected function getImportDirs() { protected function getImportDirs() {
$dirs_ = (array)$this->importDir; $dirs_ = (array)$this->importDir;
$dirs = []; $dirs = array();
foreach ( $dirs_ as $dir ) { foreach ( $dirs_ as $dir ) {
$dirs[$dir] = ''; $dirs[$dir] = '';
} }
@ -116,7 +116,7 @@ class lessc {
$oldImport = $this->importDir; $oldImport = $this->importDir;
$this->importDir = (array)$this->importDir; $this->importDir = (array)$this->importDir;
$this->allParsedFiles = []; $this->allParsedFiles = array();
$parser = new Less_Parser( $this->getOptions() ); $parser = new Less_Parser( $this->getOptions() );
$parser->SetImportDirs( $this->getImportDirs() ); $parser->SetImportDirs( $this->getImportDirs() );
@ -151,7 +151,7 @@ class lessc {
$this->importDir = (array)$this->importDir; $this->importDir = (array)$this->importDir;
$this->importDir[] = Less_Parser::AbsPath( $pi['dirname'] ).'/'; $this->importDir[] = Less_Parser::AbsPath( $pi['dirname'] ).'/';
$this->allParsedFiles = []; $this->allParsedFiles = array();
$this->addParsedFile( $fname ); $this->addParsedFile( $fname );
$parser = new Less_Parser( $this->getOptions() ); $parser = new Less_Parser( $this->getOptions() );
@ -237,7 +237,7 @@ class lessc {
if ( $root !== null ) { if ( $root !== null ) {
// If we have a root value which means we should rebuild. // If we have a root value which means we should rebuild.
$out = []; $out = array();
$out['root'] = $root; $out['root'] = $root;
$out['compiled'] = $this->compileFile( $root ); $out['compiled'] = $this->compileFile( $root );
$out['files'] = $this->allParsedFiles(); $out['files'] = $this->allParsedFiles();

View File

@ -2,14 +2,28 @@
/** /**
* Autoloader * Autoloader
*
* @package Less
* @subpackage autoload
*/ */
class Less_Autoloader { class Less_Autoloader {
/** @var bool */ /**
* Registered flag
*
* @var boolean
*/
protected static $registered = false; protected static $registered = false;
/** /**
* Register the autoloader in the SPL autoloader * Library directory
*
* @var string
*/
protected static $libDir;
/**
* Register the autoloader in the spl autoloader
* *
* @return void * @return void
* @throws Exception If there was an error in registration * @throws Exception If there was an error in registration
@ -19,7 +33,9 @@ class Less_Autoloader {
return; return;
} }
if ( !spl_autoload_register( [ 'Less_Autoloader', 'loadClass' ] ) ) { self::$libDir = dirname( __FILE__ );
if ( false === spl_autoload_register( array( 'Less_Autoloader', 'loadClass' ) ) ) {
throw new Exception( 'Unable to register Less_Autoloader::loadClass as an autoloading method.' ); throw new Exception( 'Unable to register Less_Autoloader::loadClass as an autoloading method.' );
} }
@ -27,17 +43,17 @@ class Less_Autoloader {
} }
/** /**
* Unregister the autoloader * Unregisters the autoloader
* *
* @return void * @return void
*/ */
public static function unregister() { public static function unregister() {
spl_autoload_unregister( [ 'Less_Autoloader', 'loadClass' ] ); spl_autoload_unregister( array( 'Less_Autoloader', 'loadClass' ) );
self::$registered = false; self::$registered = false;
} }
/** /**
* Load the class * Loads the class
* *
* @param string $className The class to load * @param string $className The class to load
*/ */
@ -48,10 +64,14 @@ class Less_Autoloader {
} }
$className = substr( $className, 5 ); $className = substr( $className, 5 );
$fileName = __DIR__ . DIRECTORY_SEPARATOR . str_replace( '_', DIRECTORY_SEPARATOR, $className ) . '.php'; $fileName = self::$libDir . DIRECTORY_SEPARATOR . str_replace( '_', DIRECTORY_SEPARATOR, $className ) . '.php';
if ( file_exists( $fileName ) ) {
require $fileName; require $fileName;
return true; return true;
} else {
throw new Exception( 'file not loadable '.$fileName );
}
} }
} }

View File

@ -1,7 +1,13 @@
<?php <?php
require_once dirname( __FILE__ ).'/Version.php';
/** /**
* Utility for handling the generation and caching of css files * Utility for handling the generation and caching of css files
*
* @package Less
* @subpackage cache
*
*/ */
class Less_Cache { class Less_Cache {
@ -25,31 +31,31 @@ class Less_Cache {
* @param array $less_files Array of .less files to compile * @param array $less_files Array of .less files to compile
* @param array $parser_options Array of compiler options * @param array $parser_options Array of compiler options
* @param array $modify_vars Array of variables * @param array $modify_vars Array of variables
* @return string|false Name of the css file * @return string Name of the css file
*/ */
public static function Get( $less_files, $parser_options = [], $modify_vars = [] ) { public static function Get( $less_files, $parser_options = array(), $modify_vars = array() ) {
// check $cache_dir // check $cache_dir
if ( isset( $parser_options['cache_dir'] ) ) { if ( isset( $parser_options['cache_dir'] ) ) {
self::$cache_dir = $parser_options['cache_dir']; Less_Cache::$cache_dir = $parser_options['cache_dir'];
} }
if ( empty( self::$cache_dir ) ) { if ( empty( Less_Cache::$cache_dir ) ) {
throw new Exception( 'cache_dir not set' ); throw new Exception( 'cache_dir not set' );
} }
if ( isset( $parser_options['prefix'] ) ) { if ( isset( $parser_options['prefix'] ) ) {
self::$prefix = $parser_options['prefix']; Less_Cache::$prefix = $parser_options['prefix'];
} }
if ( empty( self::$prefix ) ) { if ( empty( Less_Cache::$prefix ) ) {
throw new Exception( 'prefix not set' ); throw new Exception( 'prefix not set' );
} }
if ( isset( $parser_options['prefix_vars'] ) ) { if ( isset( $parser_options['prefix_vars'] ) ) {
self::$prefix_vars = $parser_options['prefix_vars']; Less_Cache::$prefix_vars = $parser_options['prefix_vars'];
} }
if ( empty( self::$prefix_vars ) ) { if ( empty( Less_Cache::$prefix_vars ) ) {
throw new Exception( 'prefix_vars not set' ); throw new Exception( 'prefix_vars not set' );
} }
@ -59,18 +65,18 @@ class Less_Cache {
// create a file for variables // create a file for variables
if ( !empty( $modify_vars ) ) { if ( !empty( $modify_vars ) ) {
$lessvars = Less_Parser::serializeVars( $modify_vars ); $lessvars = Less_Parser::serializeVars( $modify_vars );
$vars_file = self::$cache_dir . self::$prefix_vars . sha1( $lessvars ) . '.less'; $vars_file = Less_Cache::$cache_dir . Less_Cache::$prefix_vars . sha1( $lessvars ) . '.less';
if ( !file_exists( $vars_file ) ) { if ( !file_exists( $vars_file ) ) {
file_put_contents( $vars_file, $lessvars ); file_put_contents( $vars_file, $lessvars );
} }
$less_files += [ $vars_file => '/' ]; $less_files += array( $vars_file => '/' );
} }
// generate name for compiled css file // generate name for compiled css file
$hash = md5( json_encode( $less_files ) ); $hash = md5( json_encode( $less_files ) );
$list_file = self::$cache_dir . self::$prefix . $hash . '.list'; $list_file = Less_Cache::$cache_dir . Less_Cache::$prefix . $hash . '.list';
// check cached content // check cached content
if ( !isset( $parser_options['use_cache'] ) || $parser_options['use_cache'] === true ) { if ( !isset( $parser_options['use_cache'] ) || $parser_options['use_cache'] === true ) {
@ -123,13 +129,19 @@ class Less_Cache {
* @param array $modify_vars Array of variables * @param array $modify_vars Array of variables
* @return string Name of the css file * @return string Name of the css file
*/ */
public static function Regen( $less_files, $parser_options = [], $modify_vars = [] ) { public static function Regen( $less_files, $parser_options = array(), $modify_vars = array() ) {
$parser_options['use_cache'] = false; $parser_options['use_cache'] = false;
return self::Get( $less_files, $parser_options, $modify_vars ); return self::Get( $less_files, $parser_options, $modify_vars );
} }
public static function Cache( &$less_files, $parser_options = [] ) { public static function Cache( &$less_files, $parser_options = array() ) {
$parser_options['cache_dir'] = self::$cache_dir; // get less.php if it exists
$file = dirname( __FILE__ ) . '/Less.php';
if ( file_exists( $file ) && !class_exists( 'Less_Parser' ) ) {
require_once $file;
}
$parser_options['cache_dir'] = Less_Cache::$cache_dir;
$parser = new Less_Parser( $parser_options ); $parser = new Less_Parser( $parser_options );
// combine files // combine files
@ -160,52 +172,54 @@ class Less_Cache {
return $parser_options['output']; return $parser_options['output'];
} }
return self::$cache_dir . $parser_options['output']; return Less_Cache::$cache_dir.$parser_options['output'];
} }
return self::$cache_dir . $compiled_name; return Less_Cache::$cache_dir.$compiled_name;
} }
private static function CompiledName( $files, $extrahash ) { private static function CompiledName( $files, $extrahash ) {
// save the file list // save the file list
$temp = [ Less_Version::cache_version ]; $temp = array( Less_Version::cache_version );
foreach ( $files as $file ) { foreach ( $files as $file ) {
$temp[] = filemtime( $file )."\t".filesize( $file )."\t".$file; $temp[] = filemtime( $file )."\t".filesize( $file )."\t".$file;
} }
return self::$prefix . sha1( json_encode( $temp ) . $extrahash ) . '.css'; return Less_Cache::$prefix.sha1( json_encode( $temp ).$extrahash ).'.css';
} }
public static function SetCacheDir( $dir ) { public static function SetCacheDir( $dir ) {
self::$cache_dir = $dir; Less_Cache::$cache_dir = $dir;
self::CheckCacheDir(); self::CheckCacheDir();
} }
public static function CheckCacheDir() { public static function CheckCacheDir() {
self::$cache_dir = str_replace( '\\', '/', self::$cache_dir ); Less_Cache::$cache_dir = str_replace( '\\', '/', Less_Cache::$cache_dir );
self::$cache_dir = rtrim( self::$cache_dir, '/' ) . '/'; Less_Cache::$cache_dir = rtrim( Less_Cache::$cache_dir, '/' ).'/';
if ( !file_exists( self::$cache_dir ) ) { if ( !file_exists( Less_Cache::$cache_dir ) ) {
if ( !mkdir( self::$cache_dir ) ) { if ( !mkdir( Less_Cache::$cache_dir ) ) {
throw new Less_Exception_Parser( 'Less.php cache directory couldn\'t be created: ' . self::$cache_dir ); throw new Less_Exception_Parser( 'Less.php cache directory couldn\'t be created: '.Less_Cache::$cache_dir );
} }
} elseif ( !is_dir( self::$cache_dir ) ) { } elseif ( !is_dir( Less_Cache::$cache_dir ) ) {
throw new Less_Exception_Parser( 'Less.php cache directory doesn\'t exist: ' . self::$cache_dir ); throw new Less_Exception_Parser( 'Less.php cache directory doesn\'t exist: '.Less_Cache::$cache_dir );
} elseif ( !is_writable( self::$cache_dir ) ) { } elseif ( !is_writable( Less_Cache::$cache_dir ) ) {
throw new Less_Exception_Parser( 'Less.php cache directory isn\'t writable: ' . self::$cache_dir ); throw new Less_Exception_Parser( 'Less.php cache directory isn\'t writable: '.Less_Cache::$cache_dir );
} }
} }
/** /**
* Delete unused less.php files * Delete unused less.php files
*
*/ */
public static function CleanCache() { public static function CleanCache() {
static $clean = false; static $clean = false;
if ( $clean || empty( self::$cache_dir ) ) { if ( $clean || empty( Less_Cache::$cache_dir ) ) {
return; return;
} }
@ -213,9 +227,9 @@ class Less_Cache {
// only remove files with extensions created by less.php // only remove files with extensions created by less.php
// css files removed based on the list files // css files removed based on the list files
$remove_types = [ 'lesscache' => 1,'list' => 1,'less' => 1,'map' => 1 ]; $remove_types = array( 'lesscache' => 1,'list' => 1,'less' => 1,'map' => 1 );
$files = scandir( self::$cache_dir ); $files = scandir( Less_Cache::$cache_dir );
if ( !$files ) { if ( !$files ) {
return; return;
} }
@ -224,7 +238,7 @@ class Less_Cache {
foreach ( $files as $file ) { foreach ( $files as $file ) {
// don't delete if the file wasn't created with less.php // don't delete if the file wasn't created with less.php
if ( strpos( $file, self::$prefix ) !== 0 ) { if ( strpos( $file, Less_Cache::$prefix ) !== 0 ) {
continue; continue;
} }
@ -235,7 +249,7 @@ class Less_Cache {
continue; continue;
} }
$full_path = self::$cache_dir . $file; $full_path = Less_Cache::$cache_dir . $file;
$mtime = filemtime( $full_path ); $mtime = filemtime( $full_path );
// don't delete if it's a relatively new file // don't delete if it's a relatively new file
@ -247,7 +261,7 @@ class Less_Cache {
if ( $type === 'list' ) { if ( $type === 'list' ) {
self::ListFiles( $full_path, $list, $css_file_name ); self::ListFiles( $full_path, $list, $css_file_name );
if ( $css_file_name ) { if ( $css_file_name ) {
$css_file = self::$cache_dir . $css_file_name; $css_file = Less_Cache::$cache_dir . $css_file_name;
if ( file_exists( $css_file ) ) { if ( file_exists( $css_file ) ) {
unlink( $css_file ); unlink( $css_file );
} }
@ -256,10 +270,12 @@ class Less_Cache {
unlink( $full_path ); unlink( $full_path );
} }
} }
/** /**
* Get the list of less files and generated css file from a list file * Get the list of less files and generated css file from a list file
*
*/ */
static function ListFiles( $list_file, &$list, &$css_file_name ) { static function ListFiles( $list_file, &$list, &$css_file_name ) {
$list = explode( "\n", file_get_contents( $list_file ) ); $list = explode( "\n", file_get_contents( $list_file ) );
@ -267,10 +283,11 @@ class Less_Cache {
// pop the cached name that should match $compiled_name // pop the cached name that should match $compiled_name
$css_file_name = array_pop( $list ); $css_file_name = array_pop( $list );
if ( !preg_match( '/^' . self::$prefix . '[a-f0-9]+\.css$/', $css_file_name ) ) { if ( !preg_match( '/^' . Less_Cache::$prefix . '[a-f0-9]+\.css$/', $css_file_name ) ) {
$list[] = $css_file_name; $list[] = $css_file_name;
$css_file_name = false; $css_file_name = false;
} }
} }
} }

View File

@ -1,12 +1,14 @@
<?php <?php
/** /**
* Utility for css colors * Utility for css colors
* *
* @private * @package Less
* @subpackage color
*/ */
class Less_Colors { class Less_Colors {
private const COLORS = [ public static $colors = array(
'aliceblue' => '#f0f8ff', 'aliceblue' => '#f0f8ff',
'antiquewhite' => '#faebd7', 'antiquewhite' => '#faebd7',
'aqua' => '#00ffff', 'aqua' => '#00ffff',
@ -153,24 +155,15 @@ class Less_Colors {
'white' => '#ffffff', 'white' => '#ffffff',
'whitesmoke' => '#f5f5f5', 'whitesmoke' => '#f5f5f5',
'yellow' => '#ffff00', 'yellow' => '#ffff00',
'yellowgreen' => '#9acd32', 'yellowgreen' => '#9acd32'
]; );
/** public static function hasOwnProperty( $color ) {
* @param string $color return isset( self::$colors[$color] );
* @return bool
*/
public static function hasOwnProperty( string $color ): bool {
return isset( self::COLORS[$color] );
} }
/** public static function color( $color ) {
* @param string $color Should be an existing color name, return self::$colors[$color];
* checked via hasOwnProperty()
* @return string the corresponding hexadecimal representation
*/
public static function color( string $color ): string {
return self::COLORS[$color];
} }
} }

View File

@ -1,6 +1,10 @@
<?php <?php
/** /**
* @private * Configurable
*
* @package Less
* @subpackage Core
*/ */
abstract class Less_Configurable { abstract class Less_Configurable {
@ -9,14 +13,14 @@ abstract class Less_Configurable {
* *
* @var array * @var array
*/ */
protected $options = []; protected $options = array();
/** /**
* Array of default options * Array of default options
* *
* @var array * @var array
*/ */
protected $defaultOptions = []; protected $defaultOptions = array();
/** /**
* Set options * Set options
@ -24,7 +28,9 @@ abstract class Less_Configurable {
* If $options is an object it will be converted into an array by called * If $options is an object it will be converted into an array by called
* it's toArray method. * it's toArray method.
* *
* @throws Exception
* @param array|object $options * @param array|object $options
*
*/ */
public function setOptions( $options ) { public function setOptions( $options ) {
$options = array_intersect_key( $options, $this->defaultOptions ); $options = array_intersect_key( $options, $this->defaultOptions );

View File

@ -1,35 +1,39 @@
<?php <?php
/** /**
* @private * Environment
*
* @package Less
* @subpackage environment
*/ */
class Less_Environment { class Less_Environment {
/** // public $paths = array(); // option - unmodified - paths to search for imports on
* Information about the current file - for error reporting and importing and making urls relative etc. //public static $files = array(); // list of files that have been imported, used for import-once
* //public $rootpath; // option - rootpath to append to URL's
* - rootpath: rootpath to append to URLs //public static $strictImports = null; // option -
* //public $insecure; // option - whether to allow imports from insecure ssl hosts
* @var array|null $currentFileInfo; //public $processImports; // option - whether to process imports. if false then imports will not be imported
*/ //public $javascriptEnabled; // option - whether JavaScript is enabled. if undefined, defaults to true
public $currentFileInfo; //public $useFileCache; // browser only - whether to use the per file session cache
public $currentFileInfo; // information about the current file - for error reporting and importing and making urls relative etc.
/* Whether we are currently importing multiple copies */ public $importMultiple = false; // whether we are currently importing multiple copies
public $importMultiple = false;
/** /**
* @var array * @var array
*/ */
public $frames = []; public $frames = array();
/** /**
* @var array * @var array
*/ */
public $mediaBlocks = []; public $mediaBlocks = array();
/** /**
* @var array * @var array
*/ */
public $mediaPath = []; public $mediaPath = array();
public static $parensStack = 0; public static $parensStack = 0;
@ -44,7 +48,7 @@ class Less_Environment {
/** /**
* @var array * @var array
*/ */
public $functions = []; public $functions = array();
public function Init() { public function Init() {
self::$parensStack = 0; self::$parensStack = 0;
@ -54,7 +58,7 @@ class Less_Environment {
if ( Less_Parser::$options['compress'] ) { if ( Less_Parser::$options['compress'] ) {
self::$_outputMap = [ Less_Environment::$_outputMap = array(
',' => ',', ',' => ',',
': ' => ':', ': ' => ':',
'' => '', '' => '',
@ -66,11 +70,11 @@ class Less_Environment {
'|' => '|', '|' => '|',
'^' => '^', '^' => '^',
'^^' => '^^' '^^' => '^^'
]; );
} else { } else {
self::$_outputMap = [ Less_Environment::$_outputMap = array(
',' => ', ', ',' => ', ',
': ' => ': ', ': ' => ': ',
'' => '', '' => '',
@ -82,19 +86,19 @@ class Less_Environment {
'|' => '|', '|' => '|',
'^' => ' ^ ', '^' => ' ^ ',
'^^' => ' ^^ ' '^^' => ' ^^ '
]; );
} }
} }
public function copyEvalEnv( $frames = [] ) { public function copyEvalEnv( $frames = array() ) {
$new_env = new Less_Environment(); $new_env = new Less_Environment();
$new_env->frames = $frames; $new_env->frames = $frames;
return $new_env; return $new_env;
} }
public static function isMathOn() { public static function isMathOn() {
return !Less_Parser::$options['strictMath'] || self::$parensStack; return !Less_Parser::$options['strictMath'] || Less_Environment::$parensStack;
} }
public static function isPathRelative( $path ) { public static function isPathRelative( $path ) {
@ -104,14 +108,15 @@ class Less_Environment {
/** /**
* Canonicalize a path by resolving references to '/./', '/../' * Canonicalize a path by resolving references to '/./', '/../'
* Does not remove leading "../" * Does not remove leading "../"
* @param string $path or url * @param string path or url
* @return string Canonicalized path * @return string Canonicalized path
*
*/ */
public static function normalizePath( $path ) { public static function normalizePath( $path ) {
$segments = explode( '/', $path ); $segments = explode( '/', $path );
$segments = array_reverse( $segments ); $segments = array_reverse( $segments );
$path = []; $path = array();
$path_len = 0; $path_len = 0;
while ( $segments ) { while ( $segments ) {
@ -122,7 +127,6 @@ class Less_Environment {
break; break;
case '..': case '..':
// @phan-suppress-next-line PhanTypeInvalidDimOffset False positive
if ( !$path_len || ( $path[$path_len - 1] === '..' ) ) { if ( !$path_len || ( $path[$path_len - 1] === '..' ) ) {
$path[] = $segment; $path[] = $segment;
$path_len++; $path_len++;

View File

@ -1,6 +1,10 @@
<?php <?php
/** /**
* @private * Chunk Exception
*
* @package Less
* @subpackage exception
*/ */
class Less_Exception_Chunk extends Less_Exception_Parser { class Less_Exception_Chunk extends Less_Exception_Parser {
@ -11,11 +15,13 @@ class Less_Exception_Chunk extends Less_Exception_Parser {
protected $input_len; protected $input_len;
/** /**
* Constructor
*
* @param string $input * @param string $input
* @param Exception|null $previous Previous exception * @param Exception $previous Previous exception
* @param int|null $index The current parser index * @param integer $index The current parser index
* @param array|null $currentFile The file * @param Less_FileInfo|string $currentFile The file
* @param int $code The exception code * @param integer $code The exception code
*/ */
public function __construct( $input, Exception $previous = null, $index = null, $currentFile = null, $code = 0 ) { public function __construct( $input, Exception $previous = null, $index = null, $currentFile = null, $code = 0 ) {
$this->message = 'ParseError: Unexpected input'; // default message $this->message = 'ParseError: Unexpected input'; // default message
@ -34,6 +40,7 @@ class Less_Exception_Chunk extends Less_Exception_Parser {
/** /**
* See less.js chunks() * See less.js chunks()
* We don't actually need the chunks * We don't actually need the chunks
*
*/ */
protected function Chunks() { protected function Chunks() {
$level = 0; $level = 0;
@ -88,9 +95,7 @@ class Less_Exception_Chunk extends Less_Exception_Parser {
break; break;
// \ // \
case 92: case 92:
if ( $this->parserCurrentIndex < $this->input_len - 1 ) { if ( $this->parserCurrentIndex < $this->input_len - 1 ) { $this->parserCurrentIndex++; break;
$this->parserCurrentIndex++;
break;
} }
return $this->fail( "unescaped `\\`" ); return $this->fail( "unescaped `\\`" );
@ -100,12 +105,11 @@ class Less_Exception_Chunk extends Less_Exception_Parser {
case 96: case 96:
$matched = 0; $matched = 0;
$currentChunkStartIndex = $this->parserCurrentIndex; $currentChunkStartIndex = $this->parserCurrentIndex;
for ( $this->parserCurrentIndex += 1; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++ ) { for ( $this->parserCurrentIndex = $this->parserCurrentIndex + 1; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++ ) {
$cc2 = $this->CharCode( $this->parserCurrentIndex ); $cc2 = $this->CharCode( $this->parserCurrentIndex );
if ( $cc2 > 96 ) { continue; if ( $cc2 > 96 ) { continue;
} }
if ( $cc2 == $cc ) { $matched = 1; if ( $cc2 == $cc ) { $matched = 1; break;
break;
} }
if ( $cc2 == 92 ) { // \ if ( $cc2 == 92 ) { // \
if ( $this->parserCurrentIndex == $this->input_len - 1 ) { if ( $this->parserCurrentIndex == $this->input_len - 1 ) {
@ -125,7 +129,7 @@ break;
$cc2 = $this->CharCode( $this->parserCurrentIndex + 1 ); $cc2 = $this->CharCode( $this->parserCurrentIndex + 1 );
if ( $cc2 == 47 ) { if ( $cc2 == 47 ) {
// //, find lnfeed // //, find lnfeed
for ( $this->parserCurrentIndex += 2; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++ ) { for ( $this->parserCurrentIndex = $this->parserCurrentIndex + 2; $this->parserCurrentIndex < $this->input_len; $this->parserCurrentIndex++ ) {
$cc2 = $this->CharCode( $this->parserCurrentIndex ); $cc2 = $this->CharCode( $this->parserCurrentIndex );
if ( ( $cc2 <= 13 ) && ( ( $cc2 == 10 ) || ( $cc2 == 13 ) ) ) { break; if ( ( $cc2 <= 13 ) && ( ( $cc2 == 10 ) || ( $cc2 == 13 ) ) ) { break;
} }
@ -133,7 +137,7 @@ break;
} else if ( $cc2 == 42 ) { } else if ( $cc2 == 42 ) {
// /*, find */ // /*, find */
$lastMultiComment = $currentChunkStartIndex = $this->parserCurrentIndex; $lastMultiComment = $currentChunkStartIndex = $this->parserCurrentIndex;
for ( $this->parserCurrentIndex += 2; $this->parserCurrentIndex < $this->input_len - 1; $this->parserCurrentIndex++ ) { for ( $this->parserCurrentIndex = $this->parserCurrentIndex + 2; $this->parserCurrentIndex < $this->input_len - 1; $this->parserCurrentIndex++ ) {
$cc2 = $this->CharCode( $this->parserCurrentIndex ); $cc2 = $this->CharCode( $this->parserCurrentIndex );
if ( $cc2 == 125 ) { $lastMultiCommentEndBrace = $this->parserCurrentIndex; if ( $cc2 == 125 ) { $lastMultiCommentEndBrace = $this->parserCurrentIndex;
} }

View File

@ -2,6 +2,9 @@
/** /**
* Compiler Exception * Compiler Exception
*
* @package Less
* @subpackage exception
*/ */
class Less_Exception_Compiler extends Less_Exception_Parser { class Less_Exception_Compiler extends Less_Exception_Parser {

View File

@ -2,36 +2,46 @@
/** /**
* Parser Exception * Parser Exception
*
* @package Less
* @subpackage exception
*/ */
class Less_Exception_Parser extends Exception { class Less_Exception_Parser extends Exception {
/** /**
* The current file * The current file
* *
* @var array * @var Less_ImportedFile
*/ */
public $currentFile; public $currentFile;
/** /**
* The current parser index * The current parser index
* *
* @var int * @var integer
*/ */
public $index; public $index;
protected $input; protected $input;
protected $details = []; protected $details = array();
/** /**
* @param string|null $message * Constructor
* @param Exception|null $previous Previous exception *
* @param int|null $index The current parser index * @param string $message
* @param array|null $currentFile The file * @param Exception $previous Previous exception
* @param int $code The exception code * @param integer $index The current parser index
* @param Less_FileInfo|string $currentFile The file
* @param integer $code The exception code
*/ */
public function __construct( $message = null, Exception $previous = null, $index = null, $currentFile = null, $code = 0 ) { public function __construct( $message = null, Exception $previous = null, $index = null, $currentFile = null, $code = 0 ) {
if ( PHP_VERSION_ID < 50300 ) {
$this->previous = $previous;
parent::__construct( $message, $code );
} else {
parent::__construct( $message, $code, $previous ); parent::__construct( $message, $code, $previous );
}
$this->currentFile = $currentFile; $this->currentFile = $currentFile;
$this->index = $index; $this->index = $index;
@ -46,7 +56,9 @@ class Less_Exception_Parser extends Exception {
} }
/** /**
* Set a message based on the exception info * Converts the exception to string
*
* @return string
*/ */
public function genMessage() { public function genMessage() {
if ( $this->currentFile && $this->currentFile['filename'] ) { if ( $this->currentFile && $this->currentFile['filename'] ) {
@ -66,16 +78,17 @@ class Less_Exception_Parser extends Exception {
$last_line = min( $count, $start_line + 6 ); $last_line = min( $count, $start_line + 6 );
$num_len = strlen( $last_line ); $num_len = strlen( $last_line );
for ( $i = $start_line; $i < $last_line; $i++ ) { for ( $i = $start_line; $i < $last_line; $i++ ) {
$this->message .= "\n" . str_pad( (string)( $i + 1 ), $num_len, '0', STR_PAD_LEFT ) . '| ' . $lines[$i]; $this->message .= "\n".str_pad( $i + 1, $num_len, '0', STR_PAD_LEFT ).'| '.$lines[$i];
} }
} }
} }
} }
/** /**
* Returns the line number the error was encountered * Returns the line number the error was encountered
* *
* @return int * @return integer
*/ */
public function getLineNumber() { public function getLineNumber() {
if ( $this->index ) { if ( $this->index ) {
@ -92,7 +105,7 @@ class Less_Exception_Parser extends Exception {
/** /**
* Returns the column the error was encountered * Returns the column the error was encountered
* *
* @return int * @return integer
*/ */
public function getColumn() { public function getColumn() {
$part = substr( $this->input, 0, $this->index ); $part = substr( $this->input, 0, $this->index );

View File

@ -2,22 +2,23 @@
/** /**
* Builtin functions * Builtin functions
* @see https://lesscss.org/functions/ *
* @package Less
* @subpackage function
* @see http://lesscss.org/functions/
*/ */
class Less_Functions { class Less_Functions {
public $env; public $env;
public $currentFileInfo; public $currentFileInfo;
function __construct( $env, array $currentFileInfo = null ) { function __construct( $env, $currentFileInfo = null ) {
$this->env = $env; $this->env = $env;
$this->currentFileInfo = $currentFileInfo; $this->currentFileInfo = $currentFileInfo;
} }
/** /**
* @param string $op * @param string $op
* @param float $a
* @param float $b
*/ */
public static function operate( $op, $a, $b ) { public static function operate( $op, $a, $b ) {
switch ( $op ) { switch ( $op ) {
@ -62,20 +63,20 @@ class Less_Functions {
if ( $n instanceof Less_Tree_Dimension && $n->unit->is( '%' ) ) { if ( $n instanceof Less_Tree_Dimension && $n->unit->is( '%' ) ) {
return (float)$n->value * $size / 100; return (float)$n->value * $size / 100;
} else { } else {
return self::number( $n ); return Less_Functions::number( $n );
} }
} }
public function rgb( $r = null, $g = null, $b = null ) { public function rgb( $r = null, $g = null, $b = null ) {
if ( $r === null || $g === null || $b === null ) { if ( is_null( $r ) || is_null( $g ) || is_null( $b ) ) {
throw new Less_Exception_Compiler( "rgb expects three parameters" ); throw new Less_Exception_Compiler( "rgb expects three parameters" );
} }
return $this->rgba( $r, $g, $b, 1.0 ); return $this->rgba( $r, $g, $b, 1.0 );
} }
public function rgba( $r = null, $g = null, $b = null, $a = null ) { public function rgba( $r = null, $g = null, $b = null, $a = null ) {
$rgb = [ $r, $g, $b ]; $rgb = array( $r, $g, $b );
$rgb = array_map( [ 'Less_Functions','scaled' ], $rgb ); $rgb = array_map( array( 'Less_Functions','scaled' ), $rgb );
$a = self::number( $a ); $a = self::number( $a );
return new Less_Tree_Color( $rgb, $a ); return new Less_Tree_Color( $rgb, $a );
@ -95,30 +96,18 @@ class Less_Functions {
$m1 = $l * 2 - $m2; $m1 = $l * 2 - $m2;
return $this->rgba( return $this->rgba( self::hsla_hue( $h + 1 / 3, $m1, $m2 ) * 255,
self::hsla_hue( $h + 1 / 3, $m1, $m2 ) * 255,
self::hsla_hue( $h, $m1, $m2 ) * 255, self::hsla_hue( $h, $m1, $m2 ) * 255,
self::hsla_hue( $h - 1 / 3, $m1, $m2 ) * 255, self::hsla_hue( $h - 1 / 3, $m1, $m2 ) * 255,
$a $a );
);
} }
/** /**
* @param float $h * @param double $h
* @param float $m1
* @param float $m2
*/ */
public function hsla_hue( $h, $m1, $m2 ) { public function hsla_hue( $h, $m1, $m2 ) {
$h = $h < 0 ? $h + 1 : ( $h > 1 ? $h - 1 : $h ); $h = $h < 0 ? $h + 1 : ( $h > 1 ? $h - 1 : $h );
if ( $h * 6 < 1 ) { if ( $h * 6 < 1 ) return $m1 + ( $m2 - $m1 ) * $h * 6; else if ( $h * 2 < 1 ) return $m2; else if ( $h * 3 < 2 ) return $m1 + ( $m2 - $m1 ) * ( 2 / 3 - $h ) * 6; else return $m1;
return $m1 + ( $m2 - $m1 ) * $h * 6;
} elseif ( $h * 2 < 1 ) {
return $m2;
} elseif ( $h * 3 < 2 ) {
return $m1 + ( $m2 - $m1 ) * ( 2 / 3 - $h ) * 6;
} else {
return $m1;
}
} }
public function hsv( $h, $s, $v ) { public function hsv( $h, $s, $v ) {
@ -126,42 +115,33 @@ class Less_Functions {
} }
/** /**
* @param Less_Tree|float $h * @param double $a
* @param Less_Tree|float $s
* @param Less_Tree|float $v
* @param float $a
*/ */
public function hsva( $h, $s, $v, $a ) { public function hsva( $h, $s, $v, $a ) {
$h = ( ( self::number( $h ) % 360 ) / 360 ) * 360; $h = ( ( Less_Functions::number( $h ) % 360 ) / 360 ) * 360;
$s = self::number( $s ); $s = Less_Functions::number( $s );
$v = self::number( $v ); $v = Less_Functions::number( $v );
$a = self::number( $a ); $a = Less_Functions::number( $a );
$i = floor( (int)( $h / 60 ) % 6 ); $i = floor( ( $h / 60 ) % 6 );
$f = ( $h / 60 ) - $i; $f = ( $h / 60 ) - $i;
$vs = [ $vs = array( $v,
$v,
$v * ( 1 - $s ), $v * ( 1 - $s ),
$v * ( 1 - $f * $s ), $v * ( 1 - $f * $s ),
$v * ( 1 - ( 1 - $f ) * $s ) $v * ( 1 - ( 1 - $f ) * $s ) );
];
$perm = [ $perm = array( array( 0, 3, 1 ),
[ 0, 3, 1 ], array( 2, 0, 1 ),
[ 2, 0, 1 ], array( 1, 0, 3 ),
[ 1, 0, 3 ], array( 1, 2, 0 ),
[ 1, 2, 0 ], array( 3, 1, 0 ),
[ 3, 1, 0 ], array( 0, 1, 2 ) );
[ 0, 1, 2 ]
];
return $this->rgba( return $this->rgba( $vs[$perm[$i][0]] * 255,
$vs[$perm[$i][0]] * 255,
$vs[$perm[$i][1]] * 255, $vs[$perm[$i][1]] * 255,
$vs[$perm[$i][2]] * 255, $vs[$perm[$i][2]] * 255,
$a $a );
);
} }
public function hue( $color = null ) { public function hue( $color = null ) {
@ -295,8 +275,7 @@ class Less_Functions {
} }
/** /**
* @param Less_Tree_Color|null $color * @param Less_Tree_Dimension $amount
* @param Less_Tree_Dimension|null $amount
*/ */
public function desaturate( $color = null, $amount = null ) { public function desaturate( $color = null, $amount = null ) {
if ( !$color instanceof Less_Tree_Color ) { if ( !$color instanceof Less_Tree_Color ) {
@ -307,6 +286,7 @@ class Less_Functions {
} }
$hsl = $color->toHSL(); $hsl = $color->toHSL();
$hsl['s'] -= $amount->value / 100; $hsl['s'] -= $amount->value / 100;
$hsl['s'] = self::clamp( $hsl['s'] ); $hsl['s'] = self::clamp( $hsl['s'] );
@ -405,13 +385,11 @@ class Less_Functions {
// //
// Copyright (c) 2006-2009 Hampton Catlin, Nathan Weizenbaum, and Chris Eppstein // Copyright (c) 2006-2009 Hampton Catlin, Nathan Weizenbaum, and Chris Eppstein
// https://sass-lang.com/ // http://sass-lang.com
// //
/** /**
* @param Less_Tree|null $color1 * @param Less_Tree_Color $color1
* @param Less_Tree|null $color2
* @param Less_Tree|null $weight
*/ */
public function mix( $color1 = null, $color2 = null, $weight = null ) { public function mix( $color1 = null, $color2 = null, $weight = null ) {
if ( !$color1 instanceof Less_Tree_Color ) { if ( !$color1 instanceof Less_Tree_Color ) {
@ -436,11 +414,9 @@ class Less_Functions {
$w1 = ( ( ( ( $w * $a ) == -1 ) ? $w : ( $w + $a ) / ( 1 + $w * $a ) ) + 1 ) / 2; $w1 = ( ( ( ( $w * $a ) == -1 ) ? $w : ( $w + $a ) / ( 1 + $w * $a ) ) + 1 ) / 2;
$w2 = 1 - $w1; $w2 = 1 - $w1;
$rgb = [ $rgb = array( $color1->rgb[0] * $w1 + $color2->rgb[0] * $w2,
$color1->rgb[0] * $w1 + $color2->rgb[0] * $w2,
$color1->rgb[1] * $w1 + $color2->rgb[1] * $w2, $color1->rgb[1] * $w1 + $color2->rgb[1] * $w2,
$color1->rgb[2] * $w1 + $color2->rgb[2] * $w2 $color1->rgb[2] * $w1 + $color2->rgb[2] * $w2 );
];
$alpha = $color1->alpha * $p + $color2->alpha * ( 1 - $p ); $alpha = $color1->alpha * $p + $color2->alpha * ( 1 - $p );
@ -480,7 +456,7 @@ class Less_Functions {
if ( !$threshold ) { if ( !$threshold ) {
$threshold = 0.43; $threshold = 0.43;
} else { } else {
$threshold = self::number( $threshold ); $threshold = Less_Functions::number( $threshold );
} }
if ( $color->luma() < $threshold ) { if ( $color->luma() < $threshold ) {
@ -498,7 +474,7 @@ class Less_Functions {
} }
public function escape( $str ) { public function escape( $str ) {
$revert = [ '%21' => '!', '%2A' => '*', '%27' => "'",'%3F' => '?','%26' => '&','%2C' => ',','%2F' => '/','%40' => '@','%2B' => '+','%24' => '$' ]; $revert = array( '%21' => '!', '%2A' => '*', '%27' => "'",'%3F' => '?','%26' => '&','%2C' => ',','%2F' => '/','%40' => '@','%2B' => '+','%24' => '$' );
return new Less_Tree_Anonymous( strtr( rawurlencode( $str->value ), $revert ) ); return new Less_Tree_Anonymous( strtr( rawurlencode( $str->value ), $revert ) );
} }
@ -674,8 +650,7 @@ class Less_Functions {
} }
/** /**
* @param bool $isMin * @param boolean $isMin
* @param array<Less_Tree> $args
*/ */
private function _minmax( $isMin, $args ) { private function _minmax( $isMin, $args ) {
$arg_count = count( $args ); $arg_count = count( $args );
@ -688,24 +663,18 @@ class Less_Functions {
$unitClone = null; $unitClone = null;
$unitStatic = null; $unitStatic = null;
// elems only contains original argument values. $order = array(); // elems only contains original argument values.
$order = []; $values = array(); // key is the unit.toString() for unified tree.Dimension values,
// key is the unit.toString() for unified tree.Dimension values,
// value is the index into the order array. // value is the index into the order array.
$values = [];
for ( $i = 0; $i < $arg_count; $i++ ) { for ( $i = 0; $i < $arg_count; $i++ ) {
$current = $args[$i]; $current = $args[$i];
if ( !( $current instanceof Less_Tree_Dimension ) ) { if ( !( $current instanceof Less_Tree_Dimension ) ) {
// @phan-suppress-next-line PhanUndeclaredProperty Checked Less_Tree->value if ( is_array( $args[$i]->value ) ) {
if ( property_exists( $args[$i], 'value' ) && is_array( $args[$i]->value ) ) {
// @phan-suppress-next-line PhanUndeclaredProperty Checked Less_Tree->value
$args[] = $args[$i]->value; $args[] = $args[$i]->value;
} }
continue; continue;
} }
// PhanTypeInvalidDimOffset -- False positive, safe after continue or non-first iterations
'@phan-var non-empty-list<Less_Tree_Dimension> $order';
if ( $current->unit->toString() === '' && !$unitClone ) { if ( $current->unit->toString() === '' && !$unitClone ) {
$temp = new Less_Tree_Dimension( $current->value, $unitClone ); $temp = new Less_Tree_Dimension( $current->value, $unitClone );
@ -756,9 +725,9 @@ class Less_Functions {
if ( count( $order ) == 1 ) { if ( count( $order ) == 1 ) {
return $order[0]; return $order[0];
} }
$args = []; $args = array();
foreach ( $order as $a ) { foreach ( $order as $a ) {
$args[] = $a->toCSS(); $args[] = $a->toCSS( $this->env );
} }
return new Less_Tree_Anonymous( ( $isMin ? 'min(' : 'max(' ) . implode( Less_Environment::$_outputMap[','], $args ).')' ); return new Less_Tree_Anonymous( ( $isMin ? 'min(' : 'max(' ) . implode( Less_Environment::$_outputMap[','], $args ).')' );
} }
@ -838,12 +807,10 @@ class Less_Functions {
} }
/** /**
* @param Less_Tree $n * @param string $unit
* @param Less_Tree|string $unit
*/ */
public function isunit( $n, $unit ) { public function isunit( $n, $unit ) {
if ( is_object( $unit ) && property_exists( $unit, 'value' ) ) { if ( is_object( $unit ) && property_exists( $unit, 'value' ) ) {
// @phan-suppress-next-line PhanUndeclaredProperty Checked Less_Tree->value
$unit = $unit->value; $unit = $unit->value;
} }
@ -851,7 +818,6 @@ class Less_Functions {
} }
/** /**
* @param Less_Tree $n
* @param string $type * @param string $type
*/ */
private function _isa( $n, $type ) { private function _isa( $n, $type ) {
@ -900,12 +866,11 @@ class Less_Functions {
$filePath = str_replace( '\\', '/', $filePath ); $filePath = str_replace( '\\', '/', $filePath );
if ( Less_Environment::isPathRelative( $filePath ) ) { if ( Less_Environment::isPathRelative( $filePath ) ) {
$currentFileInfo = $this->currentFileInfo;
'@phan-var array $currentFileInfo';
if ( Less_Parser::$options['relativeUrls'] ) { if ( Less_Parser::$options['relativeUrls'] ) {
$temp = $currentFileInfo['currentDirectory']; $temp = $this->currentFileInfo['currentDirectory'];
} else { } else {
$temp = $currentFileInfo['entryPath']; $temp = $this->currentFileInfo['entryPath'];
} }
if ( !empty( $temp ) ) { if ( !empty( $temp ) ) {
@ -930,7 +895,7 @@ class Less_Functions {
$mimetype = Less_Mime::lookup( $filePath ); $mimetype = Less_Mime::lookup( $filePath );
$charset = Less_Mime::charsets_lookup( $mimetype ); $charset = Less_Mime::charsets_lookup( $mimetype );
$useBase64 = !in_array( $charset, [ 'US-ASCII', 'UTF-8' ] ); $useBase64 = !in_array( $charset, array( 'US-ASCII', 'UTF-8' ) );
if ( $useBase64 ) { $mimetype .= ';base64'; if ( $useBase64 ) { $mimetype .= ';base64';
} }
@ -949,8 +914,8 @@ class Less_Functions {
$DATA_URI_MAX_KB = 32; $DATA_URI_MAX_KB = 32;
$fileSizeInKB = round( strlen( $buf ) / 1024 ); $fileSizeInKB = round( strlen( $buf ) / 1024 );
if ( $fileSizeInKB >= $DATA_URI_MAX_KB ) { if ( $fileSizeInKB >= $DATA_URI_MAX_KB ) {
$url = new Less_Tree_Url( ( $filePathNode ?: $mimetypeNode ), $this->currentFileInfo ); $url = new Less_Tree_Url( ( $filePathNode ? $filePathNode : $mimetypeNode ), $this->currentFileInfo );
return $url->compile( $this->env ); return $url->compile( $this );
} }
if ( $buf ) { if ( $buf ) {
@ -1044,18 +1009,17 @@ class Less_Functions {
* @return string The encoded string * @return string The encoded string
*/ */
public static function encodeURIComponent( $string ) { public static function encodeURIComponent( $string ) {
$revert = [ '%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')' ]; $revert = array( '%21' => '!', '%2A' => '*', '%27' => "'", '%28' => '(', '%29' => ')' );
return strtr( rawurlencode( $string ), $revert ); return strtr( rawurlencode( $string ), $revert );
} }
// Color Blending // Color Blending
// ref: https://www.w3.org/TR/compositing-1/ // ref: http://www.w3.org/TR/compositing-1
public function colorBlend( $mode, $color1, $color2 ) { public function colorBlend( $mode, $color1, $color2 ) {
// backdrop $ab = $color1->alpha; // backdrop
$ab = $color1->alpha; $as = $color2->alpha; // source
// source $r = array(); // result
$as = $color2->alpha;
$result = [];
$ar = $as + $ab * ( 1 - $as ); $ar = $as + $ab * ( 1 - $as );
for ( $i = 0; $i < 3; $i++ ) { for ( $i = 0; $i < 3; $i++ ) {
@ -1065,10 +1029,10 @@ class Less_Functions {
if ( $ar ) { if ( $ar ) {
$cr = ( $as * $cs + $ab * ( $cb - $as * ( $cb + $cs - $cr ) ) ) / $ar; $cr = ( $as * $cs + $ab * ( $cb - $as * ( $cb + $cs - $cr ) ) ) / $ar;
} }
$result[$i] = $cr * 255; $r[$i] = $cr * 255;
} }
return new Less_Tree_Color( $result, $ar ); return new Less_Tree_Color( $r, $ar );
} }
public function multiply( $color1 = null, $color2 = null ) { public function multiply( $color1 = null, $color2 = null ) {
@ -1079,7 +1043,7 @@ class Less_Functions {
throw new Less_Exception_Compiler( 'The second argument to multiply must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); throw new Less_Exception_Compiler( 'The second argument to multiply must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
} }
return $this->colorBlend( [ $this,'colorBlendMultiply' ], $color1, $color2 ); return $this->colorBlend( array( $this,'colorBlendMultiply' ), $color1, $color2 );
} }
private function colorBlendMultiply( $cb, $cs ) { private function colorBlendMultiply( $cb, $cs ) {
@ -1094,7 +1058,7 @@ class Less_Functions {
throw new Less_Exception_Compiler( 'The second argument to screen must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); throw new Less_Exception_Compiler( 'The second argument to screen must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
} }
return $this->colorBlend( [ $this,'colorBlendScreen' ], $color1, $color2 ); return $this->colorBlend( array( $this,'colorBlendScreen' ), $color1, $color2 );
} }
private function colorBlendScreen( $cb, $cs ) { private function colorBlendScreen( $cb, $cs ) {
@ -1109,7 +1073,7 @@ class Less_Functions {
throw new Less_Exception_Compiler( 'The second argument to overlay must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); throw new Less_Exception_Compiler( 'The second argument to overlay must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
} }
return $this->colorBlend( [ $this,'colorBlendOverlay' ], $color1, $color2 ); return $this->colorBlend( array( $this,'colorBlendOverlay' ), $color1, $color2 );
} }
private function colorBlendOverlay( $cb, $cs ) { private function colorBlendOverlay( $cb, $cs ) {
@ -1127,7 +1091,7 @@ class Less_Functions {
throw new Less_Exception_Compiler( 'The second argument to softlight must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); throw new Less_Exception_Compiler( 'The second argument to softlight must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
} }
return $this->colorBlend( [ $this,'colorBlendSoftlight' ], $color1, $color2 ); return $this->colorBlend( array( $this,'colorBlendSoftlight' ), $color1, $color2 );
} }
private function colorBlendSoftlight( $cb, $cs ) { private function colorBlendSoftlight( $cb, $cs ) {
@ -1149,7 +1113,7 @@ class Less_Functions {
throw new Less_Exception_Compiler( 'The second argument to hardlight must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); throw new Less_Exception_Compiler( 'The second argument to hardlight must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
} }
return $this->colorBlend( [ $this,'colorBlendHardlight' ], $color1, $color2 ); return $this->colorBlend( array( $this,'colorBlendHardlight' ), $color1, $color2 );
} }
private function colorBlendHardlight( $cb, $cs ) { private function colorBlendHardlight( $cb, $cs ) {
@ -1164,7 +1128,7 @@ class Less_Functions {
throw new Less_Exception_Compiler( 'The second argument to difference must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); throw new Less_Exception_Compiler( 'The second argument to difference must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
} }
return $this->colorBlend( [ $this,'colorBlendDifference' ], $color1, $color2 ); return $this->colorBlend( array( $this,'colorBlendDifference' ), $color1, $color2 );
} }
private function colorBlendDifference( $cb, $cs ) { private function colorBlendDifference( $cb, $cs ) {
@ -1179,7 +1143,7 @@ class Less_Functions {
throw new Less_Exception_Compiler( 'The second argument to exclusion must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); throw new Less_Exception_Compiler( 'The second argument to exclusion must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
} }
return $this->colorBlend( [ $this,'colorBlendExclusion' ], $color1, $color2 ); return $this->colorBlend( array( $this,'colorBlendExclusion' ), $color1, $color2 );
} }
private function colorBlendExclusion( $cb, $cs ) { private function colorBlendExclusion( $cb, $cs ) {
@ -1194,7 +1158,7 @@ class Less_Functions {
throw new Less_Exception_Compiler( 'The second argument to average must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); throw new Less_Exception_Compiler( 'The second argument to average must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
} }
return $this->colorBlend( [ $this,'colorBlendAverage' ], $color1, $color2 ); return $this->colorBlend( array( $this,'colorBlendAverage' ), $color1, $color2 );
} }
// non-w3c functions: // non-w3c functions:
@ -1210,7 +1174,7 @@ class Less_Functions {
throw new Less_Exception_Compiler( 'The second argument to negation must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) ); throw new Less_Exception_Compiler( 'The second argument to negation must be a color' . ( $color2 instanceof Less_Tree_Expression ? ' (did you forgot commas?)' : '' ) );
} }
return $this->colorBlend( [ $this,'colorBlendNegation' ], $color1, $color2 ); return $this->colorBlend( array( $this,'colorBlendNegation' ), $color1, $color2 );
} }
public function colorBlendNegation( $cb, $cs ) { public function colorBlendNegation( $cb, $cs ) {

View File

@ -1,14 +1,16 @@
<?php <?php
/** /**
* Mime lookup * Mime lookup
* *
* @private * @package Less
* @subpackage node
*/ */
class Less_Mime { class Less_Mime {
// this map is intentionally incomplete // this map is intentionally incomplete
// if you want more, install 'mime' dep // if you want more, install 'mime' dep
private static $types = [ static $_types = array(
'.htm' => 'text/html', '.htm' => 'text/html',
'.html' => 'text/html', '.html' => 'text/html',
'.gif' => 'image/gif', '.gif' => 'image/gif',
@ -20,13 +22,16 @@ class Less_Mime {
'.eot' => 'application/vnd.ms-fontobject', '.eot' => 'application/vnd.ms-fontobject',
'.woff' => 'application/x-font-woff', '.woff' => 'application/x-font-woff',
'.svg' => 'image/svg+xml', '.svg' => 'image/svg+xml',
]; );
public static function lookup( $filepath ) { public static function lookup( $filepath ) {
$parts = explode( '.', $filepath ); $parts = explode( '.', $filepath );
$ext = '.'.strtolower( array_pop( $parts ) ); $ext = '.'.strtolower( array_pop( $parts ) );
return self::$types[$ext] ?? null; if ( !isset( self::$_types[$ext] ) ) {
return null;
}
return self::$_types[$ext];
} }
public static function charsets_lookup( $type = null ) { public static function charsets_lookup( $type = null ) {

View File

@ -1,24 +1,26 @@
<?php <?php
/** /**
* Parser output * Parser output
* *
* @private * @package Less
* @subpackage output
*/ */
class Less_Output { class Less_Output {
/** /**
* Output holder * Output holder
* *
* @var string[] * @var string
*/ */
protected $strs = []; protected $strs = array();
/** /**
* Adds a chunk to the stack * Adds a chunk to the stack
* *
* @param string $chunk The chunk to output * @param string $chunk The chunk to output
* @param array|null $fileInfo The file information * @param Less_FileInfo $fileInfo The file information
* @param int $index The index * @param integer $index The index
* @param mixed $mapLines * @param mixed $mapLines
*/ */
public function add( $chunk, $fileInfo = null, $index = 0, $mapLines = null ) { public function add( $chunk, $fileInfo = null, $index = 0, $mapLines = null ) {
@ -28,7 +30,7 @@ class Less_Output {
/** /**
* Is the output empty? * Is the output empty?
* *
* @return bool * @return boolean
*/ */
public function isEmpty() { public function isEmpty() {
return count( $this->strs ) === 0; return count( $this->strs ) === 0;

View File

@ -1,8 +1,10 @@
<?php <?php
/** /**
* Parser output with source map * Parser output with source map
* *
* @private * @package Less
* @subpackage Output
*/ */
class Less_Output_Mapped extends Less_Output { class Less_Output_Mapped extends Less_Output {
@ -16,14 +18,14 @@ class Less_Output_Mapped extends Less_Output {
/** /**
* Current line * Current line
* *
* @var int * @var integer
*/ */
protected $lineNumber = 0; protected $lineNumber = 0;
/** /**
* Current column * Current column
* *
* @var int * @var integer
*/ */
protected $column = 0; protected $column = 0;
@ -32,7 +34,7 @@ class Less_Output_Mapped extends Less_Output {
* *
* @var array * @var array
*/ */
protected $contentsMap = []; protected $contentsMap = array();
/** /**
* Constructor * Constructor
@ -50,8 +52,8 @@ class Less_Output_Mapped extends Less_Output {
* The $index for less.php may be different from less.js since less.php does not chunkify inputs * The $index for less.php may be different from less.js since less.php does not chunkify inputs
* *
* @param string $chunk * @param string $chunk
* @param array|null $fileInfo * @param string $fileInfo
* @param int $index * @param integer $index
* @param mixed $mapLines * @param mixed $mapLines
*/ */
public function add( $chunk, $fileInfo = null, $index = 0, $mapLines = null ) { public function add( $chunk, $fileInfo = null, $index = 0, $mapLines = null ) {
@ -60,7 +62,7 @@ class Less_Output_Mapped extends Less_Output {
return; return;
} }
$sourceLines = []; $sourceLines = array();
$sourceColumns = ' '; $sourceColumns = ' ';
if ( $fileInfo ) { if ( $fileInfo ) {

File diff suppressed because it is too large Load Diff

View File

@ -1,29 +1,31 @@
<?php <?php
/** /**
* Encode / Decode Base64 VLQ. * Encode / Decode Base64 VLQ.
* *
* @private * @package Less
* @subpackage SourceMap
*/ */
class Less_SourceMap_Base64VLQ { class Less_SourceMap_Base64VLQ {
/** /**
* Shift * Shift
* *
* @var int * @var integer
*/ */
private $shift = 5; private $shift = 5;
/** /**
* Mask * Mask
* *
* @var int * @var integer
*/ */
private $mask = 0x1F; // == (1 << shift) == 0b00011111 private $mask = 0x1F; // == (1 << shift) == 0b00011111
/** /**
* Continuation bit * Continuation bit
* *
* @var int * @var integer
*/ */
private $continuationBit = 0x20; // == (mask - 1 ) == 0b00100000 private $continuationBit = 0x20; // == (mask - 1 ) == 0b00100000
@ -32,7 +34,7 @@ class Less_SourceMap_Base64VLQ {
* *
* @var array * @var array
*/ */
private $charToIntMap = [ private $charToIntMap = array(
'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, 'G' => 6, 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, 'G' => 6,
'H' => 7,'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13, 'H' => 7,'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, 'M' => 12, 'N' => 13,
'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19, 'U' => 20, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, 'S' => 18, 'T' => 19, 'U' => 20,
@ -42,14 +44,14 @@ class Less_SourceMap_Base64VLQ {
'q' => 42, 'r' => 43, 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, 'q' => 42, 'r' => 43, 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48,
'x' => 49, 'y' => 50, 'z' => 51, 0 => 52, 1 => 53, 2 => 54, 3 => 55, 4 => 56, 'x' => 49, 'y' => 50, 'z' => 51, 0 => 52, 1 => 53, 2 => 54, 3 => 55, 4 => 56,
5 => 57, 6 => 58, 7 => 59, 8 => 60, 9 => 61, '+' => 62, '/' => 63, 5 => 57, 6 => 58, 7 => 59, 8 => 60, 9 => 61, '+' => 62, '/' => 63,
]; );
/** /**
* Integer to char map * Integer to char map
* *
* @var array * @var array
*/ */
private $intToCharMap = [ private $intToCharMap = array(
0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E', 5 => 'F', 6 => 'G', 0 => 'A', 1 => 'B', 2 => 'C', 3 => 'D', 4 => 'E', 5 => 'F', 6 => 'G',
7 => 'H', 8 => 'I', 9 => 'J', 10 => 'K', 11 => 'L', 12 => 'M', 13 => 'N', 7 => 'H', 8 => 'I', 9 => 'J', 10 => 'K', 11 => 'L', 12 => 'M', 13 => 'N',
14 => 'O', 15 => 'P', 16 => 'Q', 17 => 'R', 18 => 'S', 19 => 'T', 20 => 'U', 14 => 'O', 15 => 'P', 16 => 'Q', 17 => 'R', 18 => 'S', 19 => 'T', 20 => 'U',
@ -60,7 +62,7 @@ class Less_SourceMap_Base64VLQ {
49 => 'x', 50 => 'y', 51 => 'z', 52 => '0', 53 => '1', 54 => '2', 55 => '3', 49 => 'x', 50 => 'y', 51 => 'z', 52 => '0', 53 => '1', 54 => '2', 55 => '3',
56 => '4', 57 => '5', 58 => '6', 59 => '7', 60 => '8', 61 => '9', 62 => '+', 56 => '4', 57 => '5', 58 => '6', 59 => '7', 60 => '8', 61 => '9', 62 => '+',
63 => '/', 63 => '/',
]; );
/** /**
* Constructor * Constructor
@ -81,7 +83,7 @@ class Less_SourceMap_Base64VLQ {
* 2 becomes 4 (100 binary), -2 becomes 5 (101 binary) * 2 becomes 4 (100 binary), -2 becomes 5 (101 binary)
* We generate the value for 32 bit machines, hence -2147483648 becomes 1, not 4294967297, * We generate the value for 32 bit machines, hence -2147483648 becomes 1, not 4294967297,
* even on a 64 bit machine. * even on a 64 bit machine.
* @param int $aValue * @param string $aValue
*/ */
public function toVLQSigned( $aValue ) { public function toVLQSigned( $aValue ) {
return 0xffffffff & ( $aValue < 0 ? ( ( -$aValue ) << 1 ) + 1 : ( $aValue << 1 ) + 0 ); return 0xffffffff & ( $aValue < 0 ? ( ( -$aValue ) << 1 ) + 1 : ( $aValue << 1 ) + 0 );
@ -96,7 +98,7 @@ class Less_SourceMap_Base64VLQ {
* Hence * Hence
* 1 becomes -2147483648 * 1 becomes -2147483648
* even on a 64 bit machine. * even on a 64 bit machine.
* @param int $aValue * @param integer $aValue
*/ */
public function fromVLQSigned( $aValue ) { public function fromVLQSigned( $aValue ) {
return $aValue & 1 ? $this->zeroFill( ~$aValue + 2, 1 ) | ( -1 - 0x7fffffff ) : $this->zeroFill( $aValue, 1 ); return $aValue & 1 ? $this->zeroFill( ~$aValue + 2, 1 ) | ( -1 - 0x7fffffff ) : $this->zeroFill( $aValue, 1 );
@ -105,7 +107,7 @@ class Less_SourceMap_Base64VLQ {
/** /**
* Return the base 64 VLQ encoded value. * Return the base 64 VLQ encoded value.
* *
* @param int $aValue The value to encode * @param string $aValue The value to encode
* @return string The encoded value * @return string The encoded value
*/ */
public function encode( $aValue ) { public function encode( $aValue ) {
@ -128,7 +130,7 @@ class Less_SourceMap_Base64VLQ {
* Return the value decoded from base 64 VLQ. * Return the value decoded from base 64 VLQ.
* *
* @param string $encoded The encoded value to decode * @param string $encoded The encoded value to decode
* @return int The decoded value * @return integer The decoded value
*/ */
public function decode( $encoded ) { public function decode( $encoded ) {
$vlq = 0; $vlq = 0;
@ -146,9 +148,9 @@ class Less_SourceMap_Base64VLQ {
/** /**
* Right shift with zero fill. * Right shift with zero fill.
* *
* @param int $a number to shift * @param integer $a number to shift
* @param int $b number of bits to shift * @param integer $b number of bits to shift
* @return int * @return integer
*/ */
public function zeroFill( $a, $b ) { public function zeroFill( $a, $b ) {
return ( $a >= 0 ) ? ( $a >> $b ) : ( $a >> $b ) & ( PHP_INT_MAX >> ( $b - 1 ) ); return ( $a >= 0 ) ? ( $a >> $b ) : ( $a >> $b ) & ( PHP_INT_MAX >> ( $b - 1 ) );
@ -157,13 +159,13 @@ class Less_SourceMap_Base64VLQ {
/** /**
* Encode single 6-bit digit as base64. * Encode single 6-bit digit as base64.
* *
* @param int $number * @param integer $number
* @return string * @return string
* @throws Exception If the number is invalid * @throws Exception If the number is invalid
*/ */
public function base64Encode( $number ) { public function base64Encode( $number ) {
if ( $number < 0 || $number > 63 ) { if ( $number < 0 || $number > 63 ) {
throw new Exception( sprintf( 'Invalid number "%s" given. Must be between 0 and 63.', (string)$number ) ); throw new Exception( sprintf( 'Invalid number "%s" given. Must be between 0 and 63.', $number ) );
} }
return $this->intToCharMap[$number]; return $this->intToCharMap[$number];
} }
@ -172,7 +174,7 @@ class Less_SourceMap_Base64VLQ {
* Decode single 6-bit digit from base64 * Decode single 6-bit digit from base64
* *
* @param string $char * @param string $char
* @return int * @return number
* @throws Exception If the number is invalid * @throws Exception If the number is invalid
*/ */
public function base64Decode( $char ) { public function base64Decode( $char ) {

View File

@ -1,8 +1,10 @@
<?php <?php
/** /**
* Source map generator * Source map generator
* *
* @private * @package Less
* @subpackage Output
*/ */
class Less_SourceMap_Generator extends Less_Configurable { class Less_SourceMap_Generator extends Less_Configurable {
@ -16,7 +18,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
* *
* @var array * @var array
*/ */
protected $defaultOptions = [ protected $defaultOptions = array(
// an optional source root, useful for relocating source files // an optional source root, useful for relocating source files
// on a server or removing repeated values in the 'sources' entry. // on a server or removing repeated values in the 'sources' entry.
// This value is prepended to the individual entries in the 'source' field. // This value is prepended to the individual entries in the 'source' field.
@ -39,7 +41,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
// base path for filename normalization // base path for filename normalization
'sourceMapBasepath' => '' 'sourceMapBasepath' => ''
]; );
/** /**
* The base64 VLQ encoder * The base64 VLQ encoder
@ -53,7 +55,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
* *
* @var array * @var array
*/ */
protected $mappings = []; protected $mappings = array();
/** /**
* The root node * The root node
@ -67,24 +69,23 @@ class Less_SourceMap_Generator extends Less_Configurable {
* *
* @var array * @var array
*/ */
protected $contentsMap = []; protected $contentsMap = array();
/** /**
* File to content map * File to content map
* *
* @var array * @var array
*/ */
protected $sources = []; protected $sources = array();
protected $source_keys = []; protected $source_keys = array();
/** /**
* Constructor * Constructor
* *
* @param Less_Tree_Ruleset $root The root node * @param Less_Tree_Ruleset $root The root node
* @param array $contentsMap
* @param array $options Array of options * @param array $options Array of options
*/ */
public function __construct( Less_Tree_Ruleset $root, $contentsMap, $options = [] ) { public function __construct( Less_Tree_Ruleset $root, $contentsMap, $options = array() ) {
$this->root = $root; $this->root = $root;
$this->contentsMap = $contentsMap; $this->contentsMap = $contentsMap;
$this->encoder = new Less_SourceMap_Base64VLQ(); $this->encoder = new Less_SourceMap_Base64VLQ();
@ -181,20 +182,20 @@ class Less_SourceMap_Generator extends Less_Configurable {
/** /**
* Adds a mapping * Adds a mapping
* *
* @param int $generatedLine The line number in generated file * @param integer $generatedLine The line number in generated file
* @param int $generatedColumn The column number in generated file * @param integer $generatedColumn The column number in generated file
* @param int $originalLine The line number in original file * @param integer $originalLine The line number in original file
* @param int $originalColumn The column number in original file * @param integer $originalColumn The column number in original file
* @param array $fileInfo The original source file * @param string $sourceFile The original source file
*/ */
public function addMapping( $generatedLine, $generatedColumn, $originalLine, $originalColumn, $fileInfo ) { public function addMapping( $generatedLine, $generatedColumn, $originalLine, $originalColumn, $fileInfo ) {
$this->mappings[] = [ $this->mappings[] = array(
'generated_line' => $generatedLine, 'generated_line' => $generatedLine,
'generated_column' => $generatedColumn, 'generated_column' => $generatedColumn,
'original_line' => $originalLine, 'original_line' => $originalLine,
'original_column' => $originalColumn, 'original_column' => $originalColumn,
'source_file' => $fileInfo['currentUri'] 'source_file' => $fileInfo['currentUri']
]; );
$this->sources[$fileInfo['currentUri']] = $fileInfo['filename']; $this->sources[$fileInfo['currentUri']] = $fileInfo['filename'];
} }
@ -206,7 +207,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
* @see https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit# * @see https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit#
*/ */
protected function generateJson() { protected function generateJson() {
$sourceMap = []; $sourceMap = array();
$mappings = $this->generateMappings(); $mappings = $this->generateMappings();
// File version (always the first entry in the object) and must be a positive integer. // File version (always the first entry in the object) and must be a positive integer.
@ -225,13 +226,13 @@ class Less_SourceMap_Generator extends Less_Configurable {
} }
// A list of original sources used by the 'mappings' entry. // A list of original sources used by the 'mappings' entry.
$sourceMap['sources'] = []; $sourceMap['sources'] = array();
foreach ( $this->sources as $source_uri => $source_filename ) { foreach ( $this->sources as $source_uri => $source_filename ) {
$sourceMap['sources'][] = $this->normalizeFilename( $source_filename ); $sourceMap['sources'][] = $this->normalizeFilename( $source_filename );
} }
// A list of symbol names used by the 'mappings' entry. // A list of symbol names used by the 'mappings' entry.
$sourceMap['names'] = []; $sourceMap['names'] = array();
// A string with the encoded mapping data. // A string with the encoded mapping data.
$sourceMap['mappings'] = $mappings; $sourceMap['mappings'] = $mappings;
@ -260,7 +261,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
if ( empty( $this->sources ) ) { if ( empty( $this->sources ) ) {
return; return;
} }
$content = []; $content = array();
foreach ( $this->sources as $sourceFile ) { foreach ( $this->sources as $sourceFile ) {
$content[] = file_get_contents( $sourceFile ); $content[] = file_get_contents( $sourceFile );
} }
@ -280,7 +281,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
$this->source_keys = array_flip( array_keys( $this->sources ) ); $this->source_keys = array_flip( array_keys( $this->sources ) );
// group mappings by generated line number. // group mappings by generated line number.
$groupedMap = $groupedMapEncoded = []; $groupedMap = $groupedMapEncoded = array();
foreach ( $this->mappings as $m ) { foreach ( $this->mappings as $m ) {
$groupedMap[$m['generated_line']][] = $m; $groupedMap[$m['generated_line']][] = $m;
} }
@ -293,7 +294,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
$groupedMapEncoded[] = ';'; $groupedMapEncoded[] = ';';
} }
$lineMapEncoded = []; $lineMapEncoded = array();
$lastGeneratedColumn = 0; $lastGeneratedColumn = 0;
foreach ( $line_map as $m ) { foreach ( $line_map as $m ) {
@ -329,7 +330,7 @@ class Less_SourceMap_Generator extends Less_Configurable {
* Finds the index for the filename * Finds the index for the filename
* *
* @param string $filename * @param string $filename
* @return int|false * @return integer|false
*/ */
protected function findFileIndex( $filename ) { protected function findFileIndex( $filename ) {
return $this->source_keys[$filename]; return $this->source_keys[$filename];
@ -338,7 +339,6 @@ class Less_SourceMap_Generator extends Less_Configurable {
/** /**
* fix windows paths * fix windows paths
* @param string $path * @param string $path
* @param bool $addEndSlash
* @return string * @return string
*/ */
public function fixWindowsPath( $path, $addEndSlash = false ) { public function fixWindowsPath( $path, $addEndSlash = false ) {

View File

@ -3,18 +3,12 @@
/** /**
* Tree * Tree
* *
* TODO: Callers often use `property_exists(, 'value')` to distinguish * @package Less
* tree nodes that are considerd value-holding. Refactor this to move * @subpackage tree
* the 'value' property that most subclasses implement to there, and use
* something else (special value, method, or intermediate class?) to
* signal whether a subclass is considered value-holding.
*/ */
class Less_Tree { class Less_Tree {
public $cache_string; public $cache_string;
public $parensInOp = false;
public $extendOnEveryPath;
public $allExtends;
public function toCSS() { public function toCSS() {
$output = new Less_Output(); $output = new Less_Output();
@ -31,12 +25,7 @@ class Less_Tree {
public function genCSS( $output ) { public function genCSS( $output ) {
} }
public function compile( $env ) {
return $this;
}
/** /**
* @param Less_Output $output
* @param Less_Tree_Ruleset[] $rules * @param Less_Tree_Ruleset[] $rules
*/ */
public static function outputRuleset( $output, $rules ) { public static function outputRuleset( $output, $rules ) {
@ -66,6 +55,7 @@ class Less_Tree {
} }
Less_Environment::$tabLevel--; Less_Environment::$tabLevel--;
$output->add( $tabSetStr.'}' ); $output->add( $tabSetStr.'}' );
} }
public function accept( $visitor ) { public function accept( $visitor ) {
@ -74,7 +64,6 @@ class Less_Tree {
public static function ReferencedArray( $rules ) { public static function ReferencedArray( $rules ) {
foreach ( $rules as $rule ) { foreach ( $rules as $rule ) {
if ( method_exists( $rule, 'markReferenced' ) ) { if ( method_exists( $rule, 'markReferenced' ) ) {
// @phan-suppress-next-line PhanUndeclaredMethod
$rule->markReferenced(); $rule->markReferenced();
} }
} }

View File

@ -1,6 +1,10 @@
<?php <?php
/** /**
* @private * Alpha
*
* @package Less
* @subpackage tree
*/ */
class Less_Tree_Alpha extends Less_Tree { class Less_Tree_Alpha extends Less_Tree {
public $value; public $value;

View File

@ -1,6 +1,10 @@
<?php <?php
/** /**
* @private * Anonymous
*
* @package Less
* @subpackage tree
*/ */
class Less_Tree_Anonymous extends Less_Tree { class Less_Tree_Anonymous extends Less_Tree {
public $value; public $value;
@ -11,8 +15,8 @@ class Less_Tree_Anonymous extends Less_Tree {
public $type = 'Anonymous'; public $type = 'Anonymous';
/** /**
* @param int $index * @param integer $index
* @param bool|null $mapLines * @param boolean $mapLines
*/ */
public function __construct( $value, $index = null, $currentFileInfo = null, $mapLines = null ) { public function __construct( $value, $index = null, $currentFileInfo = null, $mapLines = null ) {
$this->value = $value; $this->value = $value;
@ -21,7 +25,7 @@ class Less_Tree_Anonymous extends Less_Tree {
$this->currentFileInfo = $currentFileInfo; $this->currentFileInfo = $currentFileInfo;
} }
public function compile( $env ) { public function compile() {
return new Less_Tree_Anonymous( $this->value, $this->index, $this->currentFileInfo, $this->mapLines ); return new Less_Tree_Anonymous( $this->value, $this->index, $this->currentFileInfo, $this->mapLines );
} }

Some files were not shown because too many files have changed in this diff Show More