Example #1
0
    protected function getTableSource(ServiceDefinition $definition, $table)
    {
        $namespace = $definition->getNamespace();
        $className = $definition->getClassName();
        $sm = $this->connection->getSchemaManager();
        $columns = $sm->listTableColumns($table);
        $indexes = $sm->listTableIndexes($table);
        $properties = array();
        foreach ($columns as $column) {
            $isPrimary = false;
            foreach ($indexes as $index) {
                if ($index->isPrimary() && in_array($column->getName(), $index->getColumns())) {
                    $isPrimary = true;
                    break;
                }
            }
            $properties[] = $this->convertDoctrineTypeToString($column, $isPrimary);
        }
        $columns = '';
        foreach ($properties as $property) {
            $columns .= $property . "\n";
        }
        $columns = trim($columns);
        return <<<PHP
<?php

namespace {$namespace};

use PSX\\Sql\\TableAbstract;

/**
 * {$className}
 *
 * @see http://phpsx.org/doc/concept/table.html
 */
class {$className} extends TableAbstract
{
\tpublic function getName()
\t{
\t\treturn '{$table}';
\t}

\tpublic function getColumns()
\t{
\t\treturn array(
\t\t\t{$columns}
\t\t);
\t}
}

PHP;
    }
Example #2
0
    protected function getCommandSource(ServiceDefinition $definition)
    {
        $namespace = $definition->getNamespace();
        $className = $definition->getClassName();
        $services = '';
        foreach ($definition->getServices() as $serviceName) {
            $services .= $this->getServiceSource($serviceName) . "\n\n";
        }
        $services = trim($services);
        return <<<PHP
<?php

namespace {$namespace};

use PSX\\CommandAbstract;
use PSX\\Command\\Parameter;
use PSX\\Command\\Parameters;
use PSX\\Command\\OutputInterface;

/**
 * {$className}
 *
 * @see http://phpsx.org/doc/design/command.html
 */
class {$className} extends CommandAbstract
{
\t{$services}

\tpublic function onExecute(Parameters \$parameters, OutputInterface \$output)
\t{
\t\t\$foo = \$parameters->get('foo');
\t\t\$bar = \$parameters->get('bar');

\t\t// @TODO do a task

\t\t\$output->writeln('This is an PSX sample command');
\t}

\tpublic function getParameters()
\t{
\t\treturn \$this->getParameterBuilder()
\t\t\t->setDescription('Displays informations about an foo command')
\t\t\t->addOption('foo', Parameter::TYPE_REQUIRED, 'The foo parameter')
\t\t\t->addOption('bar', Parameter::TYPE_OPTIONAL, 'The bar parameter')
\t\t\t->getParameters();
\t}
}

PHP;
    }
Example #3
0
    protected function getSchemaSource(ServiceDefinition $definition, $table)
    {
        $namespace = $definition->getNamespace();
        $className = $definition->getClassName();
        $name = lcfirst($className);
        if (!empty($table)) {
            $sm = $this->connection->getSchemaManager();
            $columns = $sm->listTableColumns($table);
            $properties = array();
            foreach ($columns as $column) {
                $type = $this->convertDoctrinTypeToString($column->getType());
                $properties[] = $this->getSchemaType($type, $column->getName());
            }
        } else {
            $properties = array($this->getSchemaType('integer', 'id'), $this->getSchemaType('string', 'title'), $this->getSchemaType('dateTime', 'date'));
        }
        $definition = '';
        foreach ($properties as $property) {
            $definition .= $property . "\n";
        }
        $definition = trim($definition);
        return <<<PHP
<?php

namespace {$namespace};

use PSX\\Data\\SchemaAbstract;

/**
 * {$className}
 *
 * @see http://phpsx.org/doc/concept/schema.html
 */
class {$className} extends SchemaAbstract
{
\tpublic function getDefinition()
\t{
\t\t\$sb = \$this->getSchemaBuilder('{$name}');
\t\t{$definition}

\t\treturn \$sb->getProperty();
\t}
}

PHP;
    }
Example #4
0
    protected function getControllerSource(ServiceDefinition $definition)
    {
        $namespace = $definition->getNamespace();
        $className = $definition->getClassName();
        $services = '';
        foreach ($definition->getServices() as $serviceName) {
            $services .= $this->getServiceSource($serviceName) . "\n\n";
        }
        $services = trim($services);
        return <<<PHP
<?php

namespace {$namespace};

use PSX\\ControllerAbstract;

/**
 * {$className}
 *
 * @see http://phpsx.org/doc/design/controller.html
 */
class {$className} extends ControllerAbstract
{
\t{$services}

\tpublic function doIndex()
\t{
\t\t// @TODO controller action

\t\t\$this->setBody(array(
\t\t\t'message' => 'This is the default controller of PSX',
\t\t));
\t}
}

PHP;
    }
Example #5
0
    protected function getControllerSource(ServiceDefinition $definition, $ramlFile)
    {
        $namespace = $definition->getNamespace();
        $className = $definition->getClassName();
        $services = '';
        foreach ($definition->getServices() as $serviceName) {
            $services .= $this->getServiceSource($serviceName) . "\n\n";
        }
        $services = trim($services);
        // documentation
        $documentation = '';
        if (!empty($ramlFile)) {
            if (!$this->isFile($ramlFile)) {
                throw new RuntimeException('RAML file "' . $ramlFile . '" does not exist');
            }
            $documentation = <<<PHP
return Documentation\\Parser\\Raml::fromFile('{$ramlFile}', \$this->context->get(Context::KEY_PATH));
PHP;
        } else {
            $documentation = <<<PHP
\$resource = new Resource(Resource::STATUS_ACTIVE, \$this->context->get(Context::KEY_PATH));

\t\t\$resource->addMethod(Resource\\Factory::getMethod('GET')
\t\t\t->addResponse(200, \$this->schemaManager->getSchema('{$namespace}\\Schema\\Collection')));

\t\treturn new Documentation\\Simple(\$resource);
PHP;
        }
        // controller
        return <<<PHP
<?php

namespace {$namespace};

use PSX\\Api\\Documentation;
use PSX\\Api\\Resource;
use PSX\\Api\\Version;
use PSX\\Controller\\SchemaApiAbstract;
use PSX\\Data\\RecordInterface;
use PSX\\Loader\\Context;

/**
 * {$className}
 *
 * @see http://phpsx.org/doc/design/controller.html
 */
class {$className} extends SchemaApiAbstract
{
\t{$services}

\t/**
\t * @return \\PSX\\Api\\DocumentationInterface
\t */
\tpublic function getDocumentation()
\t{
\t\t{$documentation}
\t}

\t/**
\t * Returns the GET response
\t *
\t * @param \\PSX\\Api\\Version \$version
\t * @return array|\\PSX\\Data\\RecordInterface
\t */
\tprotected function doGet(Version \$version)
\t{
\t\treturn array(
\t\t\t'message' => 'This is the default controller of PSX'
\t\t);
\t}

\t/**
\t * Returns the POST response
\t *
\t * @param \\PSX\\Data\\RecordInterface \$record
\t * @param \\PSX\\Api\\Version \$version
\t * @return array|\\PSX\\Data\\RecordInterface
\t */
\tprotected function doCreate(RecordInterface \$record, Version \$version)
\t{
\t}

\t/**
\t * Returns the PUT response
\t *
\t * @param \\PSX\\Data\\RecordInterface \$record
\t * @param \\PSX\\Api\\Version \$version
\t * @return array|\\PSX\\Data\\RecordInterface
\t */
\tprotected function doUpdate(RecordInterface \$record, Version \$version)
\t{
\t}

\t/**
\t * Returns the DELETE response
\t *
\t * @param \\PSX\\Data\\RecordInterface \$record
\t * @param \\PSX\\Api\\Version \$version
\t * @return array|\\PSX\\Data\\RecordInterface
\t */
\tprotected function doDelete(RecordInterface \$record, Version \$version)
\t{
\t}
}

PHP;
    }