1#!/usr/bin/env php
2<?php
3/**
4 * Build a PHPCS phar.
5 *
6 * PHP version 5
7 *
8 * @category  PHP
9 * @package   PHP_CodeSniffer
10 * @author    Benjamin Pearson <bpearson@squiz.com.au>
11 * @author    Greg Sherwood <gsherwood@squiz.net>
12 * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
13 * @license   https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
14 * @link      http://pear.php.net/package/PHP_CodeSniffer
15 */
16
17error_reporting(E_ALL | E_STRICT);
18
19if (ini_get('phar.readonly') === '1') {
20    echo 'Unable to build, phar.readonly in php.ini is set to read only.'.PHP_EOL;
21    exit(1);
22}
23
24$cwd = getCwd();
25require_once __DIR__.'/../CodeSniffer.php';
26
27$scripts = array(
28            'phpcs',
29            'phpcbf',
30           );
31
32foreach ($scripts as $script) {
33    echo "Building $script phar".PHP_EOL;
34
35    $pharFile = $cwd.'/'.$script.'.phar';
36    echo "\t=> $pharFile".PHP_EOL;
37    if (file_exists($pharFile) === true) {
38        echo "\t** file exists, removing **".PHP_EOL;
39        unlink($pharFile);
40    }
41
42    $phar = new Phar($pharFile, 0, $script.'.phar');
43
44    echo "\t=> adding files from package.xml... ";
45    buildFromPackage($phar);
46    echo 'done'.PHP_EOL;
47
48    echo "\t=> adding stub... ";
49    $stub  = '#!/usr/bin/env php'."\n";
50    $stub .= '<?php'."\n";
51    $stub .= 'Phar::mapPhar(\''.$script.'.phar\');'."\n";
52    $stub .= 'require_once "phar://'.$script.'.phar/CodeSniffer/CLI.php";'."\n";
53    $stub .= '$cli = new PHP_CodeSniffer_CLI();'."\n";
54    $stub .= '$cli->run'.$script.'();'."\n";
55    $stub .= '__HALT_COMPILER();';
56    $phar->setStub($stub);
57    echo 'done'.PHP_EOL;
58}//end foreach
59
60
61/**
62 * Build from a package list.
63 *
64 * @param object $phar The Phar class.
65 *
66 * @return void
67 */
68function buildFromPackage(&$phar)
69{
70    $packageFile = realpath(__DIR__.'/../package.xml');
71    $dom         = new DOMDocument('1.0', 'utf-8');
72    $loaded      = $dom->loadXML(file_get_contents($packageFile));
73    if ($loaded === false) {
74        echo "Unable to load package file: $packageFile".PHP_EOL;
75        exit(1);
76    }
77
78    $contents  = $dom->getElementsByTagName('contents');
79    $topLevels = $contents->item(0)->childNodes;
80    $tlLength  = $topLevels->length;
81    for ($l = 0; $l < $tlLength; $l++) {
82        $currentLevel = $topLevels->item($l);
83        buildFromNode($phar, $currentLevel, '');
84    }
85
86    // Add licence file.
87    $phar->addFile(realpath(__DIR__.'/../licence.txt'), 'licence.txt');
88    $phar['licence.txt']->compress(Phar::GZ);
89
90}//end buildFromPackage()
91
92
93/**
94 * Add from a node.
95 *
96 * @param object $phar   The Phar class.
97 * @param object $node   The node to add.
98 * @param string $prefix The prefix of the structure.
99 *
100 * @return void
101 */
102function buildFromNode(&$phar, $node, $prefix='')
103{
104    $nodeName = $node->nodeName;
105    if ($nodeName !== 'dir' && $nodeName !== 'file') {
106        // Invalid node.
107        return;
108    }
109
110    $path = $prefix.$node->getAttribute('name');
111    if ($node->getAttribute('role') === 'php' || $node->getAttribute('role') === 'data') {
112        $path = ltrim($path, '/');
113        $phar->addFile(realpath(__DIR__.'/../'.$path), $path);
114        $phar[$path]->compress(Phar::GZ);
115    }
116
117    if ($nodeName === 'dir') {
118        // Descend into the depths.
119        $path     = rtrim($path, '/').'/';
120        $children = $node->childNodes;
121        $childLn  = $children->length;
122        for ($c = 0; $c < $childLn; $c++) {
123            $child = $children->item($c);
124            buildFromNode($phar, $child, $path);
125        }
126    }
127
128}//end buildFromNode()
129