Example #1
0
 /**
  * Serve js and/or css files, mostly used in development
  *
  * @param string $filename
  */
 public static function serve_media_if_exists($filename, $application_path)
 {
     // we need to remove question mark and all after it
     if (strpos($filename, '?') !== false) {
         $temp = explode('?', $filename);
         $filename = $temp[0];
     }
     // generated files first
     if (strpos($filename, '/numbers/media_generated/') === 0) {
         $filename = str_replace('/numbers/media_generated/application_', '', $filename);
         $filename = $application_path . str_replace('_', '/', $filename);
     } else {
         if (strpos($filename, '/numbers/media_submodules/') === 0) {
             $temp = str_replace('/numbers/media_submodules/', '', $filename);
             $temp = str_replace('_', '/', $temp);
             $filename = './../libraries/vendor/' . $temp;
         } else {
             // we must return, do not exit !!!
             return;
         }
     }
     // check if file exists on file system
     if (!file_exists($filename)) {
         return;
     }
     // we need to know extension of a file
     $ext = pathinfo($filename, PATHINFO_EXTENSION);
     if ($ext == 'css' || $ext == 'js') {
         $new = $filename;
         $flag_scss = false;
         if (strpos($filename, '.scss.css') !== false) {
             $new = str_replace('.scss.css', '.scss', $new);
             $flag_scss = true;
         }
         if (file_exists($new)) {
             if ($ext == 'js') {
                 header('Content-Type: application/javascript');
                 echo file_get_contents($new);
             }
             if ($ext == 'css') {
                 header('Content-type: text/css');
                 if (!$flag_scss) {
                     echo file_get_contents($new);
                 } else {
                     if (application::get('dep.submodule.numbers.frontend.media.scss')) {
                         $temp = numbers_frontend_media_scss_base::serve($new);
                         if ($temp['success']) {
                             echo $temp['data'];
                         }
                     }
                 }
             }
             exit;
         }
     }
 }
 /**
  * Deploy application
  *
  * @param array $options
  * @return array
  */
 public static function deploy($options = array())
 {
     $result = array('success' => false, 'error' => array());
     do {
         if (empty($options['mode'])) {
             $options['mode'] = 'code';
         }
         $temp = rtrim(getcwd(), '/');
         $deployed_dir = $temp . '/../../deployed';
         $code_dir = $temp . '/../../code';
         // for development we handle deployment differently, just symlink to the code
         if ($options['mode'] == 'code_dev') {
             if (file_exists($deployed_dir)) {
                 shell_exec("rm -r {$deployed_dir}");
             }
             symlink($code_dir, $deployed_dir);
             $result['success'] = true;
             break;
         }
         $all_deps_dir = $temp . '/../../deployments';
         $time = time();
         $dep_id = 'build.' . $time . '.' . rand(100, 999);
         $dep_dir = $all_deps_dir . '/' . $dep_id;
         $media_dir = $dep_dir . '/public_html/numbers';
         if (mkdir($dep_dir, 0777) === false) {
             $result['error'][] = ' - unable to create new deployment directory ' . $dep_dir;
             break;
         }
         // copying code repository
         shell_exec("cp -r {$code_dir}/. {$dep_dir}");
         // removing what we do not want to have
         $dels = array('.git', 'Makefile');
         foreach ($dels as $v) {
             shell_exec("rm -r {$dep_dir}/{$v}");
         }
         // js, css, scss, files here
         $files_to_copy = [];
         $process_extensions = ['js', 'css'];
         if (application::get('dep.submodule.numbers.frontend.media.scss')) {
             $process_extensions[] = 'scss';
         }
         $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dep_dir));
         foreach ($iterator as $filename => $cur) {
             $extension = $cur->getExtension();
             if (in_array($extension, $process_extensions)) {
                 $parent_dir_name = basename(dirname($filename));
                 if (strpos($filename, '/controller/') !== false) {
                     $key = str_replace($dep_dir, '', $filename);
                     $files_to_copy[$extension][$key] = $filename;
                 }
             }
         }
         // create media directory
         $media_dir_full = $media_dir . '/media_generated';
         if (!empty($files_to_copy['js']) || !empty($files_to_copy['css']) || !empty($files_to_copy['scss'])) {
             mkdir($media_dir_full, 0777);
         }
         // coping javescript files
         if (!empty($files_to_copy['js'])) {
             foreach ($files_to_copy['js'] as $k => $v) {
                 $newname = ltrim(str_replace('/', '_', $k), '_');
                 shell_exec("cp -r {$v} {$media_dir_full}/{$newname}");
             }
         }
         // coping css files
         if (!empty($files_to_copy['css'])) {
             foreach ($files_to_copy['css'] as $k => $v) {
                 $newname = ltrim(str_replace('/', '_', $k), '_');
                 shell_exec("cp -r {$v} {$media_dir_full}/{$newname}");
             }
         }
         // coping scss files
         if (!empty($files_to_copy['scss'])) {
             foreach ($files_to_copy['scss'] as $k => $v) {
                 $newname = ltrim(str_replace('/', '_', $k), '_');
                 // processing scss files
                 $temp = numbers_frontend_media_scss_base::serve($v);
                 if ($temp['success']) {
                     file_put_contents("{$media_dir_full}/{$newname}.css", $temp['data']);
                 }
             }
         }
         // we need to load media from dependencies
         $result = system_dependencies::process_deps_all(['mode' => 'test']);
         // copying js, css & scss files
         $media_dir_submodule = $dep_dir . '/public_html';
         if (!empty($result['data']['media'])) {
             mkdir($media_dir_submodule . '/numbers/media_submodules', 0777);
             foreach ($result['data']['media'] as $k => $v) {
                 if (!in_array($k, ['js', 'css', 'scss'])) {
                     continue;
                 }
                 foreach ($v as $k2 => $v2) {
                     if (!isset($v2['origin']) || !isset($v2['destination'])) {
                         continue;
                     }
                     // js and css we just copy
                     $copy_from = $dep_dir . '/libraries/vendor' . $v2['origin'];
                     $copy_to = $media_dir_submodule . $v2['destination'];
                     if ($k == 'js' || $k == 'css') {
                         shell_exec("cp -r {$copy_from} {$copy_to}");
                     } else {
                         if ($k == 'scss' && application::get('dep.submodule.numbers.frontend.media.scss')) {
                             // we need to process scss
                             $temp = numbers_frontend_media_scss_base::serve($copy_from);
                             if ($temp['success']) {
                                 file_put_contents($copy_to, $temp['data']);
                             }
                         }
                     }
                 }
             }
         }
         // setting permissions
         shell_exec("chmod -R 0777 {$dep_dir}");
         // now we need to create a symlink
         if (file_exists($deployed_dir)) {
             shell_exec("rm -r {$deployed_dir}");
         }
         symlink($dep_dir, $deployed_dir);
         // cleanup older deployments,older than 5 days
         $iterator = new DirectoryIterator($all_deps_dir);
         foreach ($iterator as $filedir => $fileinfo) {
             if ($fileinfo->isDir()) {
                 $filename = $fileinfo->getFilename();
                 // sanity check
                 if ($filename == $dep_id) {
                     continue;
                 }
                 if (strpos($filename, 'build.') === 0) {
                     if ($time - $fileinfo->getMTime() > 259200) {
                         $temp = $fileinfo->getPathname();
                         shell_exec("rm -r {$temp}");
                     }
                 }
             }
         }
         $result['success'] = true;
     } while (0);
     return $result;
 }