protected function getHeaders() { $config = Project::getConfig(); $headers = array('Content-Type' => 'application/json'); $public_key = $_SERVER['HOME'] . '/.ssh/id_rsa.pub'; if (file_exists($public_key)) { $headers['X-Public-Key'] = urlencode(file_get_contents($public_key)); } if (!empty($config) && isset($config['app_id']) && isset($config['key'])) { $headers['X-App-Id'] = $config['app_id']; $headers['X-App-Key'] = $config['key']; } $headers['User-Agent'] = 'hook-cli'; return $headers; }
<?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); } });
<?php use Client\Client; use Client\Project; return array('arg0' => 'keys', 'command' => 'keys', 'description' => 'List all application keys.', 'run' => function ($args) { $client = new Client(); $keys = $client->get("apps/keys"); $project = Project::getConfig(); if (!$args['json']) { echo "Application name: {$project['name']}" . PHP_EOL; echo "Application keys:" . PHP_EOL; foreach ($keys as $key) { echo "{" . PHP_EOL; echo "\tapp_id: {$key->app_id}" . PHP_EOL; echo "\tkey: " . $key->key . PHP_EOL; echo "\ttype: " . $key->type . PHP_EOL; echo "}" . PHP_EOL; } if (count($keys) > 0) { } } return $keys; });
<?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); } });
<?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); }
<?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); } });
// 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; });
<?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); } });
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;
} Console::loading_output("Deploying..."); $schedule_data = Utils::parse_yaml($root_directory . '/schedule.yaml'); $schedule = $schedule_data && isset($schedule_data['schedule']) ? $schedule_data['schedule'] : array(); // retrieve application config // compatibility with v0.2.x $config = null; if (file_exists($root_directory . '/config.yaml')) { echo "DEPRECATE WARNING: Please move 'hook-ext/config.yaml' file to 'hook-ext/config/config.yaml'." . PHP_EOL; // TODO: remove on v0.4.x $config = Utils::parse_yaml($root_directory . '/config.yaml'); } else { $config = Utils::parse_yaml($root_directory . '/config/config.yaml'); } // try to read environment-specific configuration $environment_config_file = $root_directory . '/config/config.' . Project::getEnvironment() . '.yaml'; if (file_exists($environment_config_file)) { $environment_config = Utils::parse_yaml($environment_config_file); $config = array_replace_recursive($config, $environment_config); } $stats = $client->post('apps/deploy', array('modules' => $module_sync, 'schema' => Utils::parse_yaml($root_directory . '/schema.yaml'), 'schedule' => $schedule, 'config' => $config, 'security' => Utils::parse_yaml($root_directory . '/security.yaml'), 'packages' => Utils::parse_yaml($root_directory . '/packages.yaml'))); // remove auto-generated PHP files if (count($js_converted_modules) > 0) { foreach ($js_converted_modules as $module) { unlink($module); } } if (isset($stats->error)) { Console::error($stats->error); } if (isset($stats->schedule)) {
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')) {
<?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 {