public function publishCode()
 {
     if (!file_exists(Config::$CORE['tmp_dir_path'])) {
         mkdir(Config::$CORE['tmp_dir_path'], 0777);
     }
     $handlers = $this->event_handlers_model->getCountEventHandlers() == 1 ? "handler" : "handlers";
     $timers = $this->event_handlers_model->getCountTimers() == 1 ? "timer" : "timers";
     Log::writeInfo("Deploying {$this->event_handlers_model->getCountEventHandlers()} event " . $handlers . " and {$this->event_handlers_model->getCountTimers()} " . $timers . " to the server… ");
     try {
         $code_zip_path = realpath(getcwd() . DS . Config::$CORE['tmp_dir_path']) . DS . 'code.zip';
         $classes_path = realpath(getcwd() . DS . Config::$CLASS_LOCATION);
         $zip = new ZipArchive();
         $zip->open($code_zip_path, ZipArchive::CREATE);
         $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($classes_path), RecursiveIteratorIterator::LEAVES_ONLY);
         $class_location_folder_name = basename(Config::$CLASS_LOCATION);
         foreach ($files as $file) {
             if ($file->getFileName() === '.' || $file->getFileName() == '..') {
                 continue;
             }
             $path_part = explode($class_location_folder_name, $file);
             $zip->addFile($file, $class_location_folder_name . $path_part[1]);
         }
         $lib_path = realpath(getcwd() . DS . ".." . DS . 'lib');
         $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($lib_path), RecursiveIteratorIterator::LEAVES_ONLY);
         foreach ($files as $file) {
             if ($file->getFileName() === '.' || $file->getFileName() == '..') {
                 continue;
             }
             $path_part = explode('lib', $file);
             $zip->addFile($file, 'lib' . $path_part[1]);
         }
         $model_file_path = realpath(getcwd() . DS . Config::$CORE['tmp_dir_path']);
         $model_file_path .= DS . 'model.json';
         file_put_contents($model_file_path, $this->event_handlers_model->getJson(true));
         $zip->addFile($model_file_path, 'model.json');
         $zip->close();
         unlink($model_file_path);
         CodeRunnerUtil::getInstance()->publish($code_zip_path);
         GlobalState::$STATE = 'PUBLISH';
         Log::writeInfo("Successfully deployed all event handlers and timers.");
         if ($this->event_handlers_model->getCountEventHandlers() + $this->event_handlers_model->getCountTimers() > 5) {
             Log::writeWarn("The deployment will result in additional charges as it exceeds the free plan limit.");
             Log::writeInfo("See the Billing screen in Backendless Console for details.");
             Log::writeInfo("The billing screen is available at Manage > Billing.");
         }
         Log::writeInfo("CodeRunner will shutdown now.");
         $this->rrmdir(Config::$CORE['tmp_dir_path']);
         exit(0);
     } catch (CodeRunnerException $e) {
         Log::writeError($e->getMessage(), $target = 'all');
         //change to file
         Log::writeError("Code publishing failed..", $target = 'all');
         if (file_exists(Config::$CORE['tmp_dir_path'])) {
             $this->rrmdir(Config::$CORE['tmp_dir_path']);
         }
     }
 }
 private function __construct()
 {
     self::$code_runner_util = CodeRunnerUtil::getInstance();
 }
 public function publishCode($hosted = false)
 {
     $this->resetTmpFolder($hosted);
     if ($hosted) {
         $hosted_events = $this->hosted_model->getCountOfEvents();
         if ($hosted_events <= 0) {
             return;
         }
         Log::writeInfo("Deploying {$hosted_events} hosted service event to the server… ");
     } else {
         $handlers = $this->event_handlers_model->getCountEventHandlers() == 1 ? "handler" : "handlers";
         $timers = $this->event_handlers_model->getCountTimers() == 1 ? "timer" : "timers";
         if ($this->event_handlers_model->getCountEventHandlers() + $this->event_handlers_model->getCountTimers() <= 0) {
             return;
         }
         Log::writeInfo("Deploying {$this->event_handlers_model->getCountEventHandlers()} event " . $handlers . " and {$this->event_handlers_model->getCountTimers()} " . $timers . " to the server… ");
     }
     try {
         $code_zip_path = realpath(getcwd() . DS . Config::$CORE['tmp_dir_path']);
         if ($hosted) {
             $code_zip_path .= DS . 'hosted' . DS . 'code.zip';
         } else {
             $code_zip_path .= DS . 'events' . DS . 'code.zip';
         }
         $this->createArchive($code_zip_path, $hosted);
         CodeRunnerUtil::getInstance()->publish($code_zip_path, $hosted);
         if ($hosted) {
             Log::writeInfo("Successfully deployed all hosted user code.");
         } else {
             Log::writeInfo("Successfully deployed all event handlers and timers.");
         }
         if ($this->event_handlers_model->getCountEventHandlers() + $this->event_handlers_model->getCountTimers() > 5) {
             Log::writeWarn("The deployment will result in additional charges as it exceeds the free plan limit.");
             Log::writeInfo("See the Billing screen in Backendless Console for details.");
             Log::writeInfo("The billing screen is available at Manage > Billing.");
         }
     } catch (CodeRunnerException $e) {
         Log::writeError($e->getMessage(), $target = 'file');
         Log::writeError("Code publishing failed..", $target = 'all');
         $this->removeTmpFolder();
     }
 }