2017 © Pedro PelĂĄez
 

library classtools

Find, extract and process classes from file system

image

hanneskod/classtools

Find, extract and process classes from file system

  • Wednesday, December 13, 2017
  • by hanneskod
  • Repository
  • 3 Watchers
  • 35 Stars
  • 108,790 Installations
  • PHP
  • 21 Dependents
  • 0 Suggesters
  • 5 Forks
  • 0 Open issues
  • 11 Versions
  • 10 % Grown

The README.md

hanneskod/classtools

Packagist Version Build Status Quality Score, (*1)

Find, extract and process classes from the file system., (*2)

Installation

Install using composer. Exists as hanneskod/classtools in the packagist repository. From the command line use:, (*3)

composer require hanneskod/classtools

Using the iterator

ClassIterator consumes a symfony finder and scans files for php classes, interfaces and traits., (*4)

Access the class map

getClassMap() returns a map of class names to SplFileInfo objects., (*5)

$finder = new Symfony\Component\Finder\Finder;
$iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src'));

// Print the file names of classes, interfaces and traits in 'src'
foreach ($iter->getClassMap() as $classname => $splFileInfo) {
    echo $classname.': '.$splFileInfo->getRealPath();
}

Find syntax errors

Source files containing syntax errors can not be parsed and hence no information on contained classes can be retrieved. Use getErrors() to read the list of encountered errors., (*6)

$finder = new Symfony\Component\Finder\Finder;
$iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src'));

print_r($iter->getErrors());

Iterate over ReflectionClass objects

ClassIterator is also a Traversable, that on iteration yields class names as keys and ReflectionClass objects as values., (*7)

Note that to use reflection the classes found in filesystem must be included in the environment. Enable autoloading to dynamically load classes from a ClassIterator., (*8)

$finder = new Symfony\Component\Finder\Finder();
$iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src'));

// Enable reflection by autoloading found classes
$iter->enableAutoloading();

// Print all classes, interfaces and traits in 'src'
foreach ($iter as $class) {
    echo $class->getName();
}

Filter based on class properties

ClassIterator is filterable and filters are chainable., (*9)

$finder = new Symfony\Component\Finder\Finder();
$iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src'));
$iter->enableAutoloading();

// Print all Filter types (including the interface itself)
foreach ($iter->type('hanneskod\classtools\Iterator\Filter') as $class) {
    echo $class->getName();
}

// Print definitions in the Iterator namespace whose name contains 'Class'
foreach ($iter->inNamespace('hanneskod\classtools\Iterator\Filter')->name('/type/i') as $class) {
    echo $class->getName();
}

// Print implementations of the Filter interface
foreach ($iter->type('hanneskod\classtools\Iterator\Filter')->where('isInstantiable') as $class) {
    echo $class->getName();
}

Negate filters

Filters can also be negated by wrapping them in not() method calls., (*10)

$finder = new Symfony\Component\Finder\Finder();
$iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src'));
$iter->enableAutoloading();

// Print all classes, interfaces and traits NOT instantiable
foreach ($iter->not($iter->where('isInstantiable')) as $class) {
    echo $class->getName();
}

Transforming classes

Found class, interface and trait definitions can be transformed and written to a single file., (*11)

$finder = new Symfony\Component\Finder\Finder();
$iter = new hanneskod\classtools\Iterator\ClassIterator($finder->in('src'));
$iter->enableAutoloading();

// Print all found definitions in one snippet
echo $iter->minimize();

// The same can be done using
echo $iter->transform(new hanneskod\classtools\Transformer\MinimizingWriter);

Using the transformer

Wrap code in namespace

$reader = new Reader("<?php class Bar {}");
$writer = new Writer;
$writer->apply(new Action\NamespaceWrapper('Foo'));

// Outputs class Bar wrapped in namespace Foo
echo $writer->write($reader->read('Bar'));

The Versions

13/12 2017

dev-master

9999999-dev https://github.com/hanneskod/classtools

Find, extract and process classes from file system

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Hannes ForsgÄrd

code generator metaprogramming class finder minimizer

13/12 2017

1.1.3

1.1.3.0 https://github.com/hanneskod/classtools

Find, extract and process classes from file system

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Hannes ForsgÄrd

code generator metaprogramming class finder minimizer

28/12 2016

1.1.2

1.1.2.0 https://github.com/hanneskod/classtools

Find, extract and process classes from file system

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Hannes ForsgÄrd

code generator metaprogramming class finder minimizer

18/03 2016

1.1.1

1.1.1.0 https://github.com/hanneskod/classtools

Find, extract and process classes from file system

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Hannes ForsgÄrd

code generator metaprogramming class finder minimizer

02/03 2016

1.1.0

1.1.0.0 https://github.com/hanneskod/classtools

Find, extract and process classes from file system

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Hannes ForsgÄrd

code generator metaprogramming class finder minimizer

02/03 2016

1.0.1

1.0.1.0 https://github.com/hanneskod/classtools

Find, extract and process classes from file system

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Hannes ForsgÄrd

code generator metaprogramming class finder minimizer

20/10 2014

1.0.0

1.0.0.0 https://github.com/hanneskod/classtools

Find, extract and process classes from file system

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Hannes ForsgÄrd

code generator metaprogramming class finder minimizer

02/09 2014

1.0.0-beta3

1.0.0.0-beta3 https://github.com/hanneskod/classtools

Find, extract and process classes from file system

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Hannes ForsgÄrd

code generator metaprogramming class finder minimizer

14/08 2014

1.0.0-beta2

1.0.0.0-beta2 https://github.com/hanneskod/classtools

Find, extract and process classes from file system

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Hannes ForsgÄrd

transform metaprogramming minimize instantiate class iterator

14/08 2014

1.0.0-beta1

1.0.0.0-beta1 https://github.com/hanneskod/classtools

Find, extract and process classes from file system

  Sources   Download

WTFPL

The Requires

 

The Development Requires

by Hannes ForsgÄrd

transform metaprogramming minimize instantiate class iterator

15/05 2014

0.1.0

0.1.0.0 https://github.com/hanneskod/classtools

Iterate over classes found in filesystem

  Sources   Download

WTFPL

The Requires

  • php >=5.3.0

 

The Development Requires

by Hannes ForsgÄrd

class iterator class minimizer