Permalink
Browse files

Adding the CoreDump to the toolset.

  • Loading branch information...
1 parent 37d9a05 commit 02a9ebe2ecc39fc7ac51a7cc2a75a9db4926d7a6 @zyxist zyxist committed Jul 9, 2011
Showing with 157 additions and 0 deletions.
  1. +26 −0 src/Opl/Autoloader/Exception/FileNotFoundException.php
  2. +131 −0 src/Opl/Autoloader/Toolset/CoreDump.php
@@ -0,0 +1,26 @@
+<?php
+/*
+ * OPEN POWER LIBS <http://www.invenzzia.org>
+ *
+ * This file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE. It is also available through
+ * WWW at this URL: <http://www.invenzzia.org/license/new-bsd>
+ *
+ * Copyright (c) Invenzzia Group <http://www.invenzzia.org>
+ * and other contributors. See website for details.
+ */
+namespace Opl\Autoloader\Exception;
+use RuntimeException;
+
+/**
+ * This exception is used by the toolset classes to report that they cannot
+ * find a requested file.
+ *
+ * @author Tomasz Jędrzejewski
+ * @copyright Invenzzia Group <http://www.invenzzia.org/> and contributors.
+ * @license http://www.invenzzia.org/license/new-bsd New BSD License
+ */
+class FileNotFoundException extends RuntimeException
+{
+
+} // end FileNotFoundException;
@@ -0,0 +1,131 @@
+<?php
+/*
+ * OPEN POWER LIBS <http://www.invenzzia.org>
+ *
+ * This file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE. It is also available through
+ * WWW at this URL: <http://www.invenzzia.org/license/new-bsd>
+ *
+ * Copyright (c) Invenzzia Group <http://www.invenzzia.org>
+ * and other contributors. See website for details.
+ */
+namespace Opl\Autoloader\Toolset;
+use Opl\Autoloader\Exception\FileNotFoundException;
+
+/**
+ * This tool allows to process the application core in order to optimize
+ * the loading process.
+ *
+ * @author Tomasz Jędrzejewski
+ * @copyright Invenzzia Group <http://www.invenzzia.org/> and contributors.
+ * @license http://www.invenzzia.org/license/new-bsd New BSD License
+ */
+class CoreDump extends AbstractTool
+{
+ /**
+ * The application core.
+ * @var string[]
+ */
+ protected $core;
+
+ /**
+ * Loads the application core generated by <tt>CoreTracker</tt>.
+ *
+ * @throws FileNotFoundException
+ * @param string $coreFile The path to the core file.
+ * @return CoreDump Fluent interface.
+ */
+ public function loadCore($coreFile)
+ {
+ if(!file_exists($coreFile))
+ {
+ throw new FileNotFoundException('Cannot open the specified core file.');
+ }
+ $this->core = unserialize(file_get_contents($coreFile));
+ return $this;
+ } // end loadCore();
+
+ /**
+ * Produces a PHP file that contains the <tt>require</tt> statements that
+ * load the application core.
+ *
+ * @param string $output The path to the output file.
+ * @return CoreDump Fluent interface.
+ */
+ public function exportRequireList($output)
+ {
+ $outFile = fopen($output, 'w');
+ fwrite($outFile, '<'.'?php'.PHP_EOL.'// This file has been generated automatically by Open Power Autoloader.'.
+ PHP_EOL.'// Do not edit manually!'.PHP_EOL);
+
+ foreach($this->core as $className)
+ {
+ fwrite($outFile, 'require(\''.$this->toFilename($className, true).'\');'.PHP_EOL);
+ }
+
+ fclose($outFile);
+ return $this;
+ } // end exportRequireList();
+
+ /**
+ * Produces a PHP file that contains the concatenated application core.
+ * The algorithm correctly handles namespaces, ensuring that there will be
+ * no conflicts, and might also be used to remove the heading comments.
+ *
+ * @param string $output The path to the output file.
+ * @param boolean $removeHeadings Do we remove heading comments?
+ * @return CoreDump Fluent interface.
+ */
+ public function exportConcatenated($output, $removeHeadings = true)
+ {
+ $outFile = fopen($output, 'w');
+ fwrite($outFile, '<'.'?php'.PHP_EOL.'// This file has been generated automatically by Open Power Autoloader.'.
+ PHP_EOL.'// Do not edit manually!'.PHP_EOL);
+
+ foreach($this->core as $className)
+ {
+ fwrite($outFile, $this->parseFile($this->toFilename($className, true), $removeHeadings));
+ }
+
+ fclose($outFile);
+ return $this;
+ } // end exportConcatenated();
+
+ /**
+ * Processes a single PHP file in order to concatenate it.
+ *
+ * @param string $filename The PHP file name
+ * @param boolean $removeHeadings Do we remove heading comments?
+ * @return string The output document
+ */
+ protected function parseFile($filename, $removeHeadings)
+ {
+ $content = file_get_contents($filename);
+
+ // Removing headings
+ if($removeHeadings)
+ {
+ // \/\*\*.+\*\/
+ $content = preg_replace('#^\<\?php[\s\n\r]*\/\*\*.+\*\/[\s\n\r]*#is', '', $content);
+ }
+ else
+ {
+ $content = preg_replace('#^\<\?php\s*#i', '', $content);
+ }
+
+ // Fixing the namespace definition to avoid collisions after concatenation.
+ if(preg_match('#namespace\s+([a-zA-Z0-9\_\\\\]+)\s*([\\;\{])#i', $content, $matches))
+ {
+ if($matches[2] == ';')
+ {
+ $content = str_replace($matches[0], 'namespace '.$matches[1].'{', $content).PHP_EOL.'}'.PHP_EOL;
+ }
+ }
+ else
+ {
+ $content = PHP_EOL.'namespace'.PHP_EOL.'{'.PHP_EOL.$content.PHP_EOL.'}'.PHP_EOL;
+ }
+
+ return $content;
+ } // end parseFile();
+} // end CoreDump;

0 comments on commit 02a9ebe

Please sign in to comment.