public function EventHandler(Console\Event $pEvent)
 {
     try {
         $console = $this->getServiceLocator()->get('console');
         //            $config = $this->getServiceLocator()->get('Config');
         //            $config = new Config($config);
         /**
          * Place code here to check whether the configured endpoints are properly integrated or not.
          */
         if (true == $pEvent->getVerboseFlag()) {
             $console->write(' --------------- ', ColorInterface::LIGHT_GREEN);
             $console->writeLine('-----------------------------------------------------------', ColorInterface::YELLOW);
             $console->write("      [Listener] ", ColorInterface::LIGHT_GREEN);
             $console->writeLine(__CLASS__, ColorInterface::YELLOW);
             $console->write(' --------------- ', ColorInterface::LIGHT_GREEN);
             $console->writeLine('-----------------------------------------------------------', ColorInterface::YELLOW);
             /**
              * Place some verbose logging here for whatever you're checking above. Whether it passed/failed etc.
              */
         }
         return NULL;
     } catch (Exception $e) {
         $response = new Response();
         $response->setErrorLevel(1);
         $response->setContent('Exception Encountered: ' . $e->getMessage());
         return $response;
     }
 }
Example #2
0
 public function bootstrapSingleServerAction($args)
 {
     $zendServer = $this->serviceLocator->get('targetZendServer');
     $zendServer->setUri(new \Zend\Uri\Http($this->params('zsurl')));
     $zendServerClient = $this->serviceLocator->get('zendServerClient');
     // set the explicit timeout to 3 minutes
     $zendServerClient->setOptions(array('timeout' => 180));
     //
     $response = $this->sendApiRequest($args);
     $data = $response->responseData->bootstrap;
     $content = '';
     if (sprintf('%s', $data->success) == "true") {
         $name = sprintf("%s", $data->apiKey->name);
         $key = sprintf("%s", $data->apiKey->hash);
     }
     if (isset($args['simple-output'])) {
         $response = new Response();
         $response->setContent("{$name}\n{$key}\n");
     }
     $wait = $this->params('wait');
     if ($wait) {
         $keyService = $this->getServiceLocator()->get('defaultApiKey');
         $keyService->setName($name);
         $keyService->setKey($key);
         $this->repeater()->doUntil(array($this, 'onWaitBootstrapSingleServer'));
     }
     return $response;
 }
 /**
  * This action handles console requests to the Application.
  * It is expected that
  * the route that matches this action will set an "event" that is derived from
  * the Cornerstone\EventManager\Service
  *
  * It will handle adding a bunch of verbose error logging and will trigger the
  * provided 'event' through the EventManager. It is expected that developers
  * will attach Listeners to these events to handle the various calls that get
  * implemented.
  *
  * It is recommended that this is done by creating strategies in the Module.php
  * file by defining them in the getServiceConfig function, and calling them
  * in the onBootstrap method. Please see the packaged Module.php file for
  * examples.
  *
  * @return \Zend\Console\Response
  */
 public function eventAction()
 {
     /* This method makes sure we're in a console view, if not, tosses an exception */
     $this->RequireConsoleRequest();
     $this->mConsole = $this->getServiceLocator()->get('console');
     $this->mForce = $this->params('force', false);
     $this->mEvent = $this->params('event', false);
     $this->mVerbose = $this->params('verbose', false);
     $this->mEnvironment = $this->params()->fromRoute('env', $this->mEnvironment);
     /**
      * Output the standard Console Flag for verbose output
      */
     if (true == $this->mVerbose) {
         $this->mConsole->clearScreen();
         $this->mConsole->write("    [Controller] ", ColorInterface::LIGHT_GREEN);
         $this->mConsole->writeLine(__CLASS__, ColorInterface::YELLOW);
         $this->mConsole->write("        [Action] ", ColorInterface::LIGHT_GREEN);
         $this->mConsole->writeLine(__FUNCTION__, ColorInterface::YELLOW);
         $this->mConsole->write("       [Verbose] ", ColorInterface::LIGHT_GREEN);
         $this->mConsole->writeLine('true', ColorInterface::YELLOW);
         $this->mConsole->write("         [Force] ", ColorInterface::LIGHT_GREEN);
         $this->mConsole->writeLine(true === $this->mForce ? 'true' : 'false', ColorInterface::YELLOW);
         $this->mConsole->write(" [Event Manager] ", ColorInterface::LIGHT_GREEN);
         $this->mConsole->writeLine('Cornerstone\\EventManager\\Service', ColorInterface::YELLOW);
         $this->mConsole->write("       [Trigger] ", ColorInterface::LIGHT_GREEN);
         $this->mConsole->writeLine($this->mEvent, ColorInterface::YELLOW);
         $this->mConsole->writeLine();
     }
     $event = new EventManager\Console\Event();
     $event->setName($this->mEvent);
     $event->setTarget($this);
     $event->setForceFlag($this->mForce);
     $event->setVerboseFlag($this->mVerbose);
     $event->setEnvironment($this->mEnvironment);
     $event_result = $this->EventManager()->trigger($event);
     $response = new Response();
     $response->setErrorLevel(0);
     foreach ($event_result as $result) {
         if (is_object($result)) {
             /* @var Response $result */
             if (0 < $result->getErrorLevel()) {
                 $this->mConsole->write("[Error] ", ColorInterface::RED);
                 /* we have to use error log here so that it will write to stderr instead of stdout */
                 error_log($result->getContent());
                 $error_level = $result->getErrorLevel() + $response->getErrorLevel();
                 $response->setErrorLevel($error_level);
             } else {
                 $this->mConsole->writeLine($result->getContent());
             }
         }
     }
     if (true == $this->mVerbose && 0 == $response->getErrorLevel()) {
         $this->mConsole->write(' --------------- ', ColorInterface::LIGHT_GREEN);
         $this->mConsole->writeLine('-----------------------------------------------------------', ColorInterface::YELLOW);
         $this->mConsole->write("     [Completed] ", ColorInterface::LIGHT_GREEN);
         $this->mConsole->writeLine("Event processing", ColorInterface::YELLOW);
     }
     return $response;
 }
 public function EventHandler(Console\Event $pEvent)
 {
     try {
         $console = $this->getServiceLocator()->get('console');
         $config = $this->getServiceLocator()->get('Config');
         if (true == $pEvent->getVerboseFlag()) {
             $console->write(' --------------- ', ColorInterface::LIGHT_GREEN);
             $console->writeLine('-----------------------------------------------------------', ColorInterface::YELLOW);
             $console->write("      [Listener] ", ColorInterface::LIGHT_GREEN);
             $console->writeLine(__CLASS__, ColorInterface::YELLOW);
             $console->write(' --------------- ', ColorInterface::LIGHT_GREEN);
             $console->writeLine('-----------------------------------------------------------', ColorInterface::YELLOW);
             /**
              * Place some verbose logging here for whatever you're checking above. Whether it passed/failed etc.
              */
         }
         /**
          * Place code here to check the configuration value's existence, or to make sure it's set, etc.
          * It might be worth placing a NULL config variable in the module.config.php file so that
          * you can check to see what its value is. If you need something where NULL is an acceptable
          * value you may want to set a dummy unacceptable value, etc.
          */
         if ('example' == $config['Installation']['Vhost']['Server']['Domain']) {
             $error_message = 'Application config error, Installation->Vhost->Server->Domain is still set to default value.';
             if (true == $pEvent->getVerboseFlag()) {
                 $console->write("       [Failure] ", ColorInterface::RED);
                 $console->writeLine($error_message . PHP_EOL, ColorInterface::RED);
             }
             throw new Exception($error_message);
         }
         if ('app-skeleton.' == $config['Installation']['Vhost']['Server']['Region']) {
             $error_message = 'Application config error, Installation->Vhost->Server->Region is still set to default value.';
             if (true == $pEvent->getVerboseFlag()) {
                 $console->write("       [Failure] ", ColorInterface::RED);
                 $console->writeLine($error_message . PHP_EOL, ColorInterface::RED);
             }
             throw new Exception($error_message);
         }
         return NULL;
     } catch (Exception $e) {
         $response = new Response();
         $response->setErrorLevel(1);
         $response->setContent('Exception Encountered: ' . $e->getMessage());
         return $response;
     }
 }
Example #5
0
 public function testSetContent()
 {
     $this->response->setContent('foo, bar');
     $this->assertEquals(false, $this->response->contentSent());
     ob_start();
     $this->response->sendContent();
     $content = ob_get_clean();
     $this->assertEquals('foo, bar', $content);
     $this->assertEquals(true, $this->response->contentSent());
     $this->assertEquals($this->response, $this->response->sendContent());
 }
 public function EventHandler(Console\Event $pEvent)
 {
     try {
         $console = $this->getServiceLocator()->get('console');
         $config = $this->getServiceLocator()->get('Config');
         if (true == $pEvent->getVerboseFlag()) {
             $console->write(' --------------- ', ColorInterface::LIGHT_GREEN);
             $console->writeLine('-----------------------------------------------------------', ColorInterface::YELLOW);
             $console->write("      [Listener] ", ColorInterface::LIGHT_GREEN);
             $console->writeLine(__CLASS__, ColorInterface::YELLOW);
             $console->write(' --------------- ', ColorInterface::LIGHT_GREEN);
             $console->writeLine('-----------------------------------------------------------', ColorInterface::YELLOW);
         }
         if (true == $pEvent->getVerboseFlag()) {
             $console->write("  [Template Key] ");
             $console->writeLine($this->mTemplateKey, ColorInterface::YELLOW);
         }
         // create the view model for the vhost template
         $view = new ViewModel();
         $view->setTemplate($this->mTemplateKey);
         $vhost_config = $config['Installation']['Vhost'];
         $server = $vhost_config['Server'];
         $prefix = $server['Prefix'];
         $region = $server['Region'];
         $domain = $server['Domain'];
         $suffix = $server['Suffix'];
         $view->setVariable('ServerName', $prefix . $region . $domain . $suffix);
         $public = $server['PublicFolder'];
         $view->setVariable('DocumentRoot', getcwd() . '/' . $public);
         $view->setVariable('ApplicationEnv', $pEvent->getEnvironment());
         $view->setVariable('Config', $vhost_config);
         if (true == $pEvent->getVerboseFlag()) {
             $console->write("   [Server Name] ");
             $console->writeLine($view->getVariable('ServerName'), ColorInterface::YELLOW);
             $console->write(" [Document Root] ");
             $console->writeLine($view->getVariable('DocumentRoot'), ColorInterface::YELLOW);
             $console->write("   [Environment] ");
             $console->writeLine($view->getVariable('ApplicationEnv'), ColorInterface::YELLOW);
         }
         // setup specific configurations
         $view->setVariable('ApacheLogDir', $vhost_config['ApacheLog']);
         $view->setVariable('UseSyslog', $vhost_config['UseSysLog']);
         $view->setVariable('Ports', $vhost_config['Ports']);
         $view->setVariable('CorsOrigin', false);
         if (array_key_exists('CorsOrigin', $config['Installation'])) {
             $cors = $config['Installation']['CorsOrigin'];
             if (false === is_array($cors)) {
                 throw new Exception('CorsOrigin configuration must be an array.');
             }
             $origin_list = implode('|', $cors);
             $view->setVariable('CorsOrigin', 'http(s)?://(' . $origin_list . ')');
         }
         /**
          * Create a template map resolver from the template map in the config
          * file.
          * Using that we create a renderer that will parse the zf2 view
          * template like an ordinary template, so that we can get back its
          * contents
          */
         $map = new TemplateMapResolver($config['view_manager']['template_map']);
         $renderer = new PhpRenderer();
         $renderer->setResolver($map);
         // Rewrite/modsec rule additions
         $rewritePreView = new ViewModel();
         $rewritePreView->setTemplate($this->mRewriteRulesPreTemplateKey);
         $rewritePreView->setVariable('Config', $vhost_config);
         $view->setVariable('RewritePreRules', $renderer->render($rewritePreView));
         $rewritePostView = new ViewModel();
         $rewritePostView->setTemplate($this->mRewriteRulesPostTemplateKey);
         $rewritePostView->setVariable('Config', $vhost_config);
         $view->setVariable('RewritePostRules', $renderer->render($rewritePostView));
         $modSec = new ViewModel();
         $modSec->setTemplate($this->mModsecTemplateKey);
         $modSec->setVariable('Config', $vhost_config);
         $view->setVariable('ModSecRules', $renderer->render($modSec));
         if (true == $pEvent->getVerboseFlag()) {
             $console->write(" [View Template] ");
             $console->writeLine(realpath($map->get($this->mTemplateKey)), ColorInterface::YELLOW);
         }
         // write the vhost file here....
         $vhost_extension = $server['Extension'];
         $vhost_path = $server['Path'];
         $vhost_filename = $view->getVariable('ServerName') . '.' . $vhost_extension;
         $vhost_file = $vhost_path . $vhost_filename;
         if (true == $pEvent->getVerboseFlag()) {
             $console->write("  [Apache VHost] ");
             $console->writeLine($vhost_file, ColorInterface::YELLOW);
         }
         if (false === is_dir($vhost_path)) {
             $result = mkdir($vhost_path, 0775, true);
             if (true === $result) {
                 if (true == $pEvent->getVerboseFlag()) {
                     $console->write("        [NOTICE] ", ColorInterface::LIGHT_CYAN);
                     $console->writeLine("Vhost directory has been created.", ColorInterface::CYAN);
                 }
             } else {
                 if (true == $pEvent->getVerboseFlag()) {
                     $console->write("       [Failure] ", ColorInterface::RED);
                     $console->writeLine('Failed to create vhost directory, ' . $vhost_path . PHP_EOL, ColorInterface::RED);
                 }
                 throw new Exception('Failed to create vhost directory, ' . $vhost_path);
             }
         }
         if (false === is_writable($vhost_path)) {
             if (true == $pEvent->getVerboseFlag()) {
                 $console->write("       [Failure] ", ColorInterface::RED);
                 $console->writeLine("Vhost directory ({$vhost_path}) is not writable by web server." . PHP_EOL, ColorInterface::RED);
             }
             throw new Exception("Vhost directory ({$vhost_path}) is not writable by web server.");
         }
         if (file_exists($vhost_file) && false === $pEvent->getForceFlag()) {
             if (true == $pEvent->getVerboseFlag()) {
                 $console->write("        [NOTICE] ", ColorInterface::LIGHT_CYAN);
                 $console->writeLine('Apache VHost file already exists, skipping creation.', ColorInterface::CYAN);
                 $console->write("          [INFO] ", ColorInterface::LIGHT_CYAN);
                 $console->writeLine('To overwrite the existing file, use --force' . PHP_EOL, ColorInterface::CYAN);
             }
         } else {
             if (file_exists($vhost_file) && false === is_writable($vhost_file)) {
                 if (true == $pEvent->getVerboseFlag()) {
                     $console->write("       [Failure] ", ColorInterface::RED);
                     $console->writeLine('Apache VHost Not Writable!' . PHP_EOL, ColorInterface::RED);
                 }
                 throw new Exception(sprintf('Virtual host file %s is not writable.', $vhost_file));
             } else {
                 $pointer = fopen($vhost_file, 'w+');
                 if ($pointer === false) {
                     if (true == $pEvent->getVerboseFlag()) {
                         $console->write("       [Failure] ", ColorInterface::RED);
                         $console->writeLine('Failed to open Apache VHost for writing!' . PHP_EOL, ColorInterface::RED);
                     }
                     throw new Exception(sprintf('Failed to open Virtual host file %s for writing.', $vhost_file));
                 } else {
                     fwrite($pointer, $renderer->render($view));
                     fclose($pointer);
                     if (true == $pEvent->getVerboseFlag()) {
                         $console->write('       [Success] ', ColorInterface::LIGHT_GREEN);
                         $console->writeLine('VHost File Update Complete' . PHP_EOL, ColorInterface::YELLOW);
                         /* adding some extra spacing for the notice outside of this if block */
                         $console->write('        ');
                     }
                     $notice = $console->colorize('[NOTICE] ', ColorInterface::LIGHT_CYAN);
                     $response = new Response();
                     $response->setContent($notice . 'Virtual Host file has been updated, you may need to restart/reload your web server.');
                     return $response;
                 }
             }
         }
         return NULL;
     } catch (Exception $e) {
         $response = new Response();
         $response->setErrorLevel(1);
         $response->setContent('Exception Encountered: ' . $e->getMessage());
         return $response;
     }
 }
 public function EventHandler(Console\Event $pEvent)
 {
     try {
         $console = $this->getServiceLocator()->get('console');
         $config = $this->getServiceLocator()->get('ApplicationConfig');
         if (true == $pEvent->getVerboseFlag()) {
             $console->write(' --------------- ', ColorInterface::LIGHT_GREEN);
             $console->writeLine('-----------------------------------------------------------', ColorInterface::YELLOW);
             $console->write("      [Listener] ", ColorInterface::LIGHT_GREEN);
             $console->writeLine(__CLASS__, ColorInterface::YELLOW);
             $console->write(' --------------- ', ColorInterface::LIGHT_GREEN);
             $console->writeLine('-----------------------------------------------------------', ColorInterface::YELLOW);
         }
         if (false === array_key_exists('module_listener_options', $config)) {
             if (true == $pEvent->getVerboseFlag()) {
                 $console->write("        [NOTICE] ", ColorInterface::LIGHT_CYAN);
                 $console->writeLine("Application config does not contain an entry for 'module_listener_options'.", ColorInterface::CYAN);
                 $console->write("                 ", ColorInterface::LIGHT_CYAN);
                 $console->writeLine("Skipping creation of application cache folder." . PHP_EOL, ColorInterface::CYAN);
             }
             return NULL;
         }
         if (false === array_key_exists('cache_dir', $config['module_listener_options'])) {
             if (true == $pEvent->getVerboseFlag()) {
                 $console->write("       [Failure] ", ColorInterface::RED);
                 $console->writeLine('Application config error, module_listener_options, does not contain a "cache_dir" entry!' . PHP_EOL, ColorInterface::RED);
             }
             throw new Exception('Application config error, module_listener_options, does not contain a "cache_dir" entry!');
         }
         $cache_dir = $config['module_listener_options']['cache_dir'];
         if (true == $pEvent->getVerboseFlag()) {
             $console->write("     [Cache Dir] ");
             $console->writeLine($cache_dir, ColorInterface::YELLOW);
         }
         if (false === is_dir($cache_dir)) {
             $result = mkdir($cache_dir, 0775, true);
             if (true === $result) {
                 if (true == $pEvent->getVerboseFlag()) {
                     $console->write("        [NOTICE] ", ColorInterface::LIGHT_CYAN);
                     $console->writeLine("Cache directory has been created.", ColorInterface::CYAN);
                 }
             } else {
                 if (true == $pEvent->getVerboseFlag()) {
                     $console->write("       [Failure] ", ColorInterface::RED);
                     $console->writeLine('Failed to create cache directory, ' . $cache_dir . PHP_EOL, ColorInterface::RED);
                 }
                 throw new Exception('Failed to create cache directory, ' . $cache_dir);
             }
         }
         if (false === is_writable($cache_dir)) {
             if (true == $pEvent->getVerboseFlag()) {
                 $console->write("       [Failure] ", ColorInterface::RED);
                 $console->writeLine("Cache directory ({$cache_dir}) is not writable by web server." . PHP_EOL, ColorInterface::RED);
             }
             throw new Exception("Cache directory ({$cache_dir}) is not writable by web server.");
         }
         if (true == $pEvent->getVerboseFlag()) {
             $console->write("       [Success] ", ColorInterface::GREEN);
             $console->writeLine("Application cache folder ({$cache_dir}) exists and is writable." . PHP_EOL);
         }
         return NULL;
     } catch (Exception $e) {
         $response = new Response();
         $response->setErrorLevel(1);
         $response->setContent('Exception Encountered: ' . $e->getMessage());
         return $response;
     }
 }
 public function EventHandler(Console\Event $pEvent)
 {
     try {
         $console = $this->getServiceLocator()->get('console');
         $config = $this->getServiceLocator()->get('ApplicationConfig');
         if (true == $pEvent->getVerboseFlag()) {
             $console->write(' --------------- ', ColorInterface::LIGHT_GREEN);
             $console->writeLine('-----------------------------------------------------------', ColorInterface::YELLOW);
             $console->write("      [Listener] ", ColorInterface::LIGHT_GREEN);
             $console->writeLine(__CLASS__, ColorInterface::YELLOW);
             $console->write(' --------------- ', ColorInterface::LIGHT_GREEN);
             $console->writeLine('-----------------------------------------------------------', ColorInterface::YELLOW);
         }
         if (false === array_key_exists('module_listener_options', $config)) {
             if (true == $pEvent->getVerboseFlag()) {
                 $console->write("        [Failure] ", ColorInterface::RED);
                 $console->writeLine("Application config does not contain an entry for 'module_listener_options'.", ColorInterface::RED);
                 $console->write("                 ", ColorInterface::LIGHT_CYAN);
                 $console->writeLine("Skipping emptying of application cache folder." . PHP_EOL, ColorInterface::CYAN);
             }
             throw new Exception('Application config error, module_listener_options, does not exist. Directory empty failed!');
         }
         if (false === array_key_exists('cache_dir', $config['module_listener_options'])) {
             if (true == $pEvent->getVerboseFlag()) {
                 $console->write("       [Failure] ", ColorInterface::RED);
                 $console->writeLine('Application config error, module_listener_options, does not contain a "cache_dir" entry!' . PHP_EOL, ColorInterface::RED);
             }
             throw new Exception('Application config error, module_listener_options, does not contain a "cache_dir" entry!');
         }
         $cache_dir = $config['module_listener_options']['cache_dir'];
         if (true == $pEvent->getVerboseFlag()) {
             $console->write("     [Cache Dir] ");
             $console->writeLine($cache_dir, ColorInterface::YELLOW);
         }
         if (false === is_writable($cache_dir)) {
             if (true == $pEvent->getVerboseFlag()) {
                 $console->write("       [Failure] ", ColorInterface::RED);
                 $console->writeLine("Cache directory ({$cache_dir}) is not writable by web server." . PHP_EOL, ColorInterface::RED);
             }
             throw new Exception("Cache directory ({$cache_dir}) is not writable by web server.");
         }
         if (true == $pEvent->getVerboseFlag()) {
             $console->write("        [NOTICE] ", ColorInterface::LIGHT_CYAN);
             $console->writeLine("Emptying cache directory of any existing files/folders.", ColorInterface::CYAN);
         }
         $iterator = new RecursiveDirectoryIterator($cache_dir, FilesystemIterator::SKIP_DOTS);
         foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $path) {
             /* @var RecursiveDirectoryIterator $path */
             if (true == $pEvent->getVerboseFlag()) {
                 $console->write("        [NOTICE] ", ColorInterface::LIGHT_CYAN);
                 $console->writeLine("Deleting " . $path->getPathname(), ColorInterface::CYAN);
             }
             $path->isFile() ? unlink($path->getPathname()) : rmdir($path->getPathname());
         }
         if (true == $pEvent->getVerboseFlag()) {
             $console->write("       [Success] ", ColorInterface::GREEN);
             $console->writeLine("Application cache folder ({$cache_dir}) has been emptied." . PHP_EOL);
         }
         return NULL;
     } catch (Exception $e) {
         $response = new Response();
         $response->setErrorLevel(1);
         $response->setContent('Exception Encountered: ' . $e->getMessage());
         return $response;
     }
 }