Skip to content

Commit

Permalink
add sort optoins, fix boo boo
Browse files Browse the repository at this point in the history
  • Loading branch information
iwyg committed Jan 9, 2016
1 parent 686fc90 commit 344a513
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"name": "lucid/writer",
"license": "MIT",
"description": "object definition generators",
"require": {
"php": ">=5.6",
"lucid/common": "dev-master",
"satooshi/php-coveralls": "dev-master"
"lucid/common": "dev-master"
},
"require-dev": {
"phpunit/phpunit": "5.2.*@dev"
Expand Down
21 changes: 20 additions & 1 deletion src/Object/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ abstract class AbstractWriter implements GeneratorInterface
/** @var bool */
private $noAutoGenerateTag;

/** @var callable */
private $usesort;

/** @var array */
private static $types = [
T_CLASS => 'class',
Expand Down Expand Up @@ -105,6 +108,18 @@ public function __construct($name, $namespace = null, $type = null, $docType = s
$this->setDocType($docType);
}

/**
* Sets the sort function for use statements.
*
* @param callable $sort
*
* @return void
*/
public function setUseSort(callable $sort)
{
$this->usesort = $sort;
}

/**
* setDocType
*
Expand Down Expand Up @@ -388,7 +403,11 @@ protected function writeUseStatements(WriterInterface $writer, ImportResolver $r
$uses[] = $resolver->getImport($use);
}

natsort($uses);
if (is_callable($this->usesort)) {
usort($uses, $this->usesort);
} else {
natsort($uses);
}

foreach ($uses as $u) {
$writer->writeln(sprintf('use %s;', $u));
Expand Down
4 changes: 3 additions & 1 deletion src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ public function writeln($str = null)
public function appendln($str)
{
if ($buff = array_pop($this->lnbuff)) {
$this->addStr($buff.$str, count($this->lnbuff));
$lines = explode("\n", $str);
$this->lnbuff[] = $buff.array_shift($lines);
array_map([$this, 'writeln'], $lines);
}

return $this;
Expand Down

0 comments on commit 344a513

Please sign in to comment.