From b720f1d617312cf035d390af75818422f83e9dda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20Perr=C3=A9al?= <guillaume.perreal@irstea.fr> Date: Fri, 7 Sep 2018 13:14:12 +0200 Subject: [PATCH] =?UTF-8?q?Ajoute=20d'une=20classe=20de=20m=C3=A9tadonn?= =?UTF-8?q?=C3=A9es=20concernant=20les=20hi=C3=A9rarchies=20de=20classes?= =?UTF-8?q?=20PHP.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Metadata/ClassHierarchy.php | 42 ++++++++++++++ src/Metadata/DefaultClassHierarchy.php | 80 ++++++++++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 src/Metadata/ClassHierarchy.php create mode 100644 src/Metadata/DefaultClassHierarchy.php diff --git a/src/Metadata/ClassHierarchy.php b/src/Metadata/ClassHierarchy.php new file mode 100644 index 0000000..743cb65 --- /dev/null +++ b/src/Metadata/ClassHierarchy.php @@ -0,0 +1,42 @@ +<?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; +} diff --git a/src/Metadata/DefaultClassHierarchy.php b/src/Metadata/DefaultClassHierarchy.php new file mode 100644 index 0000000..c15e883 --- /dev/null +++ b/src/Metadata/DefaultClassHierarchy.php @@ -0,0 +1,80 @@ +<?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()] ?? []; + } +} -- GitLab