Skip to content

Commit

Permalink
Merge pull request #5 from viosys/feature/fix-classmap
Browse files Browse the repository at this point in the history
fix classmap to use multiple sdk Instances
  • Loading branch information
FalkoHilbert authored Jan 30, 2024
2 parents 13be3a5 + 99d0cb2 commit 250f4d6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
44 changes: 27 additions & 17 deletions src/ClassMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,57 @@


use BusinessCentral\Schema\EntityType;
use JsonException;

class ClassMap
{
protected static $map;
protected ?array $map = null;

public static function map(EntityType $type)
public function __construct(
protected readonly string $mapDir
)
{ }

/** @throws JsonException */
public function map(EntityType $type)
{
self::init();
$this->init();

return static::$map[$type->schema_type] ?? Entity::class;
return $this->map[$type->schema_type] ?? Entity::class;
}

public static function extend(string $name, string $class)
/** @throws JsonException */
public function extend(string $name, string $class): void
{
self::init();
$this->init();

if (!in_array(Entity::class, class_parents($class))) {
if (!in_array(Entity::class, class_parents($class), true)) {
throw new \RuntimeException(sprintf('Cannot use class %s as mapping for Business Central - Class must extend %s', $class, Entity::class));
}
static::$map[$name] = $class;
$this->map[$name] = $class;
}

public static function extendMultiple(array $map)
/** @throws JsonException */
public function extendMultiple(array $map): void
{
static::init();
$this->init();

static::$map = array_merge(static::$map, $map);
$this->map = array_merge($this->map, $map);
}

protected static function init()
/** @throws JsonException */
protected function init(): array
{
if (isset(static::$map)) {
return static::$map;
if ( $this->map !== null ) {
return $this->map;
}

$map_path = __DIR__ . '/../class_map.json';
$map_path = $this->mapDir . '/class_map.json';

if (file_exists($map_path)) {
static::$map = json_decode(file_get_contents($map_path), true);
$this->map = json_decode(file_get_contents($map_path), true, 512, JSON_THROW_ON_ERROR);
}

return static::$map;
return $this->map;
}
}
13 changes: 9 additions & 4 deletions src/Constructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public static function buildModels(
string $stubDir = __DIR__ . '/../stubs'
): void
{
static::$map = [];
static::$docs = [];
static::$sdk = SDK::instance(
$baseUri,
$tenant,
Expand Down Expand Up @@ -168,7 +170,7 @@ protected static function buildDocs(string $namespace, string $targetDir): void
$map_class = static::$map[$entity_type->schema_type] ?? Entity::class;

if ($map_class !== Entity::class) {
static::generateDocs($map_class, $entity_type);
static::generateDocs($targetDir, $map_class, $entity_type);
}
}

Expand All @@ -177,6 +179,8 @@ protected static function buildDocs(string $namespace, string $targetDir): void

protected static function buildMarkdown(string $targetDir): void
{
$classMap = new ClassMap($targetDir);

$doc_contents = '';
foreach (static::$docs as $class => $doc) {
$doc_contents .= sprintf("# %s\n", class_basename($class));
Expand Down Expand Up @@ -231,7 +235,7 @@ protected static function buildMarkdown(string $targetDir): void
$doc_contents .= "| --- | --- | :-: |\n";
/** @var NavigationProperty $item */
foreach ($doc['relations'] ?? [] as $item) {
$class = class_basename(ClassMap::map($item->getEntityType()));
$class = class_basename($classMap->map($item->getEntityType()));
$doc_contents .= sprintf(
"| %s | [%s](#%s) | %s |\n",
$item->name,
Expand Down Expand Up @@ -315,7 +319,7 @@ protected static function loadFiles($dir): array
/**
* @throws \ReflectionException
*/
protected static function generateDocs(string $class, EntityType $type): void
protected static function generateDocs(, string $targetDir, string $class, EntityType $type): void
{
$rfc = new \ReflectionClass($class);
$rfp = $rfc->getProperty('guarded');
Expand All @@ -337,8 +341,9 @@ protected static function generateDocs(string $class, EntityType $type): void
$properties[] = $doc_prop;
}

$classMap = new ClassMap($targetDir);
foreach ($type->relations() as $property) {
$base_type = ClassMap::map($property->getEntityType());
$base_type = $classMap->map($property->getEntityType());
$is_collection = $property->isCollection();

$doc_prop = [
Expand Down
2 changes: 1 addition & 1 deletion src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct(?array $attributes, Builder $query, EntityType $type
/** @return Entity */
public static function make(array $attributes, Builder $query, EntityType $type)
{
$class = ClassMap::map($type);
$class = $query->getSdk()->getClassMap()->map($type);

if (!class_exists($class)) {
$class = Entity::class;
Expand Down
12 changes: 12 additions & 0 deletions src/SDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public static function instance(string $APIUri, string $tenant, array $options =
protected string $base_uri = "https://api.businesscentral.dynamics.com";
protected string $APIUri = "https://api.businesscentral.dynamics.com/v2.0/{tenant}/{environment}/ODataV4/";

protected ClassMap $classMap;

protected array $options = [
// Credentials
'environment' => 'production',
Expand Down Expand Up @@ -87,6 +89,8 @@ protected function __construct($APIUri, $tenant, $options)
$this->getNewToken();

$this->mapEntities();


}

/**
Expand Down Expand Up @@ -205,6 +209,8 @@ public function mapEntities(): void
$json = json_decode(json_encode(simplexml_load_string($raw), JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR);

$this->setSchema(new Schema($json));

$this->classMap = new ClassMap($this->option('target_dir'));
}

/**
Expand Down Expand Up @@ -237,4 +243,10 @@ public function option(string $option, $default = null)
{
return $this->options[$option] ?? $default;
}

public function getClassMap(): ClassMap
{
return $this->classMap;
}

}

0 comments on commit 250f4d6

Please sign in to comment.