Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

WIP Reduced filesize of phar #1101

Closed
wants to merge 2 commits into from

3 participants

@siad007

No description provided.

@mvriel mvriel commented on the diff
src/phpDocumentor/PharCompiler.php
((17 lines not shown))
$phar->addFromString($path, $file_contents);
}
/**
+ * Reduce the filesize of the PHAR removing whitespace and comments.
+ *
+ * @param string $fileContent
+ *
+ * @return string
+ */
+ protected function minifyFile($fileContent)
+ {
+ $tokens = token_get_all($fileContent);
+ $cleanedCode = "";
+ foreach ($tokens as $token) {
+ if (is_array($token)) {
+ if ($token[0] != T_COMMENT && $token[0] != T_DOC_COMMENT) {
@mvriel Owner
mvriel added a note

Removing DocComments may be an issue as the template serializer uses annotations to serialize and deserialize the XML information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@mvriel
Owner

Nice! It is a good thing to reduce the phar filesize :)

@mvriel
Owner

@siad007 are you sure this does not affect the annotations?

@siad007

Yes i am - but i wanted to add some whitespace stripping ;) so it's wip...

@stof

@mvriel Instead of making the custom phar compiler more complex, I would suggest keeping PhpDocumentor focused on its core business, and using Box to generate the phar (Fabien Potencier has also switched to it for the SensioLabs tools btw)

@mvriel
Owner

@stof I have been experimenting with Box last week but it is unable to generate a phar for this project. It exceeds the max memory size.

@mvriel
Owner

@stof to be exact, here is the related issue box-project/box2#64

@mvriel mvriel referenced this pull request from a commit
@mvriel mvriel #1099, #1101: replaced the box.json with a contributed version by @kh…
…erge

The current box.json that I build could not run due to a restriction on
open files. @Kherge managed to optimize the configuration file so that
that limit would no longer be hit.
a8b2288
@mvriel
Owner

@siad007 because @kherge has done a great job with box-project and to remove the maintenance for creating phar projects I have moved all phar handling to box. That means that the custom PHAR compiler will be removed from the project.

Unfortunately that also affects this PR and I will have to close it since I can no longer apply it. Thank you for the work done on this nonetheless.

@mvriel mvriel closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 28, 2013
  1. @siad007

    Reduced filesize of phar

    siad007 authored
  2. @siad007

    Excluded templates

    siad007 authored
This page is out of date. Refresh to see the latest.
Showing with 51 additions and 8 deletions.
  1. +51 −8 src/phpDocumentor/PharCompiler.php
View
59 src/phpDocumentor/PharCompiler.php
@@ -102,18 +102,23 @@ protected function initializePharArchive($pharFile)
*/
protected function getFiles()
{
- $files = array('LICENSE', 'README.md', 'VERSION');
-
$finder = new Finder();
$iterator = $finder->files()
+ ->ignoreVCS(true)
+ ->ignoreDotFiles(true)
->in(array('bin', 'data', 'src', 'vendor'))
->notName('*.rst')
->notName('*.md')
+ ->size('> 0k')
->exclude(
array(
'output',
'behat',
'cilex/cilex/tests',
+ 'jms/metadata/tests',
+ 'jms/parser-lib/tests',
+ 'jms/serializer/tests',
+ 'monolog/monolog/tests',
'nikic/php-parser/doc',
'nikic/php-parser/test',
'nikic/php-parser/test_old',
@@ -121,11 +126,13 @@ protected function getFiles()
'phpdocumentor/graphviz/tests',
'phpdocumentor/reflection-docblock/tests',
'pimple/pimple/tests',
+ 'psr/log/Psr/Log/Test',
'twig/twig/test',
)
- );
+ )
+ ->append(array('LICENSE', 'README.md', 'VERSION'));
- return array_merge($files, iterator_to_array($iterator));
+ return iterator_to_array($iterator);
}
/**
@@ -144,7 +151,7 @@ protected function addFilesToPharArchive(array $files, \Phar $phar)
foreach ($files as $file) {
echo '.';
$counter++;
- if ($counter % 70 == 0) {
+ if ($counter % 67 == 0) {
echo ' [' . $counter . '/' . count($files) . ']' . PHP_EOL;
}
$this->addFileToPharArchive($file, $phar);
@@ -165,13 +172,49 @@ protected function addFilesToPharArchive(array $files, \Phar $phar)
*/
protected function addFileToPharArchive($file, \Phar $phar)
{
- $path = str_replace(__DIR__ . '/', '', $file);
- $file_contents = file_get_contents($file);
- $file_contents = str_replace('#!/usr/bin/env php', '', $file_contents);
+ $path = strtr(str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath()), '\\', '/');
+ $handle = fopen($file, 'r');
+ $file_contents = fread($handle, filesize($file));
+ fclose($handle);
+
+ if ($path === 'bin/phpdoc' || $path === 'bin/phpdoc.php') {
+ $file_contents = str_replace('#!/usr/bin/env php', '', $file_contents);
+ }
+
+ if (strpos($path, 'vendor') === 0 || strpos($path, 'src') === 0) {
+ $file_contents = $this->minifyFile($file_contents);
+ }
+
$phar->addFromString($path, $file_contents);
}
/**
+ * Reduce the filesize of the PHAR removing whitespace and comments.
+ *
+ * @param string $fileContent
+ *
+ * @return string
+ */
+ protected function minifyFile($fileContent)
+ {
+ $tokens = token_get_all($fileContent);
+ $cleanedCode = "";
+ foreach ($tokens as $token) {
+ if (is_array($token)) {
+ if ($token[0] != T_COMMENT && $token[0] != T_DOC_COMMENT) {
@mvriel Owner
mvriel added a note

Removing DocComments may be an issue as the template serializer uses annotations to serialize and deserialize the XML information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ $cleanedCode .= $token[1];
+ }
+ } else {
+ $cleanedCode .= $token;
+ }
+
+ }
+
+ return $cleanedCode;
+ }
+
+
+ /**
* Adds the stubs for the CLI and Web interaction to the PHAR archive.
*
* @param \Phar $phar
Something went wrong with that request. Please try again.