metagen/service.inc.php

218 lines
4.4 KiB
PHP

<?php
class mtgMetaService extends mtgMetaUnit implements mtgScope
{
private string $name;
//to better handle partial services, since we need to lookup in multiple scopes
private mtgMultiScope $merged_scope;
private $rpcs = array();
private $events = array();
private $user_types = array();
private $partials = array();
function __construct(string $name, mtgScope $scope, array $tokens = array())
{
$this->name = $name;
$this->merged_scope = new mtgMultiScope();
$this->merged_scope->addScope($scope);
$this->scope = $scope;
$this->tokens = $tokens;
}
function validate(mtgMetaInfo $meta) {}
function addPartial(mtgMetaParsedModule $module, mtgMetaService $service)
{
$this->setTokens(array_merge($this->getTokens(), $service->getTokens()));
$this->merged_scope->addScope($service->getScope());
$this->partials[$module->file] = $service;
}
function getPartials() : array
{
return $this->partials;
}
function getName() : string
{
return $this->name;
}
function findSymbol(string $name) : ?mtgMetaUnit
{
if(isset($this->user_types[$name]))
return $this->user_types[$name];
return $this->merged_scope->findSymbol($name);
}
function getRPCs() : array
{
return $this->rpcs;
}
function addRPC(mtgMetaRPC $rpc)
{
$rpc->setScope($this);
foreach($this->rpcs as $name => $_rpc)
{
if($name == $rpc->getName())
throw new Exception("Service '{$this->name}' already has RPC '{$rpc->getName()}'");
if($rpc->getNumericCode() == $_rpc->getNumericCode())
throw new Exception("Service '{$this->name}' already has RPC '{$_rpc->getName()}' with code '{$rpc->getNumericCode()}'");
}
$this->rpcs[$rpc->getName()] = $rpc;
}
function hasRPC(string $name) : bool
{
return isset($this->rpcs[$name]);
}
function getRPC(string $name) : mtgMetaRPC
{
if(!isset($this->rpcs[$name]))
throw new Exception("No such RPC '$name'");
return $this->rpcs[$name];
}
function getUserTypes() : array
{
return $this->user_types;
}
function addUserType(mtgUserType $utype)
{
$utype->setScope($this);
if($this->hasRPC($utype->getName()) || $this->hasUserType($utype->getName()))
throw new Exception("Service '{$this->name}' already has user type with name '{$utype->getName()}'");
$this->user_types[$utype->getName()] = $utype;
}
function hasUserType(string $name) : bool
{
return isset($this->user_types[$name]);
}
function getUserType(bool $name) : mtgUserType
{
if(!isset($this->user_types[$name]))
throw new Exception("No such user type '$name'");
return $this->user_types[$name];
}
function addEvent(mtgMetaStruct $evt)
{
$evt->setScope($this);
$this->events[] = $evt;
$this->addUserType($evt);
}
function getEvents() : array
{
return $this->events;
}
function getClassId() : int
{
if($this->hasToken('class_id'))
return $this->getToken('class_id');
return crc32($this->name);
}
function __toString()
{
return $this->name;
}
}
class mtgMetaRPC extends mtgMetaUnit
{
private string $name;
private int $code;
private mtgMetaPacket $in;
private mtgMetaPacket $out;
function __construct(string $name, int $code, mtgMetaPacket $in, mtgMetaPacket $out, array $tokens = array())
{
$this->name = $name;
$this->code = $code;
$this->in = $in;
$this->out = $out;
$this->tokens = $tokens;
}
function validate(mtgMetaInfo $meta) {}
function getName() : string
{
return $this->name;
}
function getCode() : int
{
return $this->code;
}
// deprecated?
function getNumericCode() : int
{
return (int)$this->code;
}
function getReq() : mtgMetaPacket
{
return $this->in;
}
function getRsp() : mtgMetaPacket
{
return $this->out;
}
}
class mtgMetaPacket extends mtgMetaStruct
{
private int $code;
function __construct(int $code, string $name, array $tokens = array())
{
$this->code = $code;
parent::__construct($name, array(), null, $tokens);
}
// deprecated
function getPrefix() : string
{
list($prefix,) = explode('_', $this->getName());
return $prefix;
}
// deprecated
function getFullName() : string
{
return $this->code . "_" . $this->getName();
}
function getCode() : int
{
return $this->code;
}
// deprecated
function getNumericCode() : int
{
return (int)$this->code;
}
}