2018-08-16 22:30:59 +00:00
|
|
|
<?php
|
2022-03-04 17:10:24 +00:00
|
|
|
|
2018-08-16 22:30:59 +00:00
|
|
|
/*
|
2024-01-13 20:03:36 +00:00
|
|
|
* Copyright (c) 2010-2024 Tim Düsterhus.
|
2018-08-16 22:30:59 +00:00
|
|
|
*
|
|
|
|
* Use of this software is governed by the Business Source License
|
|
|
|
* included in the LICENSE file.
|
|
|
|
*
|
2024-06-15 19:56:58 +00:00
|
|
|
* Change Date: 2028-06-15
|
2018-08-16 22:30:59 +00:00
|
|
|
*
|
|
|
|
* On the date above, in accordance with the Business Source
|
|
|
|
* License, use of this software will be governed by version 2
|
|
|
|
* or later of the General Public License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace chat\data\command;
|
|
|
|
|
2022-03-04 17:10:24 +00:00
|
|
|
use chat\system\cache\builder\CommandCacheBuilder;
|
|
|
|
use wcf\data\package\Package;
|
|
|
|
use wcf\system\SingletonFactory;
|
|
|
|
|
2018-08-16 22:30:59 +00:00
|
|
|
/**
|
|
|
|
* Manages the command cache.
|
|
|
|
*/
|
2022-03-04 18:35:31 +00:00
|
|
|
final class CommandCache extends SingletonFactory
|
2022-03-04 17:10:24 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* list of cached commands
|
|
|
|
* @var Command[]
|
|
|
|
*/
|
|
|
|
protected $commands = [ ];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* list of cached commands by package
|
|
|
|
* @var Command[][]
|
|
|
|
*/
|
|
|
|
protected $packages = [ ];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* list of cached triggers
|
|
|
|
* @var int[]
|
|
|
|
*/
|
|
|
|
protected $triggers = [ ];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
protected function init()
|
|
|
|
{
|
|
|
|
$data = CommandCacheBuilder::getInstance()->getData();
|
|
|
|
|
|
|
|
$this->commands = $data['commands'];
|
|
|
|
$this->packages = $data['packages'];
|
|
|
|
$this->triggers = $data['triggers'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a specific command.
|
|
|
|
*/
|
2022-03-04 17:47:19 +00:00
|
|
|
public function getCommand(int $commandID): ?Command
|
2022-03-04 17:10:24 +00:00
|
|
|
{
|
|
|
|
if (isset($this->commands[$commandID])) {
|
|
|
|
return $this->commands[$commandID];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a specific command defined by a trigger.
|
|
|
|
*/
|
2022-03-04 17:47:19 +00:00
|
|
|
public function getCommandByTrigger(string $trigger): ?Command
|
2022-03-04 17:10:24 +00:00
|
|
|
{
|
|
|
|
if (isset($this->triggers[$trigger])) {
|
|
|
|
return $this->commands[$this->triggers[$trigger]];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the command defined by the given package and identifier.
|
|
|
|
*/
|
2022-03-04 17:47:19 +00:00
|
|
|
public function getCommandByPackageAndIdentifier(Package $package, string $identifier): ?Command
|
2022-03-04 17:10:24 +00:00
|
|
|
{
|
|
|
|
if (isset($this->packages[$package->packageID][$identifier])) {
|
|
|
|
return $this->packages[$package->packageID][$identifier];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all commands.
|
|
|
|
*
|
|
|
|
* @return Command[]
|
|
|
|
*/
|
|
|
|
public function getCommands()
|
|
|
|
{
|
|
|
|
return $this->commands;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all triggers.
|
|
|
|
*
|
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
public function getTriggers()
|
|
|
|
{
|
|
|
|
return $this->triggers;
|
|
|
|
}
|
2018-08-16 22:30:59 +00:00
|
|
|
}
|