Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

updated readme; moved ClassmapTask to phing-task

  • Loading branch information...
commit 62767a3cc398bdb66aa04e0d22162a2a0e62f519 1 parent 804394e
heartsentwined authored
Showing with 14 additions and 164 deletions.
  1. +14 −4 README.md
  2. +0 −160 src/task/ClassmapTask.php
View
18 README.md
@@ -39,20 +39,30 @@ $ pear install [--alldeps] phing/phing
# Usage
-## ClassmapTask
+You must initialize an instance of your Zf2 application through **ZfTask** before using any of the remaining Tasks.
-Generate a classmap for the directory `foo/library` (and its subdirectories), save it at `foo/autoload_classmap.php`.
+## ZfTask
+
+Bootstrap the ZF2 application using the file `foo/bootstrap.php`. The bootstrap file must return an instance of `Zend\Mvc\Application`.
```xml
<project>
<target>
<includepath classpath="vendor/heartsentwined/zf2-phing-task/src/task" />
- <taskdef name="classmap" classname="ClassmapTask" />
- <classmap dir="foo/library" output="foo/autoload_classmap.php" />
+ <taskdef name="zf" classname="ZfTask" />
+ <zf bootstrap="foo/bootstrap.php" />
</target>
</project>
```
+`foo/bootstrap.php`:
+
+```php
+/* do some bootstrap */
+$application = Zend\Mvc\Application::init(/* config array */);
+return $application;
+```
+
## DoctrineEntityTask
Generate entities for the EntityManager `doctrine.entitymanager.orm_default`, base directory at `foo/src`, with filter `Foo\Entity`.
View
160 src/task/ClassmapTask.php
@@ -1,160 +0,0 @@
-<?php
-require_once __DIR__ . '/../../../../autoload.php';
-
-use Zend\File\ClassFileLocator;
-
-class ClassmapTask extends Task
-{
- protected $dir;
- protected $output;
- protected $failonerror = false;
-
- /**
- * directory to search for class files (recursive)
- *
- * @param string $dir
- * @return void
- */
- public function setDir($dir)
- {
- if (!is_dir($dir)) {
- throw new BuildException(sprintf(
- 'Directory does not exist: %s',
- realpath($dir)
- ));
- }
- if (!is_writable($dir)) {
- throw new BuildException(sprintf(
- 'Directory is not writable: %s',
- realpath($dir)
- ));
- }
- $this->dir = realpath($dir);
- }
-
- /**
- * output file
- *
- * @param string $output
- * @return void
- */
- public function setOutput($output)
- {
- touch($output);
- $this->output = realpath($output);
- }
-
- /**
- * if error occured, whether build should fail
- *
- * @param bool $value
- * @return void
- */
- public function setFailonerror($value)
- {
- $this->failonerror = $value;
- }
-
- /**
- * init
- *
- * @return void
- */
- public function init()
- {
- }
-
- /**
- * main method
- *
- * @return void
- */
- public function main()
- {
- if (!$this->dir) {
- $this->dir = getcwd();
- }
-
- $this->log(sprintf('Generating classmap file for %s', $this->dir));
-
- // We need to add the $this->dir into the relative path that is created in the classmap file.
- $classmapPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath(dirname($this->output)));
-
- // Simple case: $libraryPathCompare is in $classmapPathCompare
- if (strpos($this->dir, $classmapPath) === 0) {
- $relativePathForClassmap = substr($this->dir, strlen($classmapPath) + 1);
- } else {
- $libraryPathParts = explode('/', $this->dir);
- $classmapPathParts = explode('/', $classmapPath);
-
- // Find the common part
- $count = count($classmapPathParts);
- for ($i = 0; $i < $count; $i++) {
- if (!isset($libraryPathParts[$i]) || $libraryPathParts[$i] != $classmapPathParts[$i]) {
- // Common part end
- break;
- }
- }
-
- // Add parent dirs for the subdirs of classmap
- $relativePathForClassmap = str_repeat('../', $count - $i);
-
- // Add library subdirs
- $count = count($libraryPathParts);
- for (; $i < $count; $i++) {
- $relativePathForClassmap .= $libraryPathParts[$i] . '/';
- }
- }
-
- // Get the ClassFileLocator, and pass it the library path
- $locator = new ClassFileLocator($this->dir);
-
- // Iterate over each element in the path, and create a map of
- // classname => filename, where the filename is relative to the library path
- $map = new stdClass;
- foreach ($locator as $file) {
- $filename = str_replace($this->dir . '/', '', str_replace(DIRECTORY_SEPARATOR, '/', $file->getPath()) . '/' . $file->getFilename());
-
- // Add in relative path to library
- $filename = $relativePathForClassmap . $filename;
-
- foreach ($file->getClasses() as $class) {
- $map->{$class} = $filename;
- }
- }
-
- // Create a file with the class/file map.
- // Stupid syntax highlighters make separating < from PHP declaration necessary
- $content = '<' . "?php\n"
- . "// Auto-generated by Phing, using ZF2's classmap generator\n"
- . '// Generation time: ' . date(DATE_RFC2822) . "\n"
- . 'return ' . var_export((array) $map, true) . ';';
-
- // Prefix with __DIR__; modify the generated content
- $content = preg_replace("#(=> ')#", "=> __DIR__ . '/", $content);
-
- // Fix \' strings from injected DIRECTORY_SEPARATOR usage in iterator_apply op
- $content = str_replace("\\'", "'", $content);
-
- // Remove unnecessary double-backslashes
- $content = str_replace('\\\\', '\\', $content);
-
- // Exchange "array (" width "array("
- $content = str_replace('array (', 'array(', $content);
-
- // Align "=>" operators to match coding standard
- preg_match_all('(\n\s+([^=]+)=>)', $content, $matches, PREG_SET_ORDER);
- $maxWidth = 0;
-
- foreach ($matches as $match) {
- $maxWidth = max($maxWidth, strlen($match[1]));
- }
-
- $content = preg_replace('(\n\s+([^=]+)=>)e', "'\n \\1' . str_repeat(' ', " . $maxWidth . " - strlen('\\1')) . '=>'", $content);
-
- // Write the contents to disk
- file_put_contents($this->output, $content);
-
- $this->log(sprintf('Wrote classmap file at %s', $this->output));
- }
-}
Please sign in to comment.
Something went wrong with that request. Please try again.