CloudObjects / Directory / phpMAE / StackTemplateVariableGenerator
Sign in

StackTemplateVariableGenerator

a Class & cows:DirectoryTemplateVariableGeneratorInterface in phpMAE
Public PHP Methods
  • getTemplateVariables($coid)

    Get a key-value array of template variables for an object.
    @param string $coid The COID of the object.

Source Code
<?php

use CloudObjects\SDK\NodeReader, CloudObjects\SDK\ObjectRetriever;
use ML\IRI\IRI;

/**
 * Implementation for coid://phpmae.cloudobjects.io/StackTemplateVariableGenerator
 * Using interface coid://cloudobjects.io/DirectoryTemplateVariableGeneratorInterface
 */
class StackTemplateVariableGenerator implements DirectoryTemplateVariableGeneratorInterface {

    private $retriever;
    private $reader;

    public function __construct(ObjectRetriever $retriever) {
        $this->retriever = $retriever;
        $this->reader = new NodeReader([
            'prefixes' => [
                'co' => 'coid://cloudobjects.io/',
                'phpmae' => 'coid://phpmae.cloudobjects.io/'
            ]
        ]);
    }

    /**
     * Get a key-value array of template variables for an object.
     * @param string $coid The COID of the object.
     */
    public function getTemplateVariables($coid) {
        $coid = new IRI($coid);
        $object = $this->retriever->getObject($coid);
        if (!isset($object) || !$this->reader->hasType($object, 'phpmae:Stack'))
            return false;
        
        // Retrieve files
        $composerFile = json_decode($this->retriever->getAttachment($coid,
            $this->reader->getFirstValueString($object, 'phpmae:hasAttachedComposerFile')), true);
        $lockFile = json_decode($this->retriever->getAttachment($coid,
            $this->reader->getFirstValueString($object, 'phpmae:hasAttachedLockFile')), true);

        // Compile output
        $output = [
            'defined' => [],
            'actual' => []
        ];

        foreach ($composerFile['require'] as $name => $version) {
            $output['defined'][] = [
                'name' => $name,
                'version' => $version
            ];
        }

        foreach ($lockFile['packages'] as $package) {
            $output['actual'][] = [
                'name' => $package['name'],
                'version' => $package['version'],
                'defined' => isset($composerFile['require'][$package['name']])
            ];
        }

        return $output;
    }
}
Meta
URI / COID
coid://phpmae.cloudobjects.io/StackTemplateVariableGenerator content_copy
Revision
5-90009c479765ed4f270a01e1a08c317c content_copy
Last updated
2020-09-08 15:22 (UTC)
Usage permission
co:Public