Skip to content

Commit

Permalink
Merge pull request #43 from restcord/develop
Browse files Browse the repository at this point in the history
Merging in develop
  • Loading branch information
cryptiklemur committed Feb 15, 2018
2 parents 6da3ec7 + 4e75419 commit 932f764
Show file tree
Hide file tree
Showing 99 changed files with 3,533 additions and 1,407 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ composer.lock
docs/Gemfile
docs/Gemfile.lock
docs/_site/
cache
4 changes: 4 additions & 0 deletions .travis.yml
Expand Up @@ -2,13 +2,17 @@ language: php

sudo: false

services:
- redis-server

matrix:
include:
- php: 5.6
- php: 7.0
- php: 7.1

install:
- yes '' | pecl install redis
- composer install

script:
Expand Down
23 changes: 14 additions & 9 deletions bin/buildDocs
Expand Up @@ -33,6 +33,7 @@ function recursiveRemoveDirectory($directory)
rmdir($directory);
}

/** @noinspection PhpUnhandledExceptionInspection */
(new Application('Build Docs', '1.0.0'))
->register('buildDocs')
->addArgument('version', InputArgument::REQUIRED, 'Version to build')
Expand Down Expand Up @@ -61,15 +62,19 @@ function recursiveRemoveDirectory($directory)
mkdir($filePath, 02775, true);
}

$markdown = $twig->render(
'operation.md.twig',
[
'category' => $category,
'operation' => $operation,
'config' => $config,
'order' => $i
]
);
try {
$markdown = $twig->render(
'operation.md.twig',
[
'category' => $category,
'operation' => $operation,
'config' => $config,
'order' => $i
]
);
} catch (\Exception $e) {
throw $e;
}

file_put_contents($filePath.str_replace('/', ' or ', $config['name']).'.md', $markdown);
$i++;
Expand Down
75 changes: 42 additions & 33 deletions bin/buildDummyClasses
Expand Up @@ -10,6 +10,26 @@
* with this source code in the file LICENSE
*/

function recursiveRemoveDirectory($directory)
{
foreach (glob("{$directory}/*") as $file) {
if (is_dir($file)) {
recursiveRemoveDirectory($file);
} else {
unlink($file);
}
}
rmdir($directory);
}

$path = __DIR__.'/../src/Interfaces';
try {
recursiveRemoveDirectory($path);
} catch (\Exception $e) {
}
mkdir($path, 02775, true);
$path = realpath($path);

require __DIR__.'/../vendor/autoload.php';

use gossi\codegen\generator\CodeGenerator;
Expand Down Expand Up @@ -43,33 +63,14 @@ $license = <<<EOF
\n
EOF;

function recursiveRemoveDirectory($directory)
{
foreach (glob("{$directory}/*") as $file) {
if (is_dir($file)) {
recursiveRemoveDirectory($file);
} else {
unlink($file);
}
}
rmdir($directory);
}

/** @noinspection PhpUnhandledExceptionInspection */
(new Application('Build Dummy Classes', '1.0.0'))
->register('buildDummyClasses')
->addArgument('version', InputArgument::REQUIRED, 'Version to build')
->setCode(
function (InputInterface $input, OutputInterface $output) use ($twig, $license) {
function (InputInterface $input, OutputInterface $output) use ($twig, $license, $path) {
$style = new \Symfony\Component\Console\Style\SymfonyStyle($input, $output);
$style->title("Building Dummy Classes for: ".$input->getArgument('version'));

$path = __DIR__.'/../src/Interfaces';
try {
recursiveRemoveDirectory($path);
} catch (\Exception $e) {
}
mkdir($path, 02775, true);
$path = realpath($path);
$style->title("Building Dummy Classes for Gateway v".$input->getArgument('version'));

$definition = \GuzzleHttp\json_decode(
file_get_contents(
Expand All @@ -79,14 +80,15 @@ function recursiveRemoveDirectory($directory)
);

$generator = new CodeGenerator();
foreach ($definition['operations'] as $category => $operations) {
foreach ($definition['operations'] as $resource => $operations) {
$resource = str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $resource)));

$class = new PhpInterface();
$class->setQualifiedName('RestCord\\Interfaces\\'.ucwords($category));
$class->setDescription(ucwords($category)." Intellisense Helper");
$class->setQualifiedName('RestCord\\Interfaces\\'.ucwords($resource));
$class->setDescription(ucwords($resource)." Intellisense Helper");
$class->setMethods(
array_map(
function ($name, $operation) use ($class, $category) {
function ($name, $operation) use ($class, $resource) {
$options = new PhpParameter('options');
$options->setType(
'array',
Expand All @@ -104,19 +106,24 @@ function recursiveRemoveDirectory($directory)

$returnType = 'array';
if (isset($operation['responseTypes']) && sizeof($operation['responseTypes']) >= 1) {
$firstType = $operation['responseTypes'][0]['type'];
$array = stripos($firstType, 'Array<') !== false;
$firstType = $operation['responseTypes'][0]['type'];
$array = stripos($firstType, 'Array<') !== false;
if ($array) {
$firstType = substr($firstType, 6, -1);
}

$temp = explode("/", $firstType);
$returnType = sprintf(
"\\RestCord\\Model\\%s\\%s",
ucwords($category),
str_replace(
' ',
'',
ucwords(str_replace('-', ' ', explode('/', $firstType)[1]))
ucwords(str_replace('-', ' ', $temp[0]))
),
str_replace(
' ',
'',
ucwords(str_replace('-', ' ', $temp[1]))
)
);

Expand All @@ -129,7 +136,9 @@ function recursiveRemoveDirectory($directory)
$returnType .= $array ? '[]' : '';
}

$method = new PhpMethod($name);
$method = new PhpMethod(
lcfirst(str_replace(' ', '', ucwords(str_replace('-', ' ', $name))))
);
$method->setType($returnType, $operation['responseNote'] ?? '');
if (isset($operation['link'])) {
$method->setLongDescription('@see '.$operation['link']);
Expand All @@ -144,7 +153,7 @@ function recursiveRemoveDirectory($directory)
)
);

file_put_contents($path.'/'.ucwords($category).'.php', $license.$generator->generate($class));
file_put_contents($path.'/'.ucwords($resource).'.php', $license.$generator->generate($class));
}

$style->success('Finished. Classes built in: '.realpath($path));
Expand Down Expand Up @@ -175,7 +184,7 @@ function mapBadDocs(string $name, array $operation, string $cls): string
return $cls;
}

trigger_error($name.' operation in '.$operation['category'].' has a bad responseType: ' . $cls);
trigger_error($name.' operation in '.$operation['resource'].' has a bad responseType: '.$cls);

return $newCls;
}
101 changes: 65 additions & 36 deletions bin/buildModelClasses
Expand Up @@ -10,13 +10,31 @@
* with this source code in the file LICENSE
*/

function recursiveRemoveDirectory($directory)
{
foreach (glob("{$directory}/*") as $file) {
if (is_dir($file)) {
recursiveRemoveDirectory($file);
} else {
unlink($file);
}
}
rmdir($directory);
}

$path = __DIR__.'/../src/Model';
try {
recursiveRemoveDirectory($path);
} catch (\Exception $e) {
}
mkdir($path, 02775, true);
$path = realpath($path);

require __DIR__.'/../vendor/autoload.php';

use gossi\codegen\generator\CodeGenerator;
use gossi\codegen\model\PhpClass;
use gossi\codegen\model\PhpInterface;
use gossi\codegen\model\PhpMethod;
use gossi\codegen\model\PhpParameter;
use gossi\codegen\model\PhpProperty;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -43,34 +61,15 @@ $license = <<<EOF
\n
EOF;

function recursiveRemoveDirectory($directory)
{
foreach (glob("{$directory}/*") as $file) {
if (is_dir($file)) {
recursiveRemoveDirectory($file);
} else {
unlink($file);
}
}
rmdir($directory);
}

/** @noinspection PhpUnhandledExceptionInspection */
(new Application('Build Model Classes', '1.0.0'))
->register('buildModelClasses')
->addArgument('version', InputArgument::REQUIRED, 'Version to build')
->setCode(
function (InputInterface $input, OutputInterface $output) use ($twig, $license) {
function (InputInterface $input, OutputInterface $output) use ($twig, $license, $path) {
$style = new \Symfony\Component\Console\Style\SymfonyStyle($input, $output);
$style->title("Building Model Classes for: ".$input->getArgument('version'));

$path = __DIR__.'/../src/Model';
try {
recursiveRemoveDirectory($path);
} catch (\Exception $e) {
}
mkdir($path, 02775, true);
$path = realpath($path);

$definition = \GuzzleHttp\json_decode(
file_get_contents(
__DIR__.'/../src/Resources/service_description-v'.$input->getArgument('version').'.json'
Expand All @@ -79,16 +78,21 @@ function recursiveRemoveDirectory($directory)
);

$generator = new CodeGenerator();
foreach ($definition['models'] as $category => $models) {
foreach ($definition['models'] as $resource => $models) {
foreach ($models as $name => $model) {
$class = new PhpClass();
$class->setQualifiedName('RestCord\\Model\\'.ucwords($category).'\\'.ucwords($name));
$resource = str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $resource)));
$name = str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', $name)));
$class = new PhpClass();
$class->setQualifiedName('RestCord\\Model\\'.ucwords($resource).'\\'.ucwords($name));
$class->setDescription(ucwords($name)." Model");
$class->setProperties(
array_map(
function ($name, $property) use ($class, $category, $model) {
$prop = new PhpProperty($name);
$prop->setType(normalizeType($property['type']));
function (&$name, $property) use ($class, $resource, $model) {
$optional = strpos($name, '?') !== false;
$name = lcfirst(str_replace([' ', '?'], '', str_replace('-', '_', $name)));
$prop = new PhpProperty($name);

$prop->setType(normalizeType($name, $resource, $property).($optional ? "|null" : ""));
$prop->setDescription($property['description']);

if (isset($property['default'])) {
Expand All @@ -102,6 +106,8 @@ function recursiveRemoveDirectory($directory)
)
);

addTraits($class, $resource, $name);

$constructor = new PhpMethod('__construct');
$constructor->addSimpleParameter('content', 'array', null);
$constructor->setBody(
Expand All @@ -121,9 +127,9 @@ EOF
$constructor->setVisibility('public');
$class->setMethod($constructor);

@mkdir($path.'/'.ucwords($category), 02775, true);
@mkdir($path.'/'.ucwords($resource), 02775, true);
file_put_contents(
$path.'/'.ucwords($category).'/'.ucwords($name).'.php',
$path.'/'.ucwords($resource).'/'.ucwords($name).'.php',
$license.$generator->generate($class)
);
}
Expand All @@ -136,13 +142,18 @@ EOF
->setDefaultCommand('buildModelClasses', true)
->run();

function normalizeType(string $type): string
function normalizeType(string $name, string $resource, array $property): string
{
$type = $property['type'];
if ($name === 'user' && $resource === 'Guild') {
$type = "user/user";
}

switch ($type) {
default:
if (strpos($type, '/') !== false || strpos($type, 'Array<') === 0) {
$array = false;
if (strpos($type, 'Array') === 0) {
if (stripos($type, 'Array') !== false) {
$array = true;
$type = str_replace(['Array<', '>'], '', $type);
}
Expand All @@ -151,9 +162,9 @@ function normalizeType(string $type): string
return 'int[]';
}

$cls = "\\RestCord\\Model\\";
$tmp = explode('/', $type);
$cls .= ucwords($tmp[0])."\\";
$cls = "\\RestCord\\Model\\";
$tmp = explode('/', $type);
$cls .= ucwords($tmp[0])."\\";
$clean = str_replace(['-object', '-'], ['', ' '], $tmp[1]);
$clean = ucwords($clean);
$clean = str_replace(' ', '', $clean);
Expand All @@ -165,6 +176,12 @@ function normalizeType(string $type): string
}

return $type;
case 'array':
if (strpos($property['description'], "ids") !== false) {
return "int[]";
}

return "array";
case 'datetime':
return '\DateTime';
case 'snowflake':
Expand Down Expand Up @@ -208,3 +225,15 @@ function mapBadDocs(string $cls): string

return $cls;
}

function addTraits(PhpClass $class, string $resource, string $name)
{
if ($resource === 'User' && $name === 'User') {
$class->addUseStatement(\RestCord\Traits\AvatarTrait::class)->addTrait("AvatarTrait");
}

if ($resource === 'Guild' && $name === 'Guild') {
$class->addUseStatement(\RestCord\Traits\IconTrait::class)->addTrait("IconTrait");
$class->addUseStatement(\RestCord\Traits\SplashTrait::class)->addTrait("SplashTrait");
}
}
File renamed without changes.

0 comments on commit 932f764

Please sign in to comment.