Example #1
0
<?php

use Client\Project;
return array('arg0' => 'generate:channel', 'command' => 'generate:channel <channel-name>', 'description' => 'Generate custom channel class.', 'run' => function ($args) use($commands) {
    if (strlen($args[1]) == 0) {
        throw new Exception("You must specify a channel name.");
    }
    $extension = $args['javascript'] ? '.js' : '.php';
    $dest = Project::root(Project::DIRECTORY_NAME) . '/channels/';
    $dest_file = $dest . $args[1] . $extension;
    @mkdir($dest, 0777, true);
    $template = file_get_contents(__DIR__ . '/../../templates/channel' . $extension);
    $template = preg_replace('/{name}/', ucfirst($args[1]), $template);
    $template = preg_replace('/{channel}/', $args[1], $template);
    file_put_contents($dest_file, $template);
    echo "Custom channel created at '{$dest_file}'." . PHP_EOL;
    if ($editor = getenv('EDITOR')) {
        $descriptors = array(array('file', '/dev/tty', 'r'), array('file', '/dev/tty', 'w'), array('file', '/dev/tty', 'w'));
        $process = proc_open("{$editor} {$dest_file}", $descriptors, $pipes);
    }
});
Example #2
0
<?php

use Client\Client;
use Client\Project;
use Client\Console;
return array('arg0' => 'generate:schema', 'command' => 'generate:schema', 'description' => 'Generate schema definition config.', 'run' => function ($args) use($commands) {
    $dest = Project::root(Project::DIRECTORY_NAME) . '/schema.yaml';
    @mkdir(dirname($dest), 0777, true);
    $client = new Client();
    $client->setRaiseExceptions(false);
    $cached_schema = $client->get('apps/schema');
    $yaml = new Symfony\Component\Yaml\Dumper();
    if (file_exists($dest)) {
        Console::output("You already have a schema.yaml file.\n" . "Your changes will be lost.\n" . "Overwrite? [y/n]");
        $overwrite = Console::readline() == 'y';
        if (!$overwrite) {
            throw new Exception("Aborted.");
        }
    }
    // format schema dump before save
    $schema_dump = "";
    if (!empty($cached_schema)) {
        $schema_dump = str_replace('  ', ' ', $yaml->dump(json_decode(json_encode($cached_schema), true), 3));
        $schema_dump = preg_replace('/^([a-z0-9_]+):\\n/m', "\n\$0", $schema_dump);
    }
    file_put_contents($dest, $schema_dump . file_get_contents(__DIR__ . '/../../templates/schema.yaml'));
    echo "Schema dumped at '{$dest}'." . PHP_EOL;
    if ($editor = getenv('EDITOR')) {
        $descriptors = array(array('file', '/dev/tty', 'r'), array('file', '/dev/tty', 'w'), array('file', '/dev/tty', 'w'));
        $process = proc_open("{$editor} {$dest}", $descriptors, $pipes);
    }
Example #3
0
<?php

use Client\Project;
return array('arg0' => 'generate:template', 'command' => 'generate:template <template-name>', 'description' => 'Generate HTML template.', 'run' => function ($args) use($commands) {
    if (strlen($args[1]) == 0) {
        throw new Exception("You must specify a template name.");
    }
    $template_name = basename($args[1], '.html');
    $dest = Project::root(Project::DIRECTORY_NAME) . '/templates/';
    $dest_file = $dest . $template_name . '.html';
    @mkdir($dest, 0777, true);
    $template = file_get_contents(__DIR__ . '/../../templates/template.html');
    $template = preg_replace('/{template_name}/', ucfirst($template_name), $template);
    file_put_contents($dest_file, $template);
    echo "Template created at '{$dest_file}'." . PHP_EOL;
    if ($editor = getenv('EDITOR')) {
        $descriptors = array(array('file', '/dev/tty', 'r'), array('file', '/dev/tty', 'w'), array('file', '/dev/tty', 'w'));
        $process = proc_open("{$editor} {$dest_file}", $descriptors, $pipes);
    }
});
Example #4
0
    // Generate security file
    $dest = Project::root(Project::DIRECTORY_NAME) . '/';
    @mkdir($dest, 0777, true);
    @mkdir($dest . 'config/', 0777, true);
    @mkdir($dest . 'credentials/', 0777, true);
    $default_config_files = array('security.yaml', 'packages.yaml', 'schedule.yaml', 'schema.yaml', 'config/config.yaml', 'config/config.environment.yaml');
    foreach ($default_config_files as $config_file) {
        $dest_file = $dest . $config_file;
        $template = file_get_contents(__DIR__ . '/../../templates/' . $config_file);
        // replace environment on file
        if (preg_match('/\\.environment\\./', $dest_file)) {
            $dest_file = preg_replace('/\\.(environment)\\./', '.' . Project::getEnvironment() . '.', $dest_file);
            $template = preg_replace('/{{environment}}/', Project::getEnvironment(), $template);
        }
        if (!file_exists($dest_file)) {
            Console::success('create ' . str_replace(Project::root(), "", $dest_file));
            file_put_contents($dest_file, $template);
        }
    }
    if (!$args['json']) {
        Project::createCredentialsDirectory();
        foreach ($app->keys as $key) {
            $credentials = array('app_id' => $key->app_id, 'key' => $key->key, 'type' => $key->type, 'endpoint' => Client::getEndpoint());
            $dest_file = Project::getCredentialsPath() . $key->type . '.json';
            file_put_contents($dest_file, json_encode($credentials));
            Console::success('create ' . str_replace(Project::root(), "", $dest_file));
        }
        Console::output('Application created successfully.');
    }
    return $app->keys;
});
Example #5
0
<?php

use Client\Project;
return array('arg0' => 'generate:seed', 'command' => 'generate:seed <collection-name>', 'description' => 'Generate seed template.', 'run' => function ($args) use($commands) {
    if (strlen($args[1]) == 0) {
        throw new Exception("You must specify a collection name for seeding.");
    }
    $collection = str_plural(strtolower($args[1]));
    $dest = Project::root(Project::DIRECTORY_NAME) . '/seeds/';
    $dest_file = $dest . $collection . '.yaml';
    @mkdir($dest, 0777, true);
    $template = file_get_contents(__DIR__ . '/../../templates/seed.yaml');
    $template = preg_replace('/{name}/', $collection, $template);
    file_put_contents($dest_file, $template);
    echo "Seed created at '{$dest_file}'." . PHP_EOL;
    if ($editor = getenv('EDITOR')) {
        $descriptors = array(array('file', '/dev/tty', 'r'), array('file', '/dev/tty', 'w'), array('file', '/dev/tty', 'w'));
        $process = proc_open("{$editor} {$dest_file}", $descriptors, $pipes);
    }
});
Example #6
0
<?php

use Client\Project;
return array('arg0' => 'generate:observer', 'command' => 'generate:observer <collection-name>', 'description' => 'Generate observer class for collection events.', 'run' => function ($args) use($commands) {
    if (!$args[1]) {
        throw new Exception("You must specify a collection name.");
    }
    $collection = str_plural(strtolower($args[1]));
    $extension = $args['javascript'] ? '.js' : '.php';
    $dest = Project::root(Project::DIRECTORY_NAME) . '/observers/';
    $dest_file = $dest . $collection . $extension;
    @mkdir($dest, 0777, true);
    $template = file_get_contents(__DIR__ . '/../../templates/observer' . $extension);
    $template = preg_replace('/{name}/', ucfirst($collection), $template);
    $template = preg_replace('/{collection}/', $collection, $template);
    file_put_contents($dest_file, $template);
    echo "Observer created at '{$dest_file}'." . PHP_EOL;
    if ($editor = getenv('EDITOR')) {
        $descriptors = array(array('file', '/dev/tty', 'r'), array('file', '/dev/tty', 'w'), array('file', '/dev/tty', 'w'));
        $process = proc_open("{$editor} {$dest_file}", $descriptors, $pipes);
    }
});
Example #7
0
         if (count($truncate) > 0) {
             echo "Success.";
         }
     } catch (Exception $e) {
         echo "Failed.";
     }
     echo PHP_EOL;
 }
 if (isset($options['data']) && $options['data']) {
     $current_row = 0;
     $total_rows = count($options['data']);
     foreach ($options['data'] as $data) {
         // Look for special data fields
         foreach ($data as $field => $value) {
             if (preg_match('/\\!upload ([^$]+)/', $value, $file)) {
                 $filepath = Project::root(Project::DIRECTORY_NAME) . '/seeds/' . $file[1];
                 // stop when file doens't exists
                 if (!file_exists($filepath)) {
                     throw new Exception("File not found: '{$filepath}'");
                 }
                 $mime_type = Utils::mime_type($filepath);
                 $data[$field] = 'data:' . $mime_type . ';base64,' . base64_encode(file_get_contents($filepath));
             }
         }
         $client->post('collection/' . $collection, array('data' => $data));
         $current_row += 1;
         $percent = round($current_row / $total_rows * 100);
         echo "Seeding '{$collection}': " . "{$percent}%" . str_repeat("\r", strlen($percent) + 1);
     }
 }
 echo PHP_EOL;
Example #8
0
<?php

use Client\Console;
use Client\Client;
use Client\Project;
use Client\Utils;
use Carbon\Carbon;
return array('arg0' => 'deploy', 'command' => 'deploy', 'description' => 'Deploy ext directory.', 'run' => function ($args) use($commands) {
    $client = new Client();
    Console::loading_output("Retrieving remote data...");
    $deployed = $client->get('apps/deploy');
    $root_directory = Project::root(Project::DIRECTORY_NAME);
    $js_converted_modules = array();
    // modules
    $local_modules = array();
    $module_types = array('observers', 'routes', 'templates', 'channels');
    foreach ($module_types as $module_type) {
        foreach (Utils::glob($root_directory . '/' . $module_type . '/*') as $module) {
            if (!is_file($module)) {
                continue;
            }
            $extension = pathinfo($module, PATHINFO_EXTENSION);
            if ($extension == "js") {
                $javascript_module = $module;
                $module = preg_replace('/\\.js$/', '.php', $module);
                array_push($js_converted_modules, $module);
                // keep track for removal
                // module may already exists due error on previous upload.
                // ask user to prevent overwriting some manually added .php file
                if (file_exists($module)) {
                    Console::output("Module '" . basename($module) . "' already exists.\nDo you want to overwrite with converted '" . basename($javascript_module) . "' version?\n(y/n)");
Example #9
0
use Client\Project;
return array('arg0' => 'generate:route', 'command' => 'generate:route <path> [<method=GET>]', 'description' => 'Generate a custom route for the application.', 'run' => function ($args) use($commands) {
    if (!$args[1]) {
        throw new Exception("You must specify a path.");
    }
    $extension = $args['javascript'] ? '.js' : '.php';
    $route_path = strtolower($args[1]);
    $route_method = strtolower($args[2] ?: 'get');
    $route_method_uppercase = strtoupper($route_method);
    $route_filename = $route_method . '_' . preg_replace('/:/', '', $route_path);
    $route_filename = preg_replace('/\\//', '_', $route_filename);
    // append / at the beggining of the route if it doesn't exist
    if (strpos($route_path, '/') !== 0) {
        $route_path = '/' . $route_path;
    }
    $dest = Project::root(Project::DIRECTORY_NAME) . '/routes/';
    $dest_file = $dest . $route_filename . $extension;
    @mkdir($dest, 0777, true);
    $arguments_list = array();
    preg_match_all('/:([a-z]+)/', $route_path, $arguments);
    foreach ($arguments[1] as $arg) {
        array_push($arguments_list, '$' . $arg);
    }
    $template = file_get_contents(__DIR__ . '/../../templates/route' . $extension);
    $template = preg_replace('/{path}/', $route_path, $template);
    $template = preg_replace('/{method}/', $route_method, $template);
    $template = preg_replace('/{method_uppercase}/', $route_method_uppercase, $template);
    $template = preg_replace('/{arguments}/', implode(", ", $arguments_list), $template);
    file_put_contents($dest_file, $template);
    echo "Route created at '{$dest_file}'." . PHP_EOL;
    if ($editor = getenv('EDITOR')) {
Example #10
0
<?php

use Client\Utils;
use Client\Console;
use Client\Project;
return array('arg0' => 'config:set', 'command' => 'config:set <name=value> [<name=value> ...]', 'description' => 'Set a configuration to app.', 'run' => function ($args) use($commands) {
    $config_file = Project::root(Project::DIRECTORY_NAME) . '/config.yaml';
    $configs = array();
    if (file_exists($config_file)) {
        $parser = new Symfony\Component\Yaml\Parser();
        $configs = $parser->parse(file_get_contents($config_file));
    }
    $configs_to_add = array();
    foreach ($args as $arg) {
        if (!is_null($arg) && preg_match('/=/', $arg)) {
            $config = preg_split('/=/', $arg);
            $name = $config[0];
            $value = $config[1];
            //
            // Read or extract certificate file
            // --------------------------------
            //
            if (file_exists($value)) {
                Console::output("Reading certificate file...");
                $ext = pathinfo($value, PATHINFO_EXTENSION);
                if ($ext == 'p12') {
                    $results = array();
                    $worked = openssl_pkcs12_read(file_get_contents($value), $results, null);
                    if ($worked) {
                        $value = $results['cert'] . $results['pkey'];
                    } else {