Commit b720f1d6 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Ajoute d'une classe de métadonnées concernant les hiérarchies de classes PHP.

Showing with 122 additions and 0 deletions
+122 -0
<?php declare(strict_types=1);
/*
* irstea/ng-model-generator-bundle generates Typescript interfaces for Angular using api-platform metadata.
* Copyright (C) 2018 IRSTEA
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License and the GNU
* Lesser General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
namespace Irstea\NgModelGeneratorBundle\Metadata;
use Irstea\NgModelGeneratorBundle\Models\PHPClass;
/**
* Interface ClassHierarchy.
*/
interface ClassHierarchy
{
/**
* @param PHPClass $class
*
* @return PHPClass|null
*/
public function getParent(PHPClass $class): ?PHPClass;
/**
* @param PHPClass $class
*
* @return PHPClass[]
*/
public function getChildren(PHPClass $class): array;
}
<?php declare(strict_types=1);
/*
* irstea/ng-model-generator-bundle generates Typescript interfaces for Angular using api-platform metadata.
* Copyright (C) 2018 IRSTEA
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License and the GNU
* Lesser General Public License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
namespace Irstea\NgModelGeneratorBundle\Metadata;
use Irstea\NgModelGeneratorBundle\Models\PHPClass;
/**
* Class DefaultClassHierarchy.
*/
final class DefaultClassHierarchy implements ClassHierarchy
{
/** @var PHPClass[] */
private $parents = [];
/** @var PHPClass[][] */
private $children = [];
/**
* @param PHPClass[] $classes
*/
public function preload(PHPClass $class): void
{
$className = $class->getFullName();
if (\array_key_exists($className, $this->parents)) {
return;
}
$parent = $class->getReflection()->getParentClass();
if (!$parent) {
$this->parents[$className] = null;
return;
}
$parentClassName = $parent->getName();
$this->parents[$className] = PHPClass::get($parentClassName);
if (!isset($this->children[$parentClassName])) {
$this->children[$parentClassName] = [];
}
$this->children[$parentClassName][$className] = $class;
}
/**
* {@inheritdoc}
*/
public function getParent(PHPClass $class): ?PHPClass
{
$this->preload($class);
return $this->parents[$class->getFullName()] ?? null;
}
/**
* {@inheritdoc}
*/
public function getChildren(PHPClass $class): array
{
$this->preload($class);
return $this->children[$class->getFullName()] ?? [];
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment