Пример #1
0
 public function childServer($addr)
 {
     $output = $this->output;
     $bindTo = json_decode($addr, true);
     $this->childHost = $bindTo['host'];
     $this->port = $bindTo['port'];
     cli_set_process_title("mpcmf/console server:run/child -b {$this->childHost} -p {$this->port}");
     posix_setgid(99);
     posix_setuid(99);
     posix_seteuid(99);
     posix_setegid(99);
     $loop = Factory::create();
     $socket = new reactSocketServer($loop);
     $http = new reactHttpServer($socket);
     $http->on('request', function (reactRequest $request, reactResponse $response) use($output) {
         //MPCMF_DEBUG && $output->writeln("<info>[CHILD:{$this->port}]</info> New connection");
         //MPCMF_DEBUG && $clientName = $request->getRemoteAddress() . '#' . spl_object_hash($request);
         //MPCMF_DEBUG && $output->writeln("<info>[{$clientName}] Client connected");
         profiler::resetStack();
         if (!$this->prepare($request, $response, $output)) {
             return;
         }
         //MPCMF_DEBUG && $output->writeln("<info>[{$clientName}] Starting application</info>");
         try {
             $app = $this->app();
             $slim = $app->slim();
             $originApplication = $this->applicationInstance->getCurrentApplication();
             $this->applicationInstance->setApplication($app);
             $slim->call();
         } catch (\Exception $e) {
             $response->writeHead(500);
             $response->end("Exception: {$e->getMessage()} in {$e->getFile()}:{$e->getLine()}\n{$e->getTraceAsString()}");
             return;
         }
         /** @var int[]|Headers[]|string[] $content */
         $content = $slim->response->finalize();
         Util::serializeCookies($content[1], $slim->response->cookies, $slim->settings);
         $content[1] = $content[1]->all();
         $this->applicationInstance->setApplication($originApplication);
         //MPCMF_DEBUG && $output->writeln("<info>[{$clientName}] Ending application</info>");
         //MPCMF_DEBUG && $output->writeln("<info>[CHILD:{$this->port}]</info> Writing data and closing connection");
         static $serverSoftware;
         if ($serverSoftware === null) {
             $serverSoftware = 'MPCMF Async PHP ' . phpversion();
         }
         if (array_key_exists('HTTP_ACCEPT_ENCODING', $_SERVER) && strpos($_SERVER["HTTP_ACCEPT_ENCODING"], 'gzip') !== false) {
             $content[1]['Content-Encoding'] = 'gzip';
             $content[2] = gzencode($content[2], 9);
         }
         $content[1]['X-PHP-Server'] = $serverSoftware;
         $content[1]['X-PHP-Server-Addr'] = "{$this->childHost}:{$this->port}";
         $response->writeHead($content[0], $content[1]);
         $response->end($content[2]);
         //MPCMF_DEBUG && $output->writeln("<info>[CHILD:{$this->port}]</info> Connection closed");
     });
     $output->writeln("<error>[CHILD]</error> Starting child server on {$this->childHost}:{$this->port}");
     $socket->listen($this->port, $this->childHost);
     $loop->run();
 }
Пример #2
0
 /**
  * @return applicationBase|consoleApplicationBase|consoleBase|webApplicationBase
  */
 protected function getApplication()
 {
     static $instance;
     if ($instance === null) {
         $instance = applicationInstance::getInstance()->getCurrentApplication();
     }
     return $instance;
 }
Пример #3
0
 public function getRealInviteLinks()
 {
     if ($this->inviteLinks === null || array_diff(array_keys($this->inviteLinks), $this->getInviteLinks())) {
         $this->inviteLinks = [];
         $app = applicationInstance::getInstance()->getCurrentApplication();
         foreach ($this->getInviteLinks() as $invite) {
             $invitePath = $app->getUrl('/authex/user/invite', ['invite' => $invite]);
             $this->inviteLinks[] = sprintf(self::SPF_INVITE, $_SERVER['HTTP_HOST'], $invitePath);
         }
     }
     return $this->inviteLinks;
 }
Пример #4
0
 /**
  * Executes the current command.
  *
  * This method is not because you can use this class
  * as a concrete class. In this case, instead of defining the
  * execute() method, you set the code to execute by passing
  * a Closure to the setCode() method.
  *
  * @param InputInterface  $input  An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  *
  * @return null|int null or 0 if everything went fine, or an error code
  *
  * @throws \LogicException When this method is not implemented
  *
  * @see setCode()
  */
 protected function handle(InputInterface $input, OutputInterface $output)
 {
     $config = ['module' => null, 'entityName' => null, 'params' => ['public-name' => '-', 'description' => '-', 'authors' => '-', 'mapGenerate' => '-', 'map' => '-', 'save-all' => '-', 'save-config' => '-']];
     $pattern = '/\\\\([^\\\\]+)\\\\console$/ui';
     $replace = '\\\\$1\\\\$1';
     /** @var webApplicationBase $webAppClass */
     $webAppClass = preg_replace($pattern, $replace, get_class(applicationInstance::getInstance()->getCurrentApplication()));
     $output->writeln('<info>Please, select application:</info>');
     $applicationMenu = [];
     foreach (scandir(APP_ROOT . '/apps') as $appName) {
         $appNamespace = APP_NAME . "\\apps\\" . $appName . "\\";
         $appFullClass = $appNamespace . $appName;
         $appConsoleClass = $appNamespace . 'console';
         if (!class_exists($appConsoleClass) && !class_exists($appFullClass)) {
             continue;
         }
         $applicationMenu[$appFullClass] = $appName;
     }
     /** @var webApplicationBase $baseAppClass */
     $baseAppClass = cli\menu($applicationMenu, array_search($webAppClass, $applicationMenu, false), 'Select an application');
     $baseApp = $baseAppClass::getInstance();
     $output->writeln("<info>Selected application: {$baseAppClass}</info>");
     $output->writeln('<info>Please, select module:</info>');
     $modules = [];
     foreach ($baseApp->getModules() as $module) {
         $name = $module->getModuleName();
         $modules[$name] = $name;
     }
     $selectedModule = cli\menu($modules, null, 'Select module');
     if (!isset($modules[$selectedModule])) {
         $output->writeln("<error>Module not found: {$selectedModule}</error>");
     }
     $module = $baseApp->getModules()[$selectedModule];
     $moduleClass = get_class($module);
     $output->writeln("<info>Selected module: {$moduleClass}</info>");
     $config['module'] = $module;
     $config['entityName'] = trim(readline('Entity class name: '));
     $entityHandler = new entityHolder($moduleClass, $config['entityName']);
     for (;;) {
         $menu = [];
         foreach ($config['params'] as $key => $data) {
             $menu[$key] = ucfirst($key) . ' ' . json_encode($data);
         }
         $menu['exit'] = 'Exit';
         $selected = cli\menu($menu);
         switch ($selected) {
             case 'public-name':
                 $publicName = trim(readline('Public human readable name: '));
                 $config['params']['public-name'] = '+';
                 $entityHandler->setPublicName($publicName);
                 break;
             case 'description':
                 $output->writeln('Empty line means end of description');
                 $description = '';
                 do {
                     $read = trim(readline('> '));
                     $description .= $read . "\n";
                 } while (!empty($read));
                 $config['params']['description'] = '+';
                 $entityHandler->setDescription(trim($description));
                 break;
             case 'authors':
                 $output->writeln('Add authors. Empty name means end of add');
                 $authors = [];
                 do {
                     $name = trim(readline('Name: '));
                     if (empty($name)) {
                         break;
                     }
                     $email = trim(readline('Email: '));
                     $authors[] = ['name' => $name, 'email' => $email];
                 } while (!empty($name));
                 $config['params']['authors'] = '+';
                 $entityHandler->setAuthors($authors);
                 break;
             case 'mapGenerate':
                 $output->writeln('Generating map...');
                 $dbConfig = $entityHandler->getDbConfig();
                 if (!$dbConfig) {
                     $dbConfig = [];
                     $dbConfig['configSection'] = trim(readline('Server: '));
                     $dbConfig['dbName'] = trim(readline('Database: '));
                     $dbConfig['collectionName'] = trim(readline('Collection: '));
                     $entityHandler->setDbConfig($dbConfig);
                 }
                 $count = (int) trim(readline('Limit items {example: 50}: '));
                 $generator = new entityMapGenerator($dbConfig['configSection'], $dbConfig['dbName'], $dbConfig['collectionName']);
                 $count > 0 && $generator->setLimit($count);
                 $dbFields = $generator->process();
                 $totalFields = count($dbFields);
                 error_log("Found fields in database: {$totalFields}");
                 $map = [];
                 $fieldCounter = 0;
                 foreach ($dbFields as $fieldName => $fieldData) {
                     $fieldCounter++;
                     $output->writeln("Processing field {$fieldName} ({$fieldCounter}/{$totalFields})");
                     $map[$fieldName] = ['roles' => [], 'relations' => [], 'validator' => [], 'options' => $fieldData['options'], 'name' => $fieldData['name'], 'description' => $fieldData['name'], 'type' => $fieldData['type']];
                     $askNameMenu = ['auto' => $fieldData['name'], 'manual' => 'Type manually'];
                     $askNameChoose = cli\menu($askNameMenu, 'auto', "Name for field: {$fieldName}");
                     if ($askNameChoose === 'manual') {
                         $map[$fieldName]['name'] = trim(readline('Type name: '));
                     }
                     $askDescriptionMenu = ['auto' => $fieldData['name'], 'manual' => 'Type manually'];
                     $askDescriptionChoose = cli\menu($askDescriptionMenu, 'auto', "Description for field: {$fieldName}");
                     if ($askDescriptionChoose === 'manual') {
                         $map[$fieldName]['description'] = trim(readline('Type description: '));
                     }
                     $typeMenu = ['int' => 'Integer', 'int[]' => 'Array of integer values', 'float' => 'Float', 'float[]' => 'Array of float values', 'string' => 'String', 'string[]' => 'Array of string values', 'boolean' => 'Boolean', 'boolean[]' => 'Array of boolean values', 'array' => 'Array'];
                     $map[$fieldName]['type'] = cli\menu($typeMenu, $fieldData['type'], "Select type of {$fieldName}");
                     $formTypeMenu = ['default' => ['text' => 'text', 'json' => 'json', 'checkbox' => 'checkbox', 'datetimepicker' => 'datetimepicker', 'geojson' => 'geojson', 'multitext' => 'multitext', 'multiselect' => 'multiselect', 'radio' => 'radio', 'operationValueSelect' => 'operationValueSelect', 'select' => 'select', 'password' => 'password', 'textarea' => 'textarea', 'timepicker' => 'timepicker'], 'int' => ['datetimepicker' => 'datetimepicker', 'timepicker' => 'timepicker', 'text' => 'text', 'json' => 'json', 'select' => 'select', 'radio' => 'radio'], 'string' => ['text' => 'text', 'select' => 'select', 'textarea' => 'textarea', 'password' => 'password', 'json' => 'json', 'radio' => 'radio'], 'boolean' => ['checkbox' => 'checkbox', 'json' => 'json', 'radio' => 'radio', 'select' => 'select'], 'array' => ['multiselect' => 'multiselect', 'multitext' => 'multitext', 'json' => 'json', 'geojson' => 'geojson', 'textarea' => 'textarea']];
                     $formTypeMenuSelected = isset($formTypeMenu[$fieldData['type']]) ? $formTypeMenu[$fieldData['type']] : $formTypeMenu['default'];
                     $map[$fieldName]['formType'] = cli\menu($formTypeMenuSelected, array_keys($formTypeMenuSelected)[0], "Select form type of {$fieldName}");
                     $output->writeln('Add roles:');
                     $roles = [];
                     do {
                         $roleMenu = ['key' => 'ROLE__PRIMARY_KEY', 'generate-key' => 'ROLE__GENERATE_KEY', 'title' => 'ROLE__TITLE', 'searchable' => 'ROLE__SEARCHABLE', 'sortable' => 'ROLE__SORTABLE', 'query-field' => 'ROLE__QUERY_FIELD', 'geo-area' => 'ROLE__GEO_AREA', 'geo-point' => 'ROLE__GEO_POINT'];
                         foreach ($roleMenu as $roleKey => &$roleText) {
                             if (isset($roles[$roleKey]) && $roles[$roleKey]) {
                                 $roleText = "+ {$roleText}";
                             } else {
                                 $roleText = "- {$roleText}";
                             }
                         }
                         unset($roleText);
                         $roleMenu['done'] = 'Save and exit...';
                         $selectedRole = cli\menu($roleMenu, 'done', 'Select roles:');
                         if ($selectedRole === 'done') {
                             break;
                         }
                         if (!isset($roles[$selectedRole])) {
                             $roles[$selectedRole] = false;
                         }
                         $roles[$selectedRole] = !$roles[$selectedRole];
                     } while ($selectedRole !== 'done');
                     foreach ($roles as $roleKey => $roleValue) {
                         if ($roleValue === false) {
                             unset($roles[$roleKey]);
                         }
                     }
                     $map[$fieldName]['roles'] = $roles;
                     $output->writeln('Add options:');
                     $options = ['required' => false, 'unique' => false];
                     do {
                         $optionsMenu = ['required' => 'Required', 'unique' => 'Unique', 'done' => 'Done'];
                         foreach ($optionsMenu as $optionKey => &$optionText) {
                             if (isset($options[$optionKey]) && $options[$optionKey]) {
                                 $optionText = "+ {$optionText}";
                             } else {
                                 $optionText = "- {$optionText}";
                             }
                         }
                         unset($optionText);
                         $optionsMenu['done'] = 'Save and exit...';
                         $option = cli\menu($optionsMenu, 'done', 'Select options:');
                         if ($option === 'done') {
                             break;
                         }
                         $options[$option] = !$options[$option];
                     } while ($option !== 'done');
                     $map[$fieldName]['options'] = $options;
                 }
                 $config['params']['mapGenerate'] = '+';
                 $entityHandler->setFieldsMap($map);
                 break;
             case 'map':
                 $output->writeln('Add map. Empty field name means end of add');
                 $map = [];
                 do {
                     $fieldName = trim(readline('Field name: '));
                     if (empty($fieldName)) {
                         break;
                     }
                     $map[$fieldName] = ['roles' => [], 'relations' => [], 'validator' => []];
                     $map[$fieldName]['name'] = trim(readline('Name: '));
                     $map[$fieldName]['description'] = trim(readline('Description: '));
                     $typeMenu = ['int' => 'Integer', 'int[]' => 'Array of integer values', 'float' => 'Float', 'float[]' => 'Array of float values', 'string' => 'String', 'string[]' => 'Array of string values', 'boolean' => 'Boolean', 'boolean[]' => 'Array of boolean values', 'array' => 'Array'];
                     $map[$fieldName]['type'] = cli\menu($typeMenu);
                     $formTypeMenu = ['checkbox' => 'checkbox', 'datetimepicker' => 'datetimepicker', 'geojson' => 'geojson', 'json' => 'json', 'multitext' => 'multitext', 'multiselect' => 'multiselect', 'radio' => 'radio', 'operationValueSelect' => 'operationValueSelect', 'select' => 'select', 'text' => 'text', 'password' => 'password', 'textarea' => 'textarea', 'timepicker' => 'timepicker'];
                     $map[$fieldName]['formType'] = cli\menu($formTypeMenu);
                     $output->writeln('Add roles:');
                     $roleMenu = ['key' => 'ROLE__PRIMARY_KEY', 'generate-key' => 'ROLE__GENERATE_KEY', 'title' => 'ROLE__TITLE', 'searchable' => 'ROLE__SEARCHABLE', 'sortable' => 'ROLE__SORTABLE', 'query-field' => 'ROLE__QUERY_FIELD', 'geo-area' => 'ROLE__GEO_AREA', 'geo-point' => 'ROLE__GEO_POINT', '' => 'Done (Exit)'];
                     do {
                         $role = cli\menu($roleMenu);
                         if (empty($role)) {
                             break;
                         }
                         $map[$fieldName]['roles'][$role] = true;
                     } while (!empty($role));
                     $output->writeln('Add options:');
                     $options = ['required' => false, 'unique' => false];
                     do {
                         $optionsMenu = ['required' => 'Required: ' . json_encode($options['required']), 'unique' => 'Unique: ' . json_encode($options['unique']), 'done' => 'Done'];
                         $option = cli\menu($optionsMenu);
                         if ($option === 'done') {
                             break;
                         }
                         $options[$option] = !$options[$option];
                     } while ($option !== 'done');
                     $map[$fieldName]['options'] = $options;
                 } while (!empty($fieldName));
                 $config['params']['map'] = '+';
                 $entityHandler->setFieldsMap($map);
                 break;
             case 'save-all':
                 $config['params']['save-all'] = '+';
                 $entityHandler->saveAll();
                 break;
             case 'save-config':
                 $dbConfig = $entityHandler->getDbConfig();
                 if (!$dbConfig) {
                     $dbConfig = [];
                     $dbConfig['configSection'] = trim(readline('Server: '));
                     $dbConfig['dbName'] = trim(readline('Database: '));
                     $dbConfig['collectionName'] = trim(readline('Collection: '));
                     $entityHandler->setDbConfig($dbConfig);
                 }
                 $config['params']['save-config'] = '+';
                 $entityHandler->saveMapperConfig();
                 break;
             case 'exit':
                 break 2;
             default:
                 $output->writeln("Unknown command: {$selected}");
                 break;
         }
     }
 }