Skip to content

Commit

Permalink
Block (MarcusSchwarz#9)
Browse files Browse the repository at this point in the history
* Added object block

Blocks were handed around as \stdClass, this new class Block solves this.
All fields are public for now, so nothing breaks.

* Diversified Block by all its types and introduced type hints to methods handling a Block

* Code style

* Code style
  • Loading branch information
spoehner authored and MarcusSchwarz committed Mar 19, 2017
1 parent b38731e commit 5d7e298
Show file tree
Hide file tree
Showing 8 changed files with 525 additions and 49 deletions.
124 changes: 124 additions & 0 deletions src/LesserPhp/Block.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
namespace LesserPhp;

/**
* lesserphp
* https://www.maswaba.de/lesserphp
*
* LESS CSS compiler, adapted from http://lesscss.org
*
* Copyright 2013, Leaf Corcoran <leafot@gmail.com>
* Copyright 2016, Marcus Schwarz <github@maswaba.de>
* Copyright 2017, Stefan Pöhner <github@poe-php.de>
* Licensed under MIT or GPLv3, see LICENSE
*
* @package LesserPhp
*/

class Block
{
/**
* @var Block|null
*/
public $parent;

/**
* @var string|null
*/
public $type;

/**
* @var int
*/
public $id;

/**
* @var bool
*/
public $isVararg = false;

/**
* @var array|null
*/
public $tags;

/**
* @var array
*/
public $props = [];

/**
* @var Block[]
*/
public $children = [];

/**
* Position of this block.
*
* @var int
*/
public $count;

/**
* Add a reference to the parser so
* we can access the parser to throw errors
* or retrieve the sourceName of this block.
*
* @var Parser
*/
public $parser;

/**
* Block constructor.
*
* @param Parser $parser
* @param int $id
* @param int $count
* @param array|null $tags
* @param Block|null $parent
*/
public function __construct(Parser $parser, $id, $count, array $tags = null, Block $parent = null)
{
$this->parser = $parser;
$this->id = $id;
$this->count = $count;
$this->type = $this->getType();
$this->tags = $tags;
$this->parent = $parent;
}

/**
* @return string|null
*/
public function getType()
{
return null;
}

/**
* @param Parser $parser
* @param int $id
* @param int $count
* @param string|null $type
* @param array|null $tags
* @param Block|null $parent
*
* @return Block
*/
public static function factory(Parser $parser, $id, $count, $type = null, array $tags = null, Block $parent = null)
{
if ($type === null) {
$className = self::class;
} else {
$className = __NAMESPACE__ . '\Block\\' . ucfirst($type);
}

if (!class_exists($className)) {
throw new \UnexpectedValueException("Unknown block type: $type");
}

$block = new $className($parser, $id, $count, $tags, $parent);

return $block;
}
}
38 changes: 38 additions & 0 deletions src/LesserPhp/Block/Directive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
namespace LesserPhp\Block;

/**
* lesserphp
* https://www.maswaba.de/lesserphp
*
* LESS CSS compiler, adapted from http://lesscss.org
*
* Copyright 2013, Leaf Corcoran <leafot@gmail.com>
* Copyright 2016, Marcus Schwarz <github@maswaba.de>
* Copyright 2017, Stefan Pöhner <github@poe-php.de>
* Licensed under MIT or GPLv3, see LICENSE
*
* @package LesserPhp
*/
use LesserPhp\Block;

class Directive extends Block
{
/**
* @var string
*/
public $name;

/**
* @var mixed
*/
public $value;

/**
* @inheritdoc
*/
public function getType()
{
return 'directive';
}
}
28 changes: 28 additions & 0 deletions src/LesserPhp/Block/Media.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace LesserPhp\Block;

use LesserPhp\Block;

/**
* lesserphp
* https://www.maswaba.de/lesserphp
*
* LESS CSS compiler, adapted from http://lesscss.org
*
* Copyright 2013, Leaf Corcoran <leafot@gmail.com>
* Copyright 2016, Marcus Schwarz <github@maswaba.de>
* Copyright 2017, Stefan Pöhner <github@poe-php.de>
* Licensed under MIT or GPLv3, see LICENSE
*
* @package LesserPhp
*/
class Media extends Block
{
/**
* @inheritdoc
*/
public function getType()
{
return 'media';
}
}
28 changes: 28 additions & 0 deletions src/LesserPhp/Block/Root.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace LesserPhp\Block;

use LesserPhp\Block;

/**
* lesserphp
* https://www.maswaba.de/lesserphp
*
* LESS CSS compiler, adapted from http://lesscss.org
*
* Copyright 2013, Leaf Corcoran <leafot@gmail.com>
* Copyright 2016, Marcus Schwarz <github@maswaba.de>
* Copyright 2017, Stefan Pöhner <github@poe-php.de>
* Licensed under MIT or GPLv3, see LICENSE
*
* @package LesserPhp
*/
class Root extends Block
{
/**
* @inheritdoc
*/
public function getType()
{
return 'root';
}
}
29 changes: 29 additions & 0 deletions src/LesserPhp/Block/Ruleset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
namespace LesserPhp\Block;

/**
* lesserphp
* https://www.maswaba.de/lesserphp
*
* LESS CSS compiler, adapted from http://lesscss.org
*
* Copyright 2013, Leaf Corcoran <leafot@gmail.com>
* Copyright 2016, Marcus Schwarz <github@maswaba.de>
* Copyright 2017, Stefan Pöhner <github@poe-php.de>
* Licensed under MIT or GPLv3, see LICENSE
*
* @package LesserPhp
*/

class Ruleset extends Directive
{
/**
* @inheritdoc
*/
public function getType()
{
// this deliberately returns null
// the concept of a ruleset block does not exists, but it behaves like a directive without the directive type
return null;
}
}
32 changes: 16 additions & 16 deletions src/LesserPhp/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,11 @@ protected function compileImportedProps(array $props, $block, $out, $importDir)
*
* See lessc::compileProp()
*
* @param $block
* @param Block $block
*
* @throws \LesserPhp\Exception\GeneralException
*/
protected function compileBlock($block)
protected function compileBlock(Block $block)
{
switch ($block->type) {
case "root":
Expand All @@ -348,11 +348,11 @@ protected function compileBlock($block)
}

/**
* @param $block
* @param Block $block
*
* @throws \LesserPhp\Exception\GeneralException
*/
protected function compileCSSBlock($block)
protected function compileCSSBlock(Block $block)
{
$env = $this->pushEnv($this->env);

Expand All @@ -368,9 +368,9 @@ protected function compileCSSBlock($block)
}

/**
* @param $media
* @param Block\Media $media
*/
protected function compileMedia($media)
protected function compileMedia(Block\Media $media)
{
$env = $this->pushEnv($this->env, $media);
$parentScope = $this->mediaParent($this->scope);
Expand Down Expand Up @@ -414,10 +414,10 @@ protected function mediaParent($scope)
}

/**
* @param $block
* @param Block $block
* @param string[] $selectors
*/
protected function compileNestedBlock($block, array $selectors)
protected function compileNestedBlock(Block $block, array $selectors)
{
$this->pushEnv($this->env, $block);
$this->scope = $this->makeOutputBlock($block->type, $selectors);
Expand All @@ -430,9 +430,9 @@ protected function compileNestedBlock($block, array $selectors)
}

/**
* @param $root
* @param Block $root
*/
protected function compileRoot($root)
protected function compileRoot(Block $root)
{
$this->pushEnv($this->env);
$this->scope = $this->makeOutputBlock($root->type);
Expand All @@ -441,12 +441,12 @@ protected function compileRoot($root)
}

/**
* @param $block
* @param Block $block
* @param \stdClass $out
*
* @throws \LesserPhp\Exception\GeneralException
*/
protected function compileProps($block, $out)
protected function compileProps(Block $block, $out)
{
foreach ($this->sortProps($block->props) as $prop) {
$this->compileProp($prop, $block, $out);
Expand Down Expand Up @@ -958,12 +958,12 @@ protected function zipSetArgs(array $args, $orderedValues, $keywordValues)
* compile a prop and update $lines or $blocks appropriately
*
* @param $prop
* @param $block
* @param Block $block
* @param $out
*
* @throws \LesserPhp\Exception\GeneralException
*/
protected function compileProp($prop, $block, $out)
protected function compileProp($prop, Block $block, $out)
{
// set error position context
$this->sourceLoc = isset($prop[-1]) ? $prop[-1] : -1;
Expand Down Expand Up @@ -1675,11 +1675,11 @@ protected function makeOutputBlock($type, $selectors = null)

/**
* @param \LesserPhp\NodeEnv $parent
* @param null $block
* @param Block|null $block
*
* @return \LesserPhp\NodeEnv
*/
protected function pushEnv($parent, $block = null)
protected function pushEnv($parent, Block $block = null)
{
$e = new NodeEnv();
$e->setParent($parent);
Expand Down
Loading

0 comments on commit 5d7e298

Please sign in to comment.