Skip to content
This repository has been archived by the owner on Jan 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #13 from supergrecko/new-codegen
Browse files Browse the repository at this point in the history
Replaces old DTO Template engine
  • Loading branch information
supergrecko committed Jun 22, 2019
2 parents 274c337 + fc19918 commit 531cc45
Show file tree
Hide file tree
Showing 137 changed files with 373 additions and 165 deletions.
212 changes: 212 additions & 0 deletions src/RiotQuest/Codegen/Codegen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
<?php

namespace RiotQuest\Codegen;

use Closure;
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
use Illuminate\Support\Collection;
use ReflectionException;
use RiotQuest\Components\Framework\Collections\Collection as LeagueCollection;
use ReflectionClass;
use ReflectionMethod;

/**
* Class Codegen
*
* @package RiotQuest\Codegen
*/
class Codegen
{

/**
* @var Codegen
*/
private static $instance;

/**
* @return Codegen
*/
public static function getInstance(): Codegen {
static::$instance = static::$instance ?? new Codegen();

return static::$instance;
}

/**
* @param Closure $closure
* @throws ReflectionException
*/
public static function createAll(Closure $closure): void {
$codegen = static::getInstance();

$in = new Filesystem(new Local(__DIR__ . '/../Components/Framework/Collections/'));

foreach ($in->listContents() as $file) {
if ($file['type'] === 'file') {
$res = $codegen->createFromClass("\\RiotQuest\\Components\\Framework\\Collections\\" . $file['filename']);

$closure->call($res, $res);
}
}
}

/**
* @param string $namespace
* @return Result
* @throws ReflectionException
*/
public function createFromClass(string $namespace): Result {
$template = new Collection();
$reflector = new ReflectionClass($namespace);

$doc = $reflector->getDocComment();
$tags = $this->getDocTags($doc);
$fns = new Collection($reflector->getMethods());

$template->put('props', $this->parsePropertyTags($tags))
->put('sees', $this->parseSeeTags($tags))
->put('list', $this->parseListTags($tags))
->put('methods', $this->parseFunctions($fns))
->put('name', $reflector->getShortName())
->put('class', $reflector->getName());

return new Result($template);
}

/**
* @param string $comment
* @return Collection
*/
private function getDocTags(string $comment): Collection {
$comment = explode("\r\n", $comment);

$lines = new Collection();
foreach ($comment as $line) {
$line = trim($line);
$line = substr($line, 2);

if (substr($line, 0, 1) === '@') {
$lines->add(explode(' ', $line));
}
}

return new Collection($lines);
}

/**
* @param Collection $tags
* @return Collection
*/
private function parsePropertyTags(Collection $tags): Collection {
$res = new Collection();

$tags->each(function ($el) use ($res) {
if ($el[0] === '@property') {
$res->put($el[2], $el[1]);
}
});

return $res;
}

/**
* @param Collection $tags
* @return Collection
*/
private function parseSeeTags(Collection $tags): Collection {
$res = new Collection();

$tags->each(function ($el) use ($res) {
if ($el[0] === '@see') {
$res->push($el[1]);
}
});

return $res;
}

/**
* @param Collection $tags
* @return Collection
*/
private function parseListTags(Collection $tags): Collection {
$res = new Collection();

$tags->each(function ($el) use ($res) {
if ($el[0] === '@list') {
$res->push($el[1]);
}
});

return $res;
}

/**
* @param Collection $tags
* @return Collection
*/
private function parseParamTags(Collection $tags): Collection {
$res = new Collection();

$tags->each(function ($el) use ($res) {
if ($el[0] === '@param') {
$res->put($el[2], $el[1]);
}
});

return $res;
}

/**
* @param Collection $tags
* @return Collection
*/
private function parseReturnTags(Collection $tags): Collection {
$res = new Collection();

$tags->each(function ($el) use ($res) {
if ($el[0] === '@return') {
$res->push($el[1]);
}
});

return $res;
}

/**
* @param Collection $functions
* @return Collection
*/
private function parseFunctions(Collection $functions): Collection {
$res = new Collection();

$functions->each(function (ReflectionMethod $fn) use ($res) {
if (!in_array($fn->getDeclaringClass()->getName(), [Collection::class, LeagueCollection::class])) {
$signature = $this->getFunctionSignature($fn);

$res->put($signature->get('name'), $signature);
}
});

return $res;
}

/**
* @param ReflectionMethod $method
* @return Collection
*/
private function getFunctionSignature(ReflectionMethod $method): Collection {
$res = new Collection();

$doc = $method->getDocComment();
$tags = $this->getDocTags($doc);

$res->put('name', $method->getShortName())
->put('returns', $this->parseReturnTags($tags))
->put('params', $this->parseParamTags($tags));

return $res;
}

}
59 changes: 59 additions & 0 deletions src/RiotQuest/Codegen/Result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace RiotQuest\Codegen;

use Illuminate\Support\Collection;

/**
* Class Result
*
* @package RiotQuest\Codegen
*/
class Result {

/**
* @var Collection
*/
private $result;

/**
* Result constructor.
* @param Collection $collection
*/
public function __construct(Collection $collection)
{
$this->result = $collection;
}

/**
* @return array
*/
public function toArray(): array {
return $this->result->toArray();
}

/**
* @return Collection
*/
public function get(): Collection {
return $this->result;
}

/**
* @param string $path
* @return Result
*/
public function pipe(string $path): self {
file_put_contents($path, $this->toString());

return $this;
}

/**
* @return string
*/
public function toString(): string {
return $this->result->toJson();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @package RiotQuest\Components\Framework\Collections
*/
class BannedChampion extends Collection
class BannedChampion extends Collection
{

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ChampionMasteryList extends Collection
/**
* Get the mastery object where the champion ID is equal to given ID
*
* @param $id
* @param int $id
* @return ChampionMastery
*/
public function findChampion(int $id)
Expand All @@ -30,7 +30,7 @@ public function findChampion(int $id)
/**
* Get every mastery record where level is equal to given level
*
* @param $level
* @param int $level
* @return Collection
*/
public function getWhereLevelEquals(int $level)
Expand All @@ -43,7 +43,7 @@ public function getWhereLevelEquals(int $level)
/**
* Get every mastery record where level is more than given level
*
* @param $level
* @param int $level
* @return Collection
*/
public function getWhereLevelMoreThan(int $level)
Expand All @@ -56,7 +56,7 @@ public function getWhereLevelMoreThan(int $level)
/**
* Get every mastery record where level is less than given level
*
* @param $level
* @param int $level
* @return Collection
*/
public function getWhereLevelLessThan(int $level)
Expand All @@ -69,7 +69,7 @@ public function getWhereLevelLessThan(int $level)
/**
* Get every mastery record where points is more than given points
*
* @param $points
* @param int $points
* @return Collection
*/
public function getWherePointsMoreThan(int $points)
Expand All @@ -82,7 +82,7 @@ public function getWherePointsMoreThan(int $points)
/**
* Get every mastery record where points is less than given points
*
* @param $points
* @param int $points
* @return Collection
*/
public function getWherePointsLessThan(int $points)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public function getSummoners()
/**
* Search for summoner name
*
* @param $name
* @param string $name
* @return string
*/
public function getWhereName($name)
public function getWhereName(string $name)
{
return array_values($this->filterArr(function (CurrentGameParticipant $e) use ($name) {
return $e->summonerName == $name;
Expand Down
2 changes: 1 addition & 1 deletion src/RiotQuest/Components/Framework/Collections/League.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class League extends Collection
/**
* Get player in league where summoner ID is equal to given ID
*
* @param $id
* @param string $id
* @return LeagueItem
*/
public function getPlayerWhereId(string $id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MatchHistory extends Collection
/**
* Checks if given match is in this list
*
* @param $id
* @param int $id
* @return MatchReference|false
*/
public function getMatch(int $id)
Expand All @@ -34,7 +34,7 @@ public function getMatch(int $id)
/**
* Pulls all matches where Queue is given id
*
* @param $id
* @param int $id
* @return MatchHistory
*/
public function getWhereQueue(int $id)
Expand All @@ -53,7 +53,7 @@ public function getWhereQueue(int $id)
/**
* Pulls all matches where Champion is given id
*
* @param $id
* @param int $id
* @return MatchHistory
*/
public function getWhereChampion(int $id)
Expand All @@ -72,7 +72,7 @@ public function getWhereChampion(int $id)
/**
* Pulls all matches where Season is given id
*
* @param $id
* @param int $id
* @return MatchHistory
*/
public function getWhereSeason(int $id)
Expand Down
Loading

0 comments on commit 531cc45

Please sign in to comment.