]> _ Git - cubist_util.git/commitdiff
wip #6968 @3
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 20 Jun 2024 06:42:48 +0000 (08:42 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 20 Jun 2024 06:42:48 +0000 (08:42 +0200)
src/CommandLine.php
src/Files/Files.php
src/Files/VirtualDirectory.php
src/Html.php

index e27f668880ce7bda6807a320e7656b57b274a998..ebbf377342cc8faa990976759951f2ee4dbe173b 100644 (file)
@@ -122,15 +122,28 @@ class CommandLine
 
     public function execute($fonction = 'shell_exec')
     {
+
         $startTime = microtime(true);
         $this->_preExecute();
         if ($fonction instanceof SSH2) {
             $this->makeCommande();
+            if (function_exists('start_measure')) {
+                start_measure('Exec CLI : ' . $this->commande);
+            }
             $o = $fonction->exec($this->commande);
+            if (function_exists('stop_measure')) {
+                stop_measure('Exec CLI : ' . $this->commande);
+            }
             file_put_contents($this->output, $o['output'] . "\n\n---\n\n" . $o['error']);
         } else if (null === $this->ssh) {
             $this->makeCommande($this->output, $this->error);
+            if (function_exists('start_measure')) {
+                start_measure('Exec CLI : ' . $this->commande);
+            }
             $fonction($this->commande);
+            if (function_exists('stop_measure')) {
+                stop_measure('Exec CLI : ' . $this->commande);
+            }
         } else {
             $this->makeCommande();
 
@@ -146,8 +159,13 @@ class CommandLine
             }
 
             $this->commande = $c;
-
+            if (function_exists('start_measure')) {
+                start_measure('Exec CLI : ' . $this->commande);
+            }
             $fonction($c);
+            if (function_exists('stop_measure')) {
+                stop_measure('Exec CLI : ' . $this->commande);
+            }
         }
         $endTime = microtime(true);
         $this->execTime = $endTime - $startTime;
@@ -177,13 +195,13 @@ class CommandLine
         $commande = $this->program;
         $commandes = array();
         foreach ($this->args as $arg) {
-            if (strlen($arg[0]) == 1) {
+            if (null === $arg[0]) {
+                $commande .= ' ' . $arg[1];
+            } else if (strlen($arg[0]) == 1) {
                 $commande .= ' -' . $arg[0];
                 if (null !== $arg[1]) {
                     $commande .= ' ' . $arg[1];
                 }
-            } elseif (null === $arg[0]) {
-                $commande .= ' ' . $arg[1];
             } else {
                 if (substr($arg[0], 0, 1) == '-') {
                     $commande .= ' ' . $arg[0];
index 6f724cd1e9aed73366ebee6dde7260271a8c723c..a7a0ae4a999921cf7e781b4a3b9c39d99a45c918 100644 (file)
@@ -74,14 +74,31 @@ class Files
      * @param array $exclude
      * @return array|\RecursiveIteratorIterator
      */
-    public static function getRecursiveDirectoryIterator($path, $mode = \RecursiveIteratorIterator::SELF_FIRST, $exclude = array())
+    public static function getRecursiveDirectoryIterator($path, $mode = \RecursiveIteratorIterator::SELF_FIRST, $exclude = [])
     {
         $path = realpath($path);
         if (!file_exists($path)) {
-            return array();
+            return [];
+        }
+        return new \RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, $exclude), $mode);
+    }
+
+    public static function listFilesOfDirectory($path, &$res = [])
+    {
+        $files = scandir($path);
+
+        foreach ($files as $key => $value) {
+            $path = realpath($path . DIRECTORY_SEPARATOR . $value);
+            if (!is_dir($path)) {
+                $res[] = $path;
+            } else if ($value != "." && $value != "..") {
+                static::listFilesOfDirectory($path, $res);
+                $res[] = $path;
+            }
         }
-        $res = new \RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, $exclude), $mode);
+
         return $res;
+
     }
 
     /**
index ac2579d4272caf34e5e34bda21342e6f9915c73d..9b8157366a11c5dae442c5ab1ca9be7052f5b550 100644 (file)
@@ -101,7 +101,8 @@ class VirtualDirectory
                 Files::mkdir($from);
             }
         }
-        $this->_directories[$this->path($to)] = $from;
+        $to = $this->path($to);
+        $this->_directories[$to] = $from;
         return $this;
     }
 
@@ -129,9 +130,7 @@ class VirtualDirectory
             Files::mkdir($dir);
         }
 
-
         foreach ($this->_copy as $to => $from) {
-
             if ($delete) {
                 $existing[$to] = true;
             }
@@ -144,8 +143,9 @@ class VirtualDirectory
             if (!$this->compare($from, $to)) {
                 $to_esc = escapeshellarg($to);
                 $from_esc = escapeshellarg($from);
-                `cp -p $from_esc $to_esc`;
-                $this->log('Copy file ' . $to);
+                $cpcmd = "cp -p $from_esc $to_esc";
+                `$cpcmd`;
+                $this->log('Copy file ' . $to . ' : ' . $cpcmd);
                 if (!file_exists($to)) {
                     $this->throwError(sprintf('Failed to copy %s to %s', $from, $to));
                 }
@@ -183,20 +183,30 @@ class VirtualDirectory
     protected function _addDirectory($from, $to)
     {
         $from = realpath($from);
-        $files = Files::getRecursiveDirectoryIterator($from);
+        Files::listFilesOfDirectory($from, $files);
 
-        foreach ($files as $file) {
-            /* @var $file SplFileInfo */
-            if ($file->isDir()) {
-                continue;
-            }
-            $path = $file->getRealPath();
+
+        if (!count($files)) {
+            dddd(`ls -l $from`);
+        }
+
+        $relativeTo = $this->relativePath($to);
+
+        $total = 0;
+        $parsed = 0;
+
+        foreach ($files as $path) {
+            $total++;
             $dest = str_replace($from, '', $path);
             if (!$path) {
+                $this->log('Skip file ' . $path . ' because no real path');
                 continue;
             }
-            $this->copy($path, $this->relativePath($to) . '/' . ltrim($dest, '/'), true);
+            $this->copy($path, $relativeTo . '/' . ltrim($dest, '/'), true);
+            $parsed++;
         }
+
+        $this->log('parsed directory ' . $from . ' (' . $to . ') (' . $parsed . '/' . $total . ')');
     }
 
     protected function _delete($existing = array())
index c7bc67c3772258f0f9f05cd450615010819689f3..f231faa3da20e911b0fca58b5e769cd433240091 100644 (file)
@@ -1,7 +1,9 @@
 <?php
 
 namespace Cubist\Util;
-use MicrodataDOM\DOMDocument;
+
+use DOMDocument;
+use DOMNode;
 
 class Html
 {
@@ -29,4 +31,34 @@ class Html
         return $dom;
     }
 
+    protected static function _getTextNode(DOMNode $domNode): array
+    {
+        $res = [];
+        foreach ($domNode->childNodes as $node) {
+            if ($node instanceof \DOMText) {
+                $res[] = $node->nodeValue;
+            }
+            if ($node->hasChildNodes()) {
+                $res = array_merge($res, static::_getTextNode($node));
+            }
+        }
+        return $res;
+    }
+
+    public static function getTextNodes($string)
+    {
+        if (!$string) {
+            return [];
+        }
+        try {
+            $doc = new DOMDocument('1.0', 'utf-8');
+            $doc->loadHTML($string);
+
+            $res= static::_getTextNode($doc);
+            return $res;
+        } catch (\Exception $e) {
+            return [$string];
+        }
+    }
+
 }