浏览代码

minor fixes and cleanup in shells and libs

euromark 12 年之前
父节点
当前提交
3f7251b68a

+ 77 - 61
Console/Command/IndentShell.php

@@ -27,9 +27,7 @@ class IndentShell extends AppShell {
 
 	public $settings = array(
 		'files' => array('php', 'ctp', 'inc', 'tpl'),
-		'spacesPerTab' => 4,
 		'againWithHalf' => true, # if 4, go again with 2 afterwards
-		'test' => false, # just count - without doing anything
 		'outputToTmp' => false, # write to filename_.ext
 		'debug' => false # add debug info after each line
 	);
@@ -43,11 +41,11 @@ class IndentShell extends AppShell {
 	 * @return void
 	 */
 	public function folder() {
-		if (!empty($this->args)) {
-			if (in_array('test', $this->args)) {
-				$this->settings['test'] = true;
-			}
+		if (!empty($this->params['extensions'])) {
+			$this->settings['files'] = String::tokenize($this->params['extensions']);
+		}
 
+		if (!empty($this->args)) {
 			if (!empty($this->args[0]) && $this->args[0] !== 'app') {
 				$folder = $this->args[0];
 				if ($folder === '/') {
@@ -72,12 +70,12 @@ class IndentShell extends AppShell {
 			$this->_searchFiles();
 
 			$this->out('found: ' . count($this->_files));
-			if ($this->settings['test']) {
+			if (!empty($this->params['dry-run'])) {
 				$this->out('TEST DONE');
 			} else {
 				$continue = $this->in(__('Modifying files! Continue?'), array('y', 'n'), 'n');
 				if (strtolower($continue) !== 'y' && strtolower($continue) !== 'yes') {
-					die('...aborted');
+					$this->error('...aborted');
 				}
 
 				$this->_correctFiles3();
@@ -96,39 +94,7 @@ class IndentShell extends AppShell {
 		}
 	}
 
-
-
-	public function getOptionParser() {
-		$subcommandParser = array(
-			'options' => array(
-				'dry-run'=> array(
-					'short' => 'd',
-					'help' => __d('cake_console', 'Dry run the update, no files will actually be modified.'),
-					'boolean' => true
-				),
-				'log'=> array(
-					'short' => 'l',
-					'help' => __d('cake_console', 'Log all ouput to file log.txt in TMP dir'),
-					'boolean' => true
-				),
-				'interactive'=> array(
-					'short' => 'i',
-					'help' => __d('cake_console', 'Interactive'),
-					'boolean' => true
-				),
-			)
-		);
-
-		return parent::getOptionParser()
-			->description(__d('cake_console', "Correct indentation of files"))
-			->addSubcommand('folder', array(
-				'help' => __d('cake_console', 'Indent all files in a folder'),
-				'parser' => $subcommandParser
-			));
-	}
-
-
-	public function _write($file, $text) {
+	protected function _write($file, $text) {
 		$text = implode(PHP_EOL, $text);
 		if ($this->settings['outputToTmp']) {
 			$filename = extractPathInfo('file', $file);
@@ -140,7 +106,7 @@ class IndentShell extends AppShell {
 		return file_put_contents($file, $text);
 	}
 
-	public function _read($file) {
+	protected function _read($file) {
 		$text = file_get_contents($file);
 		if (empty($text)) {
 			return array();
@@ -156,15 +122,17 @@ class IndentShell extends AppShell {
 	 *
 	 * 2010-09-12 ms
 	 */
-	public function _correctFiles3() {
+	protected function _correctFiles3() {
 		foreach ($this->_files as $file) {
 			$this->changes = false;
 			$textCorrect = array();
 
 			$pieces = $this->_read($file);
+			$spacesPerTab = $this->params['spaces'];
+
 			foreach ($pieces as $piece) {
-				$tmp = $this->_process($piece, $this->settings['spacesPerTab']);
-				if ($this->settings['againWithHalf'] && ($spacesPerTab = $this->settings['spacesPerTab']) % 2 === 0 && $spacesPerTab > 3) {
+				$tmp = $this->_process($piece, $spacesPerTab);
+				if ($this->settings['againWithHalf'] && $spacesPerTab % 2 === 0 && $spacesPerTab > 3) {
 					$tmp = $this->_process($tmp, $spacesPerTab/2);
 				}
 
@@ -177,25 +145,31 @@ class IndentShell extends AppShell {
 		}
 	}
 
-	public function _process($piece, $spacesPerTab) {
+	/**
+	 * @return string
+	 */
+	protected function _process($piece, $spacesPerTab) {
 		$pos = -1;
 		$spaces = $mod = $tabs = 0;
 		$debug = '';
 
 		$newPiece = $piece;
-		//TODO
-		while (mb_substr($piece, $pos+1, 1) === ' ' || mb_substr($piece, $pos+1, 1) === TB) {
-			$pos++;
-		}
-		$piece1 = mb_substr($piece, 0, $pos+1);
-		$piece1 = str_replace(str_repeat(' ', $spacesPerTab), TB, $piece1, $count);
-		if ($count > 0) {
-			$this->changes = true;
-		}
+		if ($spacesPerTab) {
+			//TODO
+			while (mb_substr($piece, $pos+1, 1) === ' ' || mb_substr($piece, $pos+1, 1) === TB) {
+				$pos++;
+			}
+			$piece1 = mb_substr($piece, 0, $pos+1);
+			$piece1 = str_replace(str_repeat(' ', $spacesPerTab), TB, $piece1, $count);
+			if ($count > 0) {
+				$this->changes = true;
+			}
+
+			$piece2 = mb_substr($piece, $pos+1);
 
-		$piece2 = mb_substr($piece, $pos+1);
+			$newPiece = $piece1 . $piece2;
+		}
 
-		$newPiece = $piece1 . $piece2;
 		$newPiece = rtrim($newPiece) . $debug;
 		if ($newPiece != $piece || strlen($newPiece) !== strlen($piece)) {
 			$this->changes = true;
@@ -209,9 +183,10 @@ class IndentShell extends AppShell {
 	 * NEW TRY!
 	 * idea: hardcore replaceing
 	 *
+	 * @deprecated
 	 * 2010-09-12 ms
 	 */
-	public function _correctFiles2() {
+	protected function _correctFiles2() {
 		foreach ($this->_files as $file) {
 			$changes = false;
 			$textCorrect = array();
@@ -240,9 +215,11 @@ class IndentShell extends AppShell {
 	/**
 	 * Old try - sometimes TABS at the beginning are not recogized...
 	 * idea: strip tabs and spaces, remember their amount and add tabs again!
+	 *
+	 * @deprecated
 	 * 2010-09-12 ms
 	 */
-	public function _correctFiles() {
+	protected function _correctFiles() {
 		foreach ($this->_files as $file) {
 			$changes = false;
 			$textCorrect = array();
@@ -320,13 +297,52 @@ class IndentShell extends AppShell {
 	 * Search files that may contain translateable strings
 	 *
 	 * @return void
-	 * @access private
 	 */
-	public function _searchFiles() {
+	protected function _searchFiles() {
 		foreach ($this->_paths as $path) {
 			$Folder = new Folder($path);
 			$files = $Folder->findRecursive('.*\.('.implode('|', $this->settings['files']).')', true);
 			$this->_files += $files;
 		}
 	}
+
+	public function getOptionParser() {
+		$subcommandParser = array(
+			'options' => array(
+				'dry-run'=> array(
+					'short' => 'd',
+					'help' => __d('cake_console', 'Dry run the update, no files will actually be modified.'),
+					'boolean' => true
+				),
+				'log'=> array(
+					'short' => 'l',
+					'help' => __d('cake_console', 'Log all ouput to file log.txt in TMP dir'),
+					'boolean' => true
+				),
+				'interactive'=> array(
+					'short' => 'i',
+					'help' => __d('cake_console', 'Interactive'),
+					'boolean' => true
+				),
+				'spaces'=> array(
+					'short' => 's',
+					'help' => __d('cake_console', 'Spaces per Tab'),
+					'default' => '4',
+				),
+				'extensions'=> array(
+					'short' => 'e',
+					'help' => __d('cake_console', 'Extensions (comma-separated)'),
+					'default' => '',
+				),
+			)
+		);
+
+		return parent::getOptionParser()
+			->description(__d('cake_console', "Correct indentation of files"))
+			->addSubcommand('folder', array(
+				'help' => __d('cake_console', 'Indent all files in a folder'),
+				'parser' => $subcommandParser
+			));
+	}
+
 }

+ 1 - 5
Console/Command/WhitespaceShell.php

@@ -5,14 +5,10 @@ App::uses('Folder', 'Utility');
 class WhitespaceShell extends AppShell {
 
 	public $autoCorrectAll = false;
+
 	# each report: [0] => found, [1] => corrected
 	public $report = array('leading'=>array(0, 0), 'trailing'=>array(0, 0));
 
-	/*
-	public function main() {
-
-	}
-	*/
 
 	public function find() {
 		$App = new Folder(APP);

+ 29 - 6
Lib/IcalLib.php

@@ -5,7 +5,7 @@
 App::import('Vendor', 'Tools.ical', array('file'=>'ical/ical.php'));
 App::import('Vendor', 'Tools.icalobject', array('file'=>'ical/i_cal_object.php'));
 
-App::uses('CakeTime', 'Utility');
+App::uses('TimeLib', 'Tools.Utility');
 
 /**
  * A wrapper for the Ical/Ics calendar lib
@@ -34,25 +34,37 @@ class IcalLib {
 	 * - id => uid with @host
 	 * - start/end/timestamp to atom
 	 * - class to upper
+	 *
+	 * @param array $data
+	 * @param bool $addStartAndEnd
 	 * @return string $icalContent (single vevent)
 	 * 2011-10-10 ms
 	 */
 	public function build($data, $addStartAndEnd = true) {
+		$replacements = array('-', ':');
+		if (isset($data['timezone'])) {
+			$replacements[] = 'Z';
+		}
+
 		if (isset($data['start'])) {
-			$data['dtstart'] = CakeTime::toAtom($data['start']);
-			$data['dtstart'] = str_replace(array('-', ':'), '', $data['dtstart']);
+			$data['dtstart'] = TimeLib::toAtom($data['start']);
+			$data['dtstart'] = str_replace($replacements, '', $data['dtstart']);
 			unset($data['start']);
 		}
 		if (isset($data['end'])) {
-			$data['dtend'] = CakeTime::toAtom($data['end']);
-			$data['dtend'] = str_replace(array('-', ':'), '', $data['dtend']);
+			$data['dtend'] = TimeLib::toAtom($data['end']);
+			$data['dtend'] = str_replace($replacements, '', $data['dtend']);
 			unset($data['end']);
 		}
 		if (isset($data['timestamp'])) {
-			$data['dtstamp'] = CakeTime::toAtom($data['timestamp']);
+			$data['dtstamp'] = TimeLib::toAtom($data['timestamp']);
 			$data['dtstamp'] = str_replace(array('-', ':'), '', $data['dtstamp']);
 			unset($data['timestamp']);
 		}
+		if (isset($data['timezone'])) {
+			$data['tzid'] = $data['timezone'];
+			unset($data['timezone']);
+		}
 		if (isset($data['id'])) {
 			$data['uid'] = $data['id'].'@'.env('HTTP_HOST');
 			unset($data['id']);
@@ -70,6 +82,12 @@ class IcalLib {
 		return $res;
 	}
 
+	/**
+	 * Start the file
+	 *
+	 * @param array $data
+	 * @return string
+	 */
 	public function createStart($data = array()) {
 		$defaults = array(
 			'version' => '2.0',
@@ -86,6 +104,11 @@ class IcalLib {
 		return implode(PHP_EOL, $res);
 	}
 
+	/**
+	 * End the file
+	 *
+	 * @return string
+	 */
 	public function createEnd() {
 		return 'END:VCALENDAR';
 	}

+ 9 - 13
Lib/Utility/NumberLib.php

@@ -79,7 +79,6 @@ class NumberLib extends CakeNumber {
 		}
 		$options = array('before' => '', 'after' => '', 'places' => 2, 'thousands' => self::$_thousandsPoint, 'decimals' => self::$_decimalPoint, 'escape' => false);
 		$options = array_merge($options, $formatOptions);
-		//$options = array;
 
 		if (!empty($options['currency'])) {
 			if (!empty(self::$_symbolRight)) {
@@ -88,21 +87,18 @@ class NumberLib extends CakeNumber {
 				$options['before'] = self::$_symbolLeft . ' ';
 			}
 		}
+
 		/*
-		else {
-			if (!empty($formatOptions['after'])) {
-				$options['after'] = $formatOptions['after'];
+		if ($spacer !== false) {
+			$spacer = ($spacer === true) ? ' ' : $spacer;
+			if ((string)$before !== '') {
+				$before .= $spacer;
 			}
-			if (!empty($formatOptions['before'])) {
-				$options['before'] = $formatOptions['before'];
+			if ((string)$after !== '') {
+				$after = $spacer . $after;
 			}
 		}
-		if (!empty($formatOptions['thousands'])) {
-			$options['thousands'] = $formatOptions['thousands'];
-		}
-		if (!empty($formatOptions['decimals'])) {
-			$options['decimals'] = $formatOptions['decimals'];
-		}
+
 		*/
 		if ($options['places'] < 0) {
 			$number = round($number, $options['places']);
@@ -292,7 +288,7 @@ class NumberLib extends CakeNumber {
 	 * @return boolean
 	 */
 	public static function isFloatEqual($x, $y, $precision = 0.0000001) {
-		return ($x+$precision >= $y) && ($x-$precision <= $y);
+		return ($x + $precision >= $y) && ($x - $precision <= $y);
 	}
 
 }

+ 35 - 0
Model/MyModel.php

@@ -1537,6 +1537,41 @@ class MyModel extends Model {
 	}
 
 	/**
+	 * Get all related entries that have been used so far
+	 *
+	 * @return array
+	 */
+	public function getRelatedInUse($modelName, $groupField, $type = 'all', $options = array()) {
+		$defaults = array(
+			'contain' => array($modelName),
+			'group' => $groupField,
+			'order' => array($modelName . '.' . $this->$modelName->displayField => 'ASC'),
+		);
+		if ($type === 'list') {
+			$defaults['fields'] = array($modelName . '.' . $this->$modelName->primaryKey, $modelName . '.' . $this->$modelName->displayField);
+		}
+		$options += $defaults;
+		return $this->find($type, $options);
+	}
+
+	/**
+	 * Get all fields that have been used so far
+	 *
+	 * @return array
+	 */
+	public function getFieldInUse($groupField, $type = 'all', $options = array()) {
+		$defaults = array(
+			'group' => $groupField,
+			'order' => array($this->alias . '.' . $this->displayField => 'ASC'),
+		);
+		if ($type === 'list') {
+			$defaults['fields'] = array($this->alias . '.' . $this->primaryKey, $this->alias . '.' . $this->displayField);
+		}
+		$options += $defaults;
+		return $this->find($type, $options);
+	}
+
+	/**
 	 * Update a row with certain fields (dont use "Model" as super-key)
 	 * @param int $id
 	 * @param array $data

+ 0 - 2
Test/Case/AllToolsTest.php

@@ -35,8 +35,6 @@ class AllToolsTest extends PHPUnit_Framework_TestSuite {
 		$path = dirname(__FILE__);
 		$Suite->addTestDirectory($path . DS . 'Controller' . DS . 'Component' . DS . 'Auth');
 
-
-
 		//$path = CakePlugin::path('Tools') . 'Test' . DS . 'Case' . DS;
 		//$Suite->addTestDirectoryRecursive($path);
 		return $Suite;

文件差异内容过多而无法显示
+ 36 - 3
Test/Case/Lib/EmailLibTest.php


+ 33 - 2
Test/Case/Lib/Utility/NumberLibTest.php

@@ -81,8 +81,6 @@ class NumberLibTest extends MyCakeTestCase {
 		);
 		foreach ($values as $was => $expected) {
 			$is = NumberLib::roundTo($was, 10);
-			//debug($expected); debug($is); echo BR;
-
 			$this->assertSame($expected, $is, null, $was);
 		}
 		//increment = 0.1
@@ -183,4 +181,37 @@ class NumberLibTest extends MyCakeTestCase {
 		}
 	}
 
+	/**
+	 * Test spacer format options for currency() method
+	 *
+	 * @return void
+	 */
+	public function testCurrencySpacer() {
+		$this->skipIf(true, 'TODO');
+
+		$result = NumberLib::currency('4.111', 'EUR');
+		$expected = '€4,11';
+		$this->assertEquals($expected, $result);
+
+		$result = NumberLib::currency('4.111', 'EUR', array('spacer' => false));
+		$expected = '€4,11';
+		$this->assertEquals($expected, $result);
+
+		$result = NumberLib::currency('4.111', 'EUR', array('spacer' => true));
+		$expected = '€ 4,11';
+		$this->assertEquals($expected, $result);
+
+		$result = NumberLib::currency('-4.111', 'GBP', array('spacer' => false, 'negative' => '-'));
+		$expected = '-£4.11';
+		$this->assertEquals($expected, $result);
+
+		$result = NumberLib::currency('-4.111', 'GBP', array('spacer' => true, 'negative' => '-'));
+		$expected = '-£ 4.11';
+		$this->assertEquals($expected, $result);
+
+		$result = NumberLib::currency('4.111', 'EUR', array('spacer' => '&nbsp;', 'escape' => false));
+		$expected = '€&nbsp;4,11';
+		$this->assertEquals($expected, $result);
+	}
+
 }

+ 12 - 0
Test/Case/View/Helper/MyHelperTest.php

@@ -9,6 +9,8 @@ class MyHelperTest extends MyCakeTestCase {
 	public $MyHelper;
 
 	public function setUp() {
+		parent::setUp();
+
 		$this->MyHelper = new MyHelper(new View(null));
 	}
 
@@ -17,5 +19,15 @@ class MyHelperTest extends MyCakeTestCase {
 		$this->assertInstanceOf('MyHelper', $this->MyHelper);
 	}
 
+	public function testLoadHelpers() {
+		$this->skipIf(class_exists('QrCodeHelper'), 'Already loaded');
+
+		$this->assertTrue(!class_exists('QrCodeHelper'));
+
+		$this->MyHelper->loadHelpers(array('Tools.QrCode'));
+
+		$this->assertTrue(class_exists('QrCodeHelper'));
+	}
+
 	//TODO
 }

+ 5 - 55
View/Helper/MyHelper.php

@@ -19,7 +19,10 @@ class MyHelper extends Helper {
 	}
 
 	/**
-	 * manually
+	 * Manually load helpers
+	 * @param array $helpers (either strings, or [string => array(config...)])
+	 * @param bool $callbacks - trigger missed callbacks
+	 * @return void
 	 */
 	public function loadHelpers($helpers = array(), $callbacks = false) {
 		foreach ((array)$helpers as $helper => $config) {
@@ -230,21 +233,6 @@ class MyHelper extends Helper {
 	public $urlHere = null;
 
 	/**
-	 * Small Helper Function to retrieve CORRECT $this->here (as it should be) - CAKE BUG !? -> this is a fix
-	 * 2009-01-06 ms
-	 */
-	public function here() {
-		if (empty($this->urlHere) && isset($_GET['url'])) {
-			$this->urlHere = $_GET['url'];
-			if (strpos($this->urlHere, '/') !== 0) {
-				$this->urlHere = '/'.$this->urlHere;
-			}
-		}
-		return $this->urlHere;
-	}
-
-
-	/**
 	 * enhancement to htmlHelper which allows the crumbs protected array
 	 * to be cleared so that more than one set of crumbs can be generated in the same view.
 	 *
@@ -255,44 +243,6 @@ class MyHelper extends Helper {
 		$this->_crumbs = array();
 	}
 
-
-
-/** deprecated */
-
-	/**
-	 * @deprecated
-	 */
-	public function nl2p($text, $options = array(), $enforceMaxLength = true) {
-		$pS = $this->Html->tag('p', null, $options);
-		$pE = '</p>';
-		if (!empty($text)) {
-			// Max length auto line break, if enabled
-			if ($enforceMaxLength) {
-				$maxLength = null;
-				if (isset($options['maxLength'])) {
-					$maxLength = (int)$options['maxLength'];
-				}
-				$text = $this->maxLength($text, $maxLength);
-			}
-			// Replace double newlines with <p>
-			$text = $pS . preg_replace('#(\r?\n) {2,}(\s+)?#u', $pE . $pS, $text) . $pE;
-			// Replace single newlines with <br>
-			$text = preg_replace('#\r?\n#u', BR, $text);
-			// Add newlines to sourcecode for sourcode readability
-			$text = preg_replace(
-				array(
-					'#' . $pE . '#u', // Matches $pE (like </p>)
-					'#' . BR . '#u', // Matches $br (like <br />)
-				),
-				array(
-					$pE . "\n",
-					BR . "\n",
-				),
-				$text);
-		}
-		return $text;
-	}
-
 	/**
 	 * This function is responsible for setting up the Url cache before the application starts generating urls in views
 	 *
@@ -313,7 +263,7 @@ class MyHelper extends Helper {
 	 *
 	 * @return void
 	 */
-	public function afterLayout($layoutFile = null) {
+	public function afterLayout($layoutFile) {
 		if (!Configure::read('UrlCache.active') || Configure::read('UrlCache.runtime.afterLayout')) {
 			return;
 		}