Skip to content

Commit

Permalink
Generator rewrite (#44)
Browse files Browse the repository at this point in the history
* Wip

* Wip

* Wip

* Wip

* Wip

* Apply fixes from StyleCI

* Wip

* Remove async

* Wip

* Wip

* Wip

* Apply fixes from StyleCI
  • Loading branch information
sebastiandedeyne committed Feb 12, 2018
1 parent 4d90ddd commit 0c32ab8
Show file tree
Hide file tree
Showing 14 changed files with 278 additions and 168 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
@@ -1,6 +1,6 @@
# The MIT License (MIT)

Copyright (c) Spatie bvba <info@spatie.be>
Copyright (c) 2017 Spatie bvba <info@spatie.be>

> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -20,6 +20,7 @@
},
"require-dev": {
"graham-campbell/analyzer": "^1.1",
"larapack/dd": "^1.1",
"league/flysystem": "^1.0",
"phpunit/phpunit": "^6.0",
"symfony/console": "^3.2",
Expand Down
19 changes: 10 additions & 9 deletions generator/Console/GenerateCommand.php
Expand Up @@ -2,22 +2,21 @@

namespace Spatie\SchemaOrg\Generator\Console;

use Spatie\SchemaOrg\Generator\Definitions;
use Symfony\Component\Console\Command\Command;
use Spatie\SchemaOrg\Generator\PackageGenerator;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class GenerateCommand extends Command
{
const SOURCE = 'https://raw.githubusercontent.com/schemaorg/schemaorg/sdo-callisto/data/schema.rdfa';

protected function configure()
{
$this
->setName('generate')
->setDescription('Generate the package code from the schema.org docs')
->addArgument('source', InputArgument::OPTIONAL, 'Path to the RDFa source file', static::SOURCE);
->addOption('local', 'l', InputOption::VALUE_NONE, 'Use a cached version of the source');
}

/**
Expand All @@ -32,13 +31,15 @@ protected function execute(InputInterface $input, OutputInterface $output)

$generator = new PackageGenerator();

$generator->generate(
file_get_contents($input->getArgument('source'))
);
$definitions = new Definitions([
'core' => 'https://raw.githubusercontent.com/schemaorg/schemaorg/sdo-callisto/data/schema.rdfa',
]);

$output->writeln('Fresh package generated! Linting...');
if (! $input->getOption('local')) {
$definitions->preload();
}

exec('find src -exec php -l {} \; | grep "!(No syntax)"');
$generator->generate($definitions);

$output->writeln('Done!');

Expand Down
51 changes: 51 additions & 0 deletions generator/Definitions.php
@@ -0,0 +1,51 @@
<?php

namespace Spatie\SchemaOrg\Generator;

use RuntimeError;
use Symfony\Component\DomCrawler\Crawler;

class Definitions
{
/** @var array */
protected $sources;

/** @var string */
protected $tempDir = __DIR__.'/temp';

public function __construct(array $sources)
{
$this->sources = $sources;
}

public function preload()
{
foreach ($this->sources as $sourceId => $sourcePath) {
$this->loadSource($sourceId, false);
}
}

public function query(string $selector): Crawler
{
return (new Crawler($this->loadSource('core')))->filter($selector);
}

protected function loadSource(string $sourceId, bool $fromCache = true): string
{
if (! isset($this->sources[$sourceId])) {
throw new RuntimeError("Source `{$sourceId}` doesn't exist");
}

$cachePath = $this->tempDir.'/'.$sourceId.'.rdfa';

if ($fromCache && file_exists($cachePath)) {
return file_get_contents($cachePath);
}

$rdfa = file_get_contents($this->sources[$sourceId]);

file_put_contents($cachePath, $rdfa);

return $rdfa;
}
}
10 changes: 4 additions & 6 deletions generator/PackageGenerator.php
Expand Up @@ -2,16 +2,14 @@

namespace Spatie\SchemaOrg\Generator;

use Spatie\SchemaOrg\Generator\Parser\RdfaParser;
use Spatie\SchemaOrg\Generator\Writer\Filesystem;
use Spatie\SchemaOrg\Generator\Parser\DefinitionParser;

class PackageGenerator
{
public function generate(string $rdfa)
public function generate(Definitions $definitions)
{
$types = (new RdfaParser($rdfa))->parse();

$types->sort();
$types = (new DefinitionParser())->parse($definitions);

$filesystem = new Filesystem(__DIR__.'/..');

Expand All @@ -23,6 +21,6 @@ public function generate(string $rdfa)
$filesystem->createType($type);
});

$filesystem->createFactory($types);
$filesystem->createBuilderClass($types);
}
}
32 changes: 32 additions & 0 deletions generator/Parser/DefinitionParser.php
@@ -0,0 +1,32 @@
<?php

namespace Spatie\SchemaOrg\Generator\Parser;

use Spatie\SchemaOrg\Generator\Property;
use Symfony\Component\DomCrawler\Crawler;
use Spatie\SchemaOrg\Generator\Definitions;
use Spatie\SchemaOrg\Generator\TypeCollection;
use Spatie\SchemaOrg\Generator\Parser\Tasks\ParseType;
use Spatie\SchemaOrg\Generator\Parser\Tasks\ParseProperty;

class DefinitionParser
{
public function parse(Definitions $definitions): TypeCollection
{
$types = $definitions
->query('[typeof="rdfs:Class"]')
->each(function (Crawler $crawler) {
return call_user_func(ParseType::fromCrawler($crawler));
});

$properties = $definitions
->query('[typeof="rdf:Property"]')
->each(function (Crawler $crawler) {
return call_user_func(ParseProperty::fromCrawler($crawler));
});

return new TypeCollection(
array_filter($types), array_filter($properties)
);
}
}
138 changes: 0 additions & 138 deletions generator/Parser/RdfaParser.php

This file was deleted.

69 changes: 69 additions & 0 deletions generator/Parser/Tasks/ParseProperty.php
@@ -0,0 +1,69 @@
<?php

namespace Spatie\SchemaOrg\Generator\Parser\Tasks;

use Spatie\SchemaOrg\Generator\Property;
use Symfony\Component\DomCrawler\Crawler;

class ParseProperty extends Task
{
public function __invoke(): ?Property
{
$node = new Crawler($this->definition);

$property = new Property();

$property->name = $this->getText($node, '[property="rdfs:label"]');

if (empty($property->name)) {
return null;
}

$property->description = $this->getText($node, '[property="rdfs:comment"]');

$property->resource = $this->getAttribute($node, 'resource');

$node
->filter('[property="http://schema.org/domainIncludes"]')
->each(function (Crawler $domain) use ($property) {
$property->addType($this->getText($domain));
});

$node
->filter('[property="http://schema.org/rangeIncludes"]')
->each(function (Crawler $range) use ($property) {
$property->addRanges(
$this->castRangesToTypes($this->getText($range))
);
});

return $property;
}

private function castRangesToTypes(string $range): array
{
switch ($range) {
case 'Boolean':
return ['bool'];
case 'False':
return ['false'];
case 'True':
return ['true'];
case 'Date':
case 'Time':
case 'DateTime':
return ['\DateTimeInterface'];
case 'Text':
case 'URL':
return ['string'];
case 'Number':
return ['float', 'int'];
case 'Float':
return ['float'];
case 'Integer':
return ['int'];
default:
return [$range];
}
}
}

0 comments on commit 0c32ab8

Please sign in to comment.