Skip to content

Commit

Permalink
- Changed project to a PHP package
Browse files Browse the repository at this point in the history
- Added support to parse for interface and trait definitions and partly enums (enum support is not finished)
- Adapted code to never PHP version
- Updated readme
- Added some examples
  • Loading branch information
muratpurc committed Jan 29, 2023
1 parent d9b69fb commit ce0594f
Show file tree
Hide file tree
Showing 20 changed files with 1,239 additions and 848 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
/vendor
/composer.lock
58 changes: 0 additions & 58 deletions autoloader.php

This file was deleted.

24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "purc/autoloader-class-map",
"type": "library",
"description": "Autoloader class map generator",
"keywords": [
"php",
"autoload",
"autoloader",
"classmap",
"generator"
],
"authors": [
{
"name": "Murat Purç",
"email": "[email protected]",
"homepage": "https://www.purc.de"
}
],
"license": ["GPL-2.0-or-later"],
"autoload": {
"psr-4": {
"Purc\\AutoloaderClassMap\\": "src/"
}
}}
76 changes: 37 additions & 39 deletions example.php
Original file line number Diff line number Diff line change
@@ -1,83 +1,81 @@
<?php
declare(strict_types=1);

/**
* Example usage for autoloader class map file generator.
*
* Parses full PEAR directory and creates a class map file!
*
* Usage:
* ------
* 1. Modifiy settings to youre requriements
* 2. call this cript from command line as follows:
* 1. Modify settings to your requirements
* 2. Call this script from command line as follows:
* $ php example.php
* 3. Check created class map file
*
* @category Development
* @package mpAutoloaderClassMap
* @author Murat Purc <[email protected]>
* @copyright Copyright (c) 2009-2011 Murat Purc (http://www.purc.de)
* @license http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
* @version $Id$
* @category Development
* @package AutoloaderClassMap
* @author Murat Purç <[email protected]>
* @copyright Murat Purç (http://www.purc.de)
* @license http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
*/


################################################################################
##### Initialization/Settings

// create a page context class, better than spamming global scope
$context = new stdClass();
require_once __DIR__ . '/vendor/autoload.php';

$classMapPath = __DIR__ . '/classmap.configuration.php';

// current path
$context->currentPath = str_replace('\\', '/', realpath(dirname(__FILE__) . '/')) . '/';
// Set the environment variable, PURC_AUTOLOADER_CLASS_MAP_FILE, you can set the global variable alternatively, e.g.
// $GLOBALS['PURC_AUTOLOADER_CLASS_MAP_FILE'] = $classMapPath;
putenv('PURC_AUTOLOADER_CLASS_MAP_FILE="' . $classMapPath . '"');

// the destination file where the class map configuration should be written in
$context->destinationFile = $context->currentPath . '/classmap.configuration.php';
$destinationFile = $classMapPath;

// list of paths from where all class/interface names should be found
// NOTE: Path depends on used environment and should be adapted
$context->pathsToParse = array(
'/path/to/my/project'
);
$pathsToParse = [
__DIR__ .'/_data/class-map-generator-fixtures'
];

// list to collect class maps
$context->classMapList = array();
$classMapList = [];

// class file finder options
$context->options = array(
$options = [
// exclude following folder names
'excludeDirs' => array('temp', 'session', 'docs', 'tests'),
'excludeDirs' => ['temp', 'session', 'docs', 'tests'],
// no specific file exclusion
'excludeFiles' => array(),
'excludeFiles' => [],
// parse all files with '.php' extension
'extensionsToParse' => '.php',
// disbale debugging
'extensionsToParse' => ['.php'],
// disable debugging
'enableDebug' => false,
);
];


################################################################################
##### Process

// include required classes
include_once($context->currentPath . 'lib/mpClassTypeFinder.php');
include_once($context->currentPath . 'lib/mpClassMapFileCreator.php');

// collect all found class/interface names with their paths
$context->classTypeFinder = new mpClassTypeFinder($context->options);
foreach ($context->pathsToParse as $pos => $dir) {
$classMap = $context->classTypeFinder->findInDir(new SplFileInfo($dir), true);
// Collect all found class/interface names with their paths
$classTypeFinder = new \Purc\AutoloaderClassMap\ClassTypeFinder($options);
foreach ($pathsToParse as $pos => $dir) {
$classMap = $classTypeFinder->findInDir(new SplFileInfo($dir), true);
if ($classMap) {
$context->classMapList = array_merge($context->classMapList, $classMap);
$classMapList = array_merge($classMapList, $classMap);
}
}
ksort($classMapList);

// uncomment following line to get some debug messages
#echo $context->classTypeFinder->getFormattedDebugMessages();

// Uncomment following line to get some debug messages
#echo $classTypeFinder->getFormattedDebugMessages();

// write the class map configuration
$context->classMapCreator = new mpClassMapFileCreator();
$context->classMapCreator->create($context->classMapList, $context->destinationFile);

// Write the class map configuration
$classMapCreator = new \Purc\AutoloaderClassMap\FileCreator();
$classMapCreator->create($classMapList, $destinationFile);

// cleanup
unset($context);
78 changes: 78 additions & 0 deletions examples/class_map_generation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
declare(strict_types=1);

/**
* Example usage for autoloader class map file generator.
*
* Parses defined directory/directories and creates a class map file!
*
* Usage:
* ------
* 1. Modify settings to your requirements
* 2. Call this script from command line as follows:
* $ php class_map_generation.php
* 3. Check created class map file
*
* @category Development
* @package AutoloaderClassMap
* @author Murat Purç <[email protected]>
* @copyright Murat Purç (http://www.purc.de)
* @license http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
*/


// ##### Initialization/Settings

require_once __DIR__ . '/../vendor/autoload.php';

// Path and name of class map file to store the class map definitions.
// Note: PHP needs write-permissions for the folder!
$classMapPath = __DIR__ . '/classmap.configuration.php';

// Set the environment variable, PURC_AUTOLOADER_CLASS_MAP_FILE, you can set the global variable alternatively, e.g.
// $GLOBALS['PURC_AUTOLOADER_CLASS_MAP_FILE'] = $classMapPath;
putenv('PURC_AUTOLOADER_CLASS_MAP_FILE="' . $classMapPath . '"');

// The destination file where the class map configuration should be written in
$destinationFile = $classMapPath;

// List of paths from where all class/interface names should be found
// NOTE: Path depends on used environment and should be adapted
$pathsToParse = [
__DIR__ . '/files'
];

// List to collect class maps
$classMapList = [];

// Class file finder options
$options = [
// exclude following folder names
'excludeDirs' => ['temp', 'session', 'docs', 'tests'],
// no specific file exclusion
'excludeFiles' => [],
// parse all files with '.php' extension
'extensionsToParse' => ['.php'],
// disable debugging
'enableDebug' => false,
];


// ##### Process

// Collect all found class/interface names with their paths
$classTypeFinder = new \Purc\AutoloaderClassMap\ClassTypeFinder($options);
foreach ($pathsToParse as $pos => $dir) {
$classMap = $classTypeFinder->findInDir(new SplFileInfo($dir));
if ($classMap) {
$classMapList = array_merge($classMapList, $classMap);
}
}
ksort($classMapList);

// Uncomment following line to get some debug messages
#echo $classTypeFinder->getFormattedDebugMessages();

// Write the class map configuration
$classMapCreator = new \Purc\AutoloaderClassMap\FileCreator();
$classMapCreator->create($classMapList, $destinationFile);
21 changes: 21 additions & 0 deletions examples/files/namespace-and-use.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Foo\Bar;

use Exception;

interface NsAndUseInterface
{
}

trait NsAndUseTrait
{
}

class NsAndUseClass
{
public function __construct()
{
throw new Exception('Instantiation not allowed!');
}
}
11 changes: 11 additions & 0 deletions examples/files/namespace-root.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace {

interface RootNsInterface {}

trait RootNsTrait {}

class RootNsClass {}

}
11 changes: 11 additions & 0 deletions examples/files/namespace-whitespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Foo \ Bar {

interface WhitespaceNsInterface {}

trait WhitespaceNsTrait {}

class WhitespaceNsClass {}

}
16 changes: 16 additions & 0 deletions examples/files/namespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace
/**/
Foo\Bar
/**/
;

interface NsInterface {
}

trait NsTrait {
}

class NsClass {
}
15 changes: 15 additions & 0 deletions examples/files/no-namespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

interface NoNsInterface {}

trait NoNsTrait {}

class NoNsClass {

public function getAnonInstance()
{
return new class implements NoNsInterface {
};
}

}
29 changes: 29 additions & 0 deletions examples/find_in_file.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

/**
* Example for finding tokens in a single file.
*
* Usage:
* ------
* 1. Modify path to file (see `$file`) to your requirements
* 2. Call this script from command line as follows:
* $ php find_in_file.php
* 3. See output
*
* @category Development
* @package AutoloaderClassMap
* @author Murat Purç <[email protected]>
* @copyright Murat Purç (http://www.purc.de)
* @license http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
*/

require_once __DIR__ . '/../vendor/autoload.php';

$file = __DIR__ . '/files/namespace.php';

$classTypeFinder = new \Purc\AutoloaderClassMap\ClassTypeFinder();
$result = $classTypeFinder->findInFile(
new SplFileInfo($file)
);
print_r($result);
Loading

0 comments on commit ce0594f

Please sign in to comment.