getProjectId(); * ``` * * ``` * // It is easy to get any metadata from a project. * $val = $metadata->getProjectMetadata($key); * ``` */ class Metadata { /** * @var ReaderInterface The metadata reader. */ private $reader; /** * @var string The project id. */ private $projectId; /** * @var int The numeric project id. */ private $numericProjectId; /** * @param ReaderInterface $reader [optional] A metadata reader implementation. */ public function __construct(ReaderInterface $reader = null) { $this->reader = $reader ?: new HttpHandlerReader(); } /** * Replace the default reader implementation * * @deprecated If a custom reader implementation is desired, provide it at * construction. * @param ReaderInterface $reader The reader implementation */ public function setReader(ReaderInterface $reader) { $this->reader = $reader; } /** * Fetch a metadata item by its path * * Example: * ``` * $projectId = $metadata->get('project/project-id'); * ``` * * @param string $path The path of the item to retrieve. */ public function get($path) { return $this->reader->read($path); } /** * Detect and return the project ID * * Example: * ``` * $projectId = $metadata->getProjectId(); * ``` * * @return string */ public function getProjectId() { if (!isset($this->projectId)) { $this->projectId = $this->get('project/project-id'); } return $this->projectId; } /** * Detect and return the numeric project ID * * Example: * ``` * $projectId = $metadata->getNumericProjectId(); * ``` * * @return string */ public function getNumericProjectId() { if (!isset($this->numericProjectId)) { $this->numericProjectId = $this->get('project/numeric-project-id'); } return $this->numericProjectId; } /** * Fetch an item from the project metadata * * Example: * ``` * $foo = $metadata->getProjectMetadata('foo'); * ``` * * @param string $key The metadata key * @return string */ public function getProjectMetadata($key) { $path = 'project/attributes/'.$key; return $this->get($path); } /** * Fetch an item from the instance metadata * * Example: * ``` * $foo = $metadata->getInstanceMetadata('foo'); * ``` * * @param string $key The instance metadata key * @return string */ public function getInstanceMetadata($key) { $path = 'instance/attributes/'.$key; return $this->get($path); } }