aboutsummaryrefslogtreecommitdiff
path: root/gnu/packages/aux-files/findclass.php
blob: d0b250c8e12ddf5f160dd723de5b9dd63cb03e55 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/**
 * The content of this file is copied from composer's src/Composer/Autoload/ClassMapGenerator.php
 * the findClasses method was extracted, to prevent using any dependency.
 *
 * Composer (and thus this file) is distributed under the expat license, and
 * ClassMapGenerator.php also contains this notice:
 *
 *   This file is part of Composer.
 *
 *   (c) Nils Adermann <naderman@naderman.de>
 *       Jordi Boggiano <j.boggiano@seld.be>
 *
 *   For the full copyright and license information, please view the LICENSE
 *   file that was distributed with this source code.
 *
 *   This file is copied from the Symfony package.
 *
 *   (c) Fabien Potencier <fabien@symfony.com>
 * 
 * To the extent to wich it makes sense, as the author of the extract:
 * Copyright © 2020 Julien Lepiller <julien@lepiller.eu>
 */

/**
 * Extract the classes in the given file
 *
 * @param  string            $path The file to check
 * @throws \RuntimeException
 * @return array             The found classes
 */
function findClasses($path)
{
    $extraTypes = PHP_VERSION_ID < 50400 ? '' : '|trait';
    if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.3', '>=')) {
        $extraTypes .= '|enum';
    }
    // Use @ here instead of Silencer to actively suppress 'unhelpful' output
    // @link https://github.com/composer/composer/pull/4886
    $contents = @php_strip_whitespace($path);
    if (!$contents) {
        if (!file_exists($path)) {
            $message = 'File at "%s" does not exist, check your classmap definitions';
        } elseif (!is_readable($path)) {
            $message = 'File at "%s" is not readable, check its permissions';
        } elseif ('' === trim(file_get_contents($path))) {
            // The input file was really empty and thus contains no classes
            return array();
        } else {
            $message = 'File at "%s" could not be parsed as PHP, it may be binary or corrupted';
        }
        $error = error_get_last();
        if (isset($error['message'])) {
            $message .= PHP_EOL . 'The following message may be helpful:' . PHP_EOL . $error['message'];
        }
        throw new \RuntimeException(sprintf($message, $path));
    }
    // return early if there is no chance of matching anything in this file
    if (!preg_match('{\b(?:class|interface'.$extraTypes.')\s}i', $contents)) {
        return array();
    }
    // strip heredocs/nowdocs
    $contents = preg_replace('{<<<[ \t]*([\'"]?)(\w+)\\1(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)(?:\s*)\\2(?=\s+|[;,.)])}s', 'null', $contents);
    // strip strings
    $contents = preg_replace('{"[^"\\\\]*+(\\\\.[^"\\\\]*+)*+"|\'[^\'\\\\]*+(\\\\.[^\'\\\\]*+)*+\'}s', 'null', $contents);
    // strip leading non-php code if needed
    if (substr($contents, 0, 2) !== '<?') {
        $contents = preg_replace('{^.+?<\?}s', '<?', $contents, 1, $replacements);
        if ($replacements === 0) {
            return array();
        }
    }
    // strip non-php blocks in the file
    $contents = preg_replace('{\?>(?:[^<]++|<(?!\?))*+<\?}s', '?><?', $contents);
    // strip trailing non-php code if needed
    $pos = strrpos($contents, '?>');
    if (false !== $pos && false === strpos(substr($contents, $pos), '<?')) {
        $contents = substr($contents, 0, $pos);
    }
    // strip comments if short open tags are in the file
    if (preg_match('{(<\?)(?!(php|hh))}i', $contents)) {
        $contents = preg_replace('{//.* | /\*(?:[^*]++|\*(?!/))*\*/}x', '', $contents);
    }
    preg_match_all('{
        (?:
             \b(?<![\$:>])(?P<type>class|interface'.$extraTypes.') \s++ (?P<name>[a-zA-Z_\x7f-\xff:][a-zA-Z0-9_\x7f-\xff:\-]*+)
           | \b(?<![\$:>])(?P<ns>namespace) (?P<nsname>\s++[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\s*+\\\\\s*+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+)? \s*+ [\{;]
        )
    }ix', $contents, $matches);
    $classes = array();
    $namespace = '';
    for ($i = 0, $len = count($matches['type']); $i < $len; $i++) {
        if (!empty($matches['ns'][$i])) {
            $namespace = str_replace(array(' ', "\t", "\r", "\n"), '', $matches['nsname'][$i]) . '\\';
        } else {
            $name = $matches['name'][$i];
            // skip anon classes extending/implementing
            if ($name === 'extends' || $name === 'implements') {
                continue;
            }
            if ($name[0] === ':') {
                // This is an XHP class, https://github.com/facebook/xhp
                $name = 'xhp'.substr(str_replace(array('-', ':'), array('_', '__'), $name), 1);
            } elseif ($matches['type'][$i] === 'enum') {
                // In Hack, something like:
                //   enum Foo: int { HERP = '123'; }
                // The regex above captures the colon, which isn't part of
                // the class name.
                $name = rtrim($name, ':');
            }
            $classes[] = ltrim($namespace . $name, '\\');
        }
    }
    return $classes;
}

$options = getopt('i:f:', []);
$file = $options["f"];
$input = $options["i"];

$classes = findClasses($file);
foreach($classes as $class) {
  echo '$classmap[\''.$class.'\'] = \''.$input.'/'.$file.'\';';
  echo "\n";
}
cripts/substitute.scm, guix/store.scm, guix/store/deduplication.scm, guix/tests.scm, tests/base32.scm, tests/builders.scm, tests/challenge.scm, tests/cpan.scm, tests/crate.scm, tests/derivations.scm, tests/gem.scm, tests/nar.scm, tests/opam.scm, tests/pki.scm, tests/publish.scm, tests/pypi.scm, tests/store-deduplication.scm, tests/store.scm, tests/substitute.scm: Adjust imports. * gnu/system/vm.scm: Likewise. (guile-sqlite3&co): Rename to... (gcrypt-sqlite3&co): ... this. Add GUILE-GCRYPT. (expression->derivation-in-linux-vm)[config]: Remove. (iso9660-image)[config]: Remove. (qemu-image)[config]: Remove. (system-docker-image)[config]: Remove. * guix/scripts/pack.scm: Adjust imports. (guile-sqlite3&co): Rename to... (gcrypt-sqlite3&co): ... this. Add GUILE-GCRYPT. (self-contained-tarball)[build]: Call 'make-config.scm' without #:libgcrypt argument. (squashfs-image)[libgcrypt]: Remove. [build]: Call 'make-config.scm' without #:libgcrypt. (docker-image)[config, json]: Remove. [build]: Add GUILE-GCRYPT to the extensions Remove (guix config) from the imported modules. * guix/self.scm (specification->package): Remove "libgcrypt", add "guile-gcrypt". (compiled-guix): Remove #:libgcrypt. [guile-gcrypt]: New variable. [dependencies]: Add it. [*core-modules*]: Remove #:libgcrypt from 'make-config.scm' call. Add #:extensions. [*config*]: Remove #:libgcrypt from 'make-config.scm' call. (%dependency-variables): Remove %libgcrypt. (make-config.scm): Remove #:libgcrypt. * build-aux/build-self.scm (guile-gcrypt): New variable. (make-config.scm): Remove #:libgcrypt. (build-program)[fake-gcrypt-hash]: New variable. Add (gcrypt hash) to the imported modules. Adjust load path assignments. * gnu/packages/package-management.scm (guix)[propagated-inputs]: Add GUILE-GCRYPT. [arguments]: In 'wrap-program' phase, add GUILE-GCRYPT to the search path. Ludovic Courtès 2018-07-24import: PyPI: Update redirected URL....* guix/import/pypi.scm (guix-package->pypi-name, pypi->guix-package): Update docstrings. (pypi-package?): Test for pypi.org, too. (pypi-fetch): s/pypi.python.org/pypi.org/ * tests/pypi.scm ("guix-package->pypi-name, new URL style", "pypi->guix-package", "pypi->guix-package, wheels"): Likewise. Marius Bakke 2017-06-18import: pypi: Adjust URIs in tests....This is a followup to d1e7ca2df8c0e0dd601079c77f67ba6828cec08a. * tests/pypi.scm ("pypi->guix-package") ("pypi->guix-package, wheels"): Expect (pypi-uri "foo" version) for the 'uri' field. Ludovic Courtès 2017-02-13tests: Adjust for 'http-fetch' change in (guix import json)....This is a followup to commit 81e0bc1834490a1a8092c75a0733b15c2b407285. * tests/cpan.scm ("cpan->guix-package"): Add a 'rest' argument to the lambda that mocks 'http-fetch'. * tests/crate.scm ("crate->guix-package"): Likewise. * tests/gem.scm ("gem->guix-package"): Likewise. * tests/pypi.scm ("pypi->guix-package"): Likewise. ("pypi->guix-package, wheels"): Likewise. Ludovic Courtès 2017-02-08import: pypi: Correctly handle multiple-URL origins....Fixes <https://bugs.gnu.org/25635>. Reported by Sergei Trofimovich <slyfox@inbox.ru>. * guix/import/pypi.scm (guix-package->pypi-name)[url->pypi-name]: New procedure. Rewrite body to match lists in addition to strings. * tests/pypi.scm ("guix-package->pypi-name, several URLs"): New test. Ludovic Courtès 2017-01-28tests: Adjust pypi test to recent importer change....This is a followup to 2f977d92d3ae517788d3dee98f63680ca149aa1a. * tests/pypi.scm ("pypi->guix-package"): Don't expect 'python-setuptools' in 'propagated-inputs'. ("pypi->guix-package, wheels"): Likewise. Signed-off-by: Ludovic Courtès <ludo@gnu.org> Carlo Zancanaro 2016-12-18tests: Mock up http-fetch in import test....This is a follow-up to commit 63773200d7ac68fcaee6efd9ffe8ea7aa3fafa38. * tests/pypi.scm ("pypi->guix-package, wheels"): Add mock definition of "http-fetch". Ricardo Wurmus 2016-12-18tests: Mock up http-fetch in import tests....This is a follow-up to commit 63773200d7ac68fcaee6efd9ffe8ea7aa3fafa38. * tests/gem.scm ("gem->guix-package"): Replace mock definition of "url-fetch" with "http-fetch". * tests/pypi.scm ("pypi->guix-package"): Add mock definition of "http-fetch". Ricardo Wurmus 2016-10-27tests: Adjust pypi test to recent importer change....This is a followup to b5c347ad3d83ee580c111bd14c80b469b0dcb294. * tests/pypi.scm ("pypi->guix-package"): Expect 'propagated-inputs', not 'inputs'. ("pypi->guix-package, wheels"): Likewise. Ludovic Courtès 2016-08-28import: Importers return prefixed licenses....* guix/import/utils.scm (define-module): Import licenses with license: prefix. (string->licenses): Use prefixed licenses. (license->symbol): Return symbols with the prefix license:. * guix/tests/pypi.scm (pypi->guix-package): Update test cases. * guix/tests/gem.scm (gem->guix-package): Update test case. David Craven 2016-07-26import: pypi: Correctly handle new-style URLs....Fixes <http://bugs.gnu.org/23997>. * guix/import/pypi.scm (guix-package->pypi-name): Rewrite using 'basename' and 'hyphen-package-name->name+version'. * tests/pypi.scm ("guix-package->pypi-name, old URL style") ("guix-package->pypi-name, new URL style"): New tests. Ludovic Courtès 2016-06-14import: pypi: read requirements from wheels....* doc/guix.tex (Invoking guix import): Mention that the pypi importer works better with "unzip". * guix/import/pypi.scm (latest-wheel-release, wheel-url->extracted-directory): New procedures. * tests/pypi.scm (("pypi->guix-package, wheels"): New test. Cyril Roelandt 2016-04-03build: Add a Guile custom test driver using SRFI-64....Before that '.log' files for scheme tests were fragmented and not included in test-suite.log. This unifies the semantics of SRFI-64 API with Automake test suite. * build-aux/test-driver.scm: New file. * Makefile.am (SCM_LOG_DRIVER, AM_SCM_LOG_DRIVER_FLAGS): New variables. (SCM_LOG_COMPILER, AM_SCM_LOG_FLAGS): Delete variables. (AM_TESTS_ENVIRONMENT): Set GUILE_AUTO_COMPILE to 0. * test-env.in: Silence guix-daemon. * doc/guix.texi (Running the Test Suite): Describe how to display the detailed results. Bug reports require only 'test-suite.log' file. * tests/base32.scm, tests/build-utils.scm, tests/builders.scm, tests/challenge.scm, tests/cpan.scm, tests/cpio.scm, tests/cran.scm, tests/cve.scm, tests/derivations.scm, tests/elpa.scm, tests/file-systems.scm, tests/gem.scm, tests/gexp.scm, tests/gnu-maintenance.scm, tests/grafts.scm, tests/graph.scm, tests/gremlin.scm, tests/hackage.scm, tests/hash.scm, tests/import-utils.scm, tests/lint.scm, tests/monads.scm, tests/nar.scm, tests/packages.scm, tests/pk-crypto.scm, tests/pki.scm, tests/profiles.scm, tests/publish.scm, tests/pypi.scm, tests/records.scm, tests/scripts-build.scm, tests/scripts.scm, tests/services.scm, tests/sets.scm, tests/size.scm, tests/snix.scm, tests/store.scm, tests/substitute.scm, tests/syscalls.scm, tests/system.scm, tests/ui.scm, tests/union.scm, tests/upstream.scm, tests/utils.scm: Don't exit at the end of test groups. * tests/containers.scm: Likewise. Use 'test-skip' instead of exiting with error code 77. Mathieu Lirzin 2016-03-19import: pypi: Emit 'pypi-uri' only when it yields the right URL....Fixes <http://bugs.gnu.org/23062>. Reported by Danny Milosavljevic <dannym@scratchpost.org>. * guix/import/pypi.scm (make-pypi-sexp): Check whether 'pypi-uri' returns SOURCE-URL and fall back to the full URL otherwise. * tests/pypi.scm ("pypi->guix-package"): Adjust expected URI accordingly. Co-authored-by: Danny Milosavljevic <dannym@scratchpost.org> Ludovic Courtès 2015-11-03import: pypi: Use "pypi-uri" instead of building the URL manually....* guix/import/pypi.scm (make-pypi-sexp): Use "pypi-uri". * tests/pypi.scm: Update the tests accordingly. Cyril Roelandt 2015-06-24import: pypi: Detect inputs....* guix/import/pypi.scm (python->package-name, maybe-inputs, compute-inputs, guess-requirements): New procedures. * guix/import/pypi.scm (guix-hash-url): Now takes a filename instead of an URL as input. * guix/import/pypi.scm (make-pypi-sexp): Now tries to generate the inputs automagically. * tests/pypi.scm: Update the test. Cyril Roelandt