Skip to content

Commit

Permalink
Merge pull request #97 from spatie/ft-reflection-support
Browse files Browse the repository at this point in the history
add reflection support
  • Loading branch information
Gummibeer authored Sep 25, 2019
2 parents 5315a5a + d0ad4a9 commit e7f5288
Show file tree
Hide file tree
Showing 1,233 changed files with 574,784 additions and 2,802 deletions.
3 changes: 2 additions & 1 deletion generator/PackageGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function generate(Definitions $definitions)

$filesystem->cloneStaticFiles();

$types->each(function (Type $type) use ($filesystem) {
$types->each(function (Type $type) use ($filesystem, $types) {
$type->setTypeCollection($types);
$filesystem->createType($type);
});

Expand Down
40 changes: 38 additions & 2 deletions generator/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class Type
/** @var string */
public $name;

/** @var array */
/** @var string[] */
public $parents = [];

/** @var string */
public $description;

/** @var array */
/** @var Property[] */
public $properties = [];

/** @var array */
Expand All @@ -22,6 +22,9 @@ class Type
/** @var string */
public $resource;

/** @var bool */
protected $parentsLoaded = false;

public function addProperty(Property $property)
{
$this->properties[$property->name] = $property;
Expand All @@ -35,4 +38,37 @@ public function addConstant(Constant $constant)

ksort($this->constants);
}

public function setTypeCollection(TypeCollection $typeCollection): void
{
if ($this->parentsLoaded) {
return;
}

$this->parentsLoaded = true;

if (empty($this->parents)) {
return;
}

$types = $typeCollection->toArray();

foreach ($this->parents as $parentType) {
if (! isset($types[$parentType])) {
continue;
}

/** @var Type $parent */
$parent = $types[$parentType];
$parent->setTypeCollection($typeCollection);

$this->parents = array_unique(array_merge($this->parents, $parent->parents));

sort($this->parents);

foreach ($parent->properties as $property) {
$this->addProperty($property);
}
}
}
}
12 changes: 12 additions & 0 deletions generator/Writer/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ class Filesystem
/** @var \League\Flysystem\Filesystem */
protected $flysystem;

/** @var \Spatie\SchemaOrg\Generator\Writer\Template */
protected $contractTemplate;

/** @var \Spatie\SchemaOrg\Generator\Writer\Template */
protected $typeTemplate;

/** @var \Spatie\SchemaOrg\Generator\Writer\Template */
protected $builderClassTemplate;

public function __construct(string $root)
{
$adapter = new Local($root);
$this->flysystem = new Flysystem($adapter);

$this->contractTemplate = new Template('Contract.php.twig');
$this->typeTemplate = new Template('Type.php.twig');
$this->builderClassTemplate = new Template('Schema.php.twig');
}
Expand Down Expand Up @@ -48,6 +55,11 @@ public function cloneStaticFiles()

public function createType(Type $type)
{
$this->flysystem->put(
"src/Contracts/{$type->name}Contract.php",
$this->contractTemplate->render(['type' => $type])
);

$this->flysystem->put(
"src/{$type->name}.php",
$this->typeTemplate->render(['type' => $type])
Expand Down
11 changes: 11 additions & 0 deletions generator/templates/twig/Contract.php.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace Spatie\SchemaOrg\Contracts;
interface {{ type.name }}Contract
{
{% for property in type.properties if not property.pending %}
public function {{ property.name }}(${{ property.name }});
{% endfor %}
}
11 changes: 5 additions & 6 deletions generator/templates/twig/Type.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@
namespace Spatie\SchemaOrg;
{% for parent in type.parents %}
use \Spatie\SchemaOrg\Contracts\{{ parent }}Contract;
{% endfor %}
/**
* {{ type.description | doc(0) }}
*
* @see {{ type.resource }}
{% if type.parents %}
*
{% for parent in type.parents %}
* @mixin \Spatie\SchemaOrg\{{ parent }}
{% endfor %}
{% for property in type.properties if property.pending %}
* @method static {{ property.name }}(${{ property.name }}) The value should be instance of pending types {{ property.ranges | join('|') }}
{% endfor %}
{% endif %}
*/
class {{ type.name }} extends BaseType
class {{ type.name }} extends BaseType{% if type.parents %} implements {% endif %}{{ type.parents|map(parent => "#{parent}Contract")|join(', ') }}
{
{% for constant in type.constants %}
/**
Expand Down
Loading

0 comments on commit e7f5288

Please sign in to comment.