diff vendor/squizlabs/php_codesniffer/scripts/build-phar.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children
line wrap: on
line diff
--- a/vendor/squizlabs/php_codesniffer/scripts/build-phar.php	Tue Jul 10 15:07:59 2018 +0100
+++ b/vendor/squizlabs/php_codesniffer/scripts/build-phar.php	Thu Feb 28 13:21:36 2019 +0000
@@ -21,108 +21,76 @@
     exit(1);
 }
 
-$cwd = getCwd();
-require_once __DIR__.'/../CodeSniffer.php';
-
-$scripts = array(
-            'phpcs',
-            'phpcbf',
-           );
+$scripts = [
+    'phpcs',
+    'phpcbf',
+];
 
 foreach ($scripts as $script) {
     echo "Building $script phar".PHP_EOL;
 
-    $pharFile = $cwd.'/'.$script.'.phar';
+    $pharName = $script.'.phar';
+    $pharFile = getcwd().'/'.$pharName;
     echo "\t=> $pharFile".PHP_EOL;
     if (file_exists($pharFile) === true) {
         echo "\t** file exists, removing **".PHP_EOL;
         unlink($pharFile);
     }
 
-    $phar = new Phar($pharFile, 0, $script.'.phar');
+    $phar = new Phar($pharFile, 0, $pharName);
 
-    echo "\t=> adding files from package.xml... ";
-    buildFromPackage($phar);
+    /*
+        Add the files.
+    */
+
+    echo "\t=> adding files... ";
+
+    $srcDir    = realpath(__DIR__.'/../src');
+    $srcDirLen = strlen($srcDir);
+
+    $rdi = new \RecursiveDirectoryIterator($srcDir, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS);
+    $di  = new \RecursiveIteratorIterator($rdi, 0, \RecursiveIteratorIterator::CATCH_GET_CHILD);
+
+    foreach ($di as $file) {
+        $filename = $file->getFilename();
+
+        // Skip hidden files.
+        if (substr($filename, 0, 1) === '.') {
+            continue;
+        }
+
+        $fullpath = $file->getPathname();
+        if (strpos($fullpath, '/Tests/') !== false) {
+            continue;
+        }
+
+        $path = 'src'.substr($fullpath, $srcDirLen);
+
+        $phar->addFromString($path, php_strip_whitespace($fullpath));
+    }
+
+    // Add autoloader.
+    $phar->addFromString('autoload.php', php_strip_whitespace(realpath(__DIR__.'/../autoload.php')));
+
+    // Add licence file.
+    $phar->addFromString('licence.txt', php_strip_whitespace(realpath(__DIR__.'/../licence.txt')));
+
     echo 'done'.PHP_EOL;
 
+    /*
+        Add the stub.
+    */
+
     echo "\t=> adding stub... ";
     $stub  = '#!/usr/bin/env php'."\n";
     $stub .= '<?php'."\n";
-    $stub .= 'Phar::mapPhar(\''.$script.'.phar\');'."\n";
-    $stub .= 'require_once "phar://'.$script.'.phar/CodeSniffer/CLI.php";'."\n";
-    $stub .= '$cli = new PHP_CodeSniffer_CLI();'."\n";
-    $stub .= '$cli->run'.$script.'();'."\n";
+    $stub .= 'Phar::mapPhar(\''.$pharName.'\');'."\n";
+    $stub .= 'require_once "phar://'.$pharName.'/autoload.php";'."\n";
+    $stub .= '$runner = new PHP_CodeSniffer\Runner();'."\n";
+    $stub .= '$exitCode = $runner->run'.$script.'();'."\n";
+    $stub .= 'exit($exitCode);'."\n";
     $stub .= '__HALT_COMPILER();';
     $phar->setStub($stub);
+
     echo 'done'.PHP_EOL;
 }//end foreach
-
-
-/**
- * Build from a package list.
- *
- * @param object $phar The Phar class.
- *
- * @return void
- */
-function buildFromPackage(&$phar)
-{
-    $packageFile = realpath(__DIR__.'/../package.xml');
-    $dom         = new DOMDocument('1.0', 'utf-8');
-    $loaded      = $dom->loadXML(file_get_contents($packageFile));
-    if ($loaded === false) {
-        echo "Unable to load package file: $packageFile".PHP_EOL;
-        exit(1);
-    }
-
-    $contents  = $dom->getElementsByTagName('contents');
-    $topLevels = $contents->item(0)->childNodes;
-    $tlLength  = $topLevels->length;
-    for ($l = 0; $l < $tlLength; $l++) {
-        $currentLevel = $topLevels->item($l);
-        buildFromNode($phar, $currentLevel, '');
-    }
-
-    // Add licence file.
-    $phar->addFile(realpath(__DIR__.'/../licence.txt'), 'licence.txt');
-    $phar['licence.txt']->compress(Phar::GZ);
-
-}//end buildFromPackage()
-
-
-/**
- * Add from a node.
- *
- * @param object $phar   The Phar class.
- * @param object $node   The node to add.
- * @param string $prefix The prefix of the structure.
- *
- * @return void
- */
-function buildFromNode(&$phar, $node, $prefix='')
-{
-    $nodeName = $node->nodeName;
-    if ($nodeName !== 'dir' && $nodeName !== 'file') {
-        // Invalid node.
-        return;
-    }
-
-    $path = $prefix.$node->getAttribute('name');
-    if ($node->getAttribute('role') === 'php' || $node->getAttribute('role') === 'data') {
-        $path = ltrim($path, '/');
-        $phar->addFile(realpath(__DIR__.'/../'.$path), $path);
-        $phar[$path]->compress(Phar::GZ);
-    }
-
-    if ($nodeName === 'dir') {
-        // Descend into the depths.
-        $path     = rtrim($path, '/').'/';
-        $children = $node->childNodes;
-        $childLn  = $children->length;
-        for ($c = 0; $c < $childLn; $c++) {
-            $child = $children->item($c);
-            buildFromNode($phar, $child, $path);
-        }
-    }
-
-}//end buildFromNode()