11#!/usr/bin/env php
22<?php
3- if (is_dir ( $ vendor = __DIR__ . '/vendor ' )) {
4- require ($ vendor . ' / autoload.php ' );
5- } elseif (is_dir ( $ vendor = __DIR__ . '/../../../vendor ' )) {
6- require ($ vendor . ' / autoload.php ' );
3+ if (file_exists ( $ autoload = __DIR__ . '/vendor/autoload.php ' )) {
4+ require ($ autoload );
5+ } elseif (file_exists ( $ autoload = __DIR__ . '/../../autoload.php ' )) {
6+ require ($ autoload );
77} else {
88 die (
99 'You must set up the project dependencies, run the following commands: ' .PHP_EOL .
@@ -21,6 +21,7 @@ $options = getopt('af:R',array(
2121 'format:: ' ,
2222 'recurse:: ' ,
2323 'version ' ,
24+ 'memory:: ' ,
2425));
2526
2627if (!isset ($ options ['debug ' ])) {
@@ -44,12 +45,22 @@ if(!isset($options['excmd']))
4445 $ options ['excmd ' ] = 'pattern ' ;
4546if (!isset ($ options ['format ' ]))
4647 $ options ['format ' ] = 2 ;
48+ if (!isset ($ options ['memory ' ]))
49+ $ options ['memory ' ] = '128M ' ;
4750if (!isset ($ options ['fields ' ])) {
4851 $ options ['fields ' ] = array ('n ' , 'k ' ,'s ' , 'a ' );
4952} else {
5053 $ options ['fields ' ] = str_split ($ options ['fields ' ]);
5154}
5255
56+ // if the memory limit option is set and is valid, adjust memory
57+ if (isset ($ options ['memory ' ])) {
58+ $ memory_limit = trim ($ options ['memory ' ]);
59+ if (isMemoryLimitValid ($ memory_limit )) {
60+ ini_set ('memory_limit ' , $ memory_limit );
61+ }
62+ }
63+
5364if (isset ($ options ['append ' ])) {
5465 if ($ options ['append ' ] === FALSE || yes_or_no ($ options ['append ' ]) == 'yes ' ) {
5566 $ options ['a ' ] = FALSE ;
@@ -111,3 +122,19 @@ function yes_or_no($arg) {
111122 return false ;
112123 }
113124}
125+
126+ function isMemoryLimitValid ($ memory_limit ) {
127+ if ($ memory_limit == "-1 " ) {
128+ // no memory limit
129+ return true ;
130+ } elseif (is_numeric ($ memory_limit ) && $ memory_limit > 0 ) {
131+ // memory limit provided in bytes
132+ return true ;
133+ } elseif (preg_match ("/\d+\s*[KMG]/ " , $ memory_limit )) {
134+ // memory limit provided in human readable sizes
135+ // as specified here: http://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes
136+ return true ;
137+ }
138+
139+ return false ;
140+ }
0 commit comments