diff --git a/library/Zend/File/ClassFileLocator.php b/library/Zend/File/ClassFileLocator.php index 993eb1f4367..4dc130bce04 100644 --- a/library/Zend/File/ClassFileLocator.php +++ b/library/Zend/File/ClassFileLocator.php @@ -1,4 +1,22 @@ false, 'useByteString' => true, 'magicFile' => null, @@ -186,8 +185,8 @@ abstract public function isFiltered($files = null); * @param string|array $file File to add * @param string|array $validator Validators to use for this file, must be set before * @param string|array $filter Filters to use for this file, must be set before - * @return \Zend\File\Transfer\Adapter\AbstractAdapter - * @throws \Zend\File\Transfer\Exception Not implemented + * @return AbstractAdapter + * @throws Exception Not implemented */ //abstract public function addFile($file, $validator = null, $filter = null); @@ -195,7 +194,7 @@ abstract public function isFiltered($files = null); * Returns all set types * * @return array List of set types - * @throws \Zend\File\Transfer\Exception Not implemented + * @throws Exception Not implemented */ //abstract public function getType(); @@ -205,8 +204,8 @@ abstract public function isFiltered($files = null); * @param string|array $type Type of files to add * @param string|array $validator Validators to use for this file, must be set before * @param string|array $filter Filters to use for this file, must be set before - * @return \Zend\File\Transfer\Adapter\AbstractAdapter - * @throws \Zend\File\Transfer\Exception Not implemented + * @return AbstractAdapter + * @throws Exception Not implemented */ //abstract public function addType($type, $validator = null, $filter = null); @@ -220,18 +219,18 @@ abstract public function isFiltered($files = null); /** * Set plugin loader to use for validator or filter chain * - * @param \Zend\Loader\ShortNameLocator $loader - * @param string $type 'filter', or 'validator' - * @return \Zend\File\Transfer\Adapter\AbstractAdapter - * @throws \Zend\File\Transfer\Exception on invalid type + * @param Loader\ShortNameLocator $loader + * @param string $type 'filter', or 'validator' + * @return AbstractAdapter + * @throws Exception\InvalidArgumentException on invalid type */ - public function setPluginLoader(ShortNameLocator $loader, $type) + public function setPluginLoader(Loader\ShortNameLocator $loader, $type) { $type = strtoupper($type); switch ($type) { case self::FILTER: case self::VALIDATOR: - $this->_loaders[$type] = $loader; + $this->loaders[$type] = $loader; return $this; default: throw new Exception\InvalidArgumentException(sprintf('Invalid type "%s" provided to setPluginLoader()', $type)); @@ -245,8 +244,8 @@ public function setPluginLoader(ShortNameLocator $loader, $type) * 'filter' or 'validator' for $type. * * @param string $type - * @return \Zend\Loader\ShortNameLocator - * @throws \Zend\File\Transfer\Exception on invalid type. + * @return Loader\ShortNameLocator + * @throws Exception\InvalidArgumentException on invalid type. */ public function getPluginLoader($type) { @@ -256,23 +255,24 @@ public function getPluginLoader($type) case self::VALIDATOR: $prefixSegment = ucfirst(strtolower($type)); $pathSegment = $prefixSegment; - if (!isset($this->_loaders[$type])) { + if (!isset($this->loaders[$type])) { $paths = array( 'Zend\\' . $prefixSegment . '\\' => 'Zend/' . $pathSegment . '/', 'Zend\\' . $prefixSegment . '\File' => 'Zend/' . $pathSegment . '/File', ); - $this->_loaders[$type] = new PrefixPathLoader($paths); + $this->loaders[$type] = new Loader\PrefixPathLoader($paths); } else { - $loader = $this->_loaders[$type]; - if ($loader instanceof PrefixPathMapper) { + $loader = $this->loaders[$type]; + if ($loader instanceof Loader\PrefixPathMapper) { $prefix = 'Zend\\' . $prefixSegment . '\File\\'; if (!$loader->getPaths($prefix)) { $loader->addPrefixPath($prefix, str_replace('_', '/', $prefix)); } } } - return $this->_loaders[$type]; + + return $this->loaders[$type]; default: throw new Exception\InvalidArgumentException(sprintf('Invalid type "%s" provided to getPluginLoader()', $type)); } @@ -291,8 +291,8 @@ public function getPluginLoader($type) * @param string $prefix * @param string $path * @param string $type - * @return \Zend\File\Transfer\Adapter\AbstractAdapter - * @throws \Zend\File\Transfer\Exception for invalid type + * @return AbstractAdapter + * @throws Exception\InvalidArgumentException for invalid type */ public function addPrefixPath($prefix, $path, $type = null) { @@ -301,22 +301,24 @@ public function addPrefixPath($prefix, $path, $type = null) case self::FILTER: case self::VALIDATOR: $loader = $this->getPluginLoader($type); - if ($loader instanceof PrefixPathMapper) { + if ($loader instanceof Loader\PrefixPathMapper) { $loader->addPrefixPath($prefix, $path); } + return $this; case null: $prefix = rtrim($prefix, '\\'); $path = rtrim($path, DIRECTORY_SEPARATOR); foreach (array(self::FILTER, self::VALIDATOR) as $type) { $loader = $this->getPluginLoader($type); - if ($loader instanceof PrefixPathMapper) { + if ($loader instanceof Loader\PrefixPathMapper) { $cType = ucfirst(strtolower($type)); $pluginPath = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR; $pluginPrefix = $prefix . '\\' . $cType; $loader->addPrefixPath($pluginPrefix, $pluginPath); } } + return $this; default: throw new Exception\InvalidArgumentException(sprintf('Invalid type "%s" provided to getPluginLoader()', $type)); @@ -327,7 +329,7 @@ public function addPrefixPath($prefix, $path, $type = null) * Add many prefix paths at once * * @param array $spec - * @return \Zend\File\Transfer\Exception + * @return AbstractAdapter */ public function addPrefixPaths(array $spec) { @@ -362,17 +364,19 @@ public function addPrefixPaths(array $spec) } } } + return $this; } /** * Adds a new validator for this class * - * @param string|array $validator Type of validator to add - * @param boolean $breakChainOnFailure If the validation chain should stop an failure - * @param string|array $options Options to set for the validator - * @param string|array $files Files to limit this validator to - * @return \Zend\File\Transfer\Adapter\AbstractAdapter + * @param string|Validator\Validator $validator Type of validator to add + * @param boolean $breakChainOnFailure If the validation chain should stop an failure + * @param string|array $options Options to set for the validator + * @param string|array $files Files to limit this validator to + * @return AbstractAdapter + * @throws Exception\InvalidArgumentException for invalid type */ public function addValidator($validator, $breakChainOnFailure = false, $options = null, $files = null) { @@ -391,22 +395,22 @@ public function addValidator($validator, $breakChainOnFailure = false, $options unset($options['messages']); } } else { - throw new Exception\InvalidArgumentException('Invalid validator provided to addValidator; must be string or Zend_VALIDATOR_Interface'); + throw new Exception\InvalidArgumentException('Invalid validator provided to addValidator; must be string or Zend\Validator\Validator'); } - $this->_validators[$name] = $validator; - $this->_break[$name] = $breakChainOnFailure; - $files = $this->_getFiles($files, true, true); + $this->validators[$name] = $validator; + $this->break[$name] = $breakChainOnFailure; + $files = $this->getFiles($files, true, true); foreach ($files as $file) { if ($name == 'NotEmpty') { - $temp = $this->_files[$file]['validators']; - $this->_files[$file]['validators'] = array($name); - $this->_files[$file]['validators'] += $temp; + $temp = $this->files[$file]['validators']; + $this->files[$file]['validators'] = array($name); + $this->files[$file]['validators'] += $temp; } else { - $this->_files[$file]['validators'][] = $name; + $this->files[$file]['validators'][] = $name; } - $this->_files[$file]['validated'] = false; + $this->files[$file]['validated'] = false; } return $this; @@ -415,9 +419,10 @@ public function addValidator($validator, $breakChainOnFailure = false, $options /** * Add Multiple validators at once * - * @param array $validators + * @param array $validators * @param string|array $files - * @return \Zend\File\Transfer\Adapter\AbstractAdapter + * @return AbstractAdapter + * @throws Exception\InvalidArgumentException for invalid type */ public function addValidators(array $validators, $files = null) { @@ -482,9 +487,9 @@ public function addValidators(array $validators, $files = null) /** * Sets a validator for the class, erasing all previous set * - * @param string|array $validator Validator to set - * @param string|array $files Files to limit this validator to - * @return Zend_File_Transfer_Adapter + * @param array $validators Validators to set + * @param string|array $files Files to limit this validator to + * @return AbstractAdapter */ public function setValidators(array $validators, $files = null) { @@ -500,21 +505,21 @@ public function setValidators(array $validators, $files = null) */ public function hasValidator($name) { - return (false !== $this->_getValidatorIdentifier($name)); + return (false !== $this->getValidatorIdentifier($name)); } /** * Retrieve individual validator * * @param string $name - * @return \Zend\Validator\Validator|null + * @return Validator\Validator|null */ public function getValidator($name) { - if (false === ($identifier = $this->_getValidatorIdentifier($name))) { + if (false === ($identifier = $this->getValidatorIdentifier($name))) { return null; } - return $this->_validators[$identifier]; + return $this->validators[$identifier]; } /** @@ -526,21 +531,21 @@ public function getValidator($name) public function getValidators($files = null) { if ($files == null) { - return $this->_validators; + return $this->validators; } - $files = $this->_getFiles($files, true, true); + $files = $this->getFiles($files, true, true); $validators = array(); foreach ($files as $file) { - if (!empty($this->_files[$file]['validators'])) { - $validators += $this->_files[$file]['validators']; + if (!empty($this->files[$file]['validators'])) { + $validators += $this->files[$file]['validators']; } } $validators = array_unique($validators); $result = array(); foreach ($validators as $validator) { - $result[$validator] = $this->_validators[$validator]; + $result[$validator] = $this->validators[$validator]; } return $result; @@ -550,27 +555,27 @@ public function getValidators($files = null) * Remove an individual validator * * @param string $name - * @return \Zend\File\Transfer\Adapter\AbstractAdapter + * @return AbstractAdapter */ public function removeValidator($name) { - if (false === ($key = $this->_getValidatorIdentifier($name))) { + if (false === ($key = $this->getValidatorIdentifier($name))) { return $this; } - unset($this->_validators[$key]); - foreach (array_keys($this->_files) as $file) { - if (empty($this->_files[$file]['validators'])) { + unset($this->validators[$key]); + foreach (array_keys($this->files) as $file) { + if (empty($this->files[$file]['validators'])) { continue; } - $index = array_search($key, $this->_files[$file]['validators']); + $index = array_search($key, $this->files[$file]['validators']); if ($index === false) { continue; } - unset($this->_files[$file]['validators'][$index]); - $this->_files[$file]['validated'] = false; + unset($this->files[$file]['validators'][$index]); + $this->files[$file]['validated'] = false; } return $this; @@ -579,14 +584,14 @@ public function removeValidator($name) /** * Remove all validators * - * @return \Zend\File\Transfer\Adapter\AbstractAdapter + * @return AbstractAdapter */ public function clearValidators() { - $this->_validators = array(); - foreach (array_keys($this->_files) as $file) { - $this->_files[$file]['validators'] = array(); - $this->_files[$file]['validated'] = false; + $this->validators = array(); + foreach (array_keys($this->files) as $file) { + $this->files[$file]['validators'] = array(); + $this->files[$file]['validated'] = false; } return $this; @@ -599,24 +604,24 @@ public function clearValidators() * @param array $files (Optional) Files to set the options for */ public function setOptions($options = array(), $files = null) { - $file = $this->_getFiles($files, false, true); + $file = $this->getFiles($files, false, true); if (is_array($options)) { if (empty($file)) { - $this->_options = array_merge($this->_options, $options); + $this->options = array_merge($this->options, $options); } foreach ($options as $name => $value) { foreach ($file as $key => $content) { switch ($name) { case 'magicFile' : - $this->_files[$key]['options'][$name] = (string) $value; + $this->files[$key]['options'][$name] = (string) $value; break; case 'ignoreNoFile' : case 'useByteString' : case 'detectInfos' : - $this->_files[$key]['options'][$name] = (boolean) $value; + $this->files[$key]['options'][$name] = (boolean) $value; break; default: @@ -636,11 +641,11 @@ public function setOptions($options = array(), $files = null) { * @return array Options for given files */ public function getOptions($files = null) { - $file = $this->_getFiles($files, false, true); + $file = $this->getFiles($files, false, true); foreach ($file as $key => $content) { - if (isset($this->_files[$key]['options'])) { - $options[$key] = $this->_files[$key]['options']; + if (isset($this->files[$key]['options'])) { + $options[$key] = $this->files[$key]['options']; } else { $options[$key] = array(); } @@ -657,18 +662,18 @@ public function getOptions($files = null) { */ public function isValid($files = null) { - $check = $this->_getFiles($files, false, true); + $check = $this->getFiles($files, false, true); if (empty($check)) { return false; } $translator = $this->getTranslator(); - $this->_messages = array(); + $this->messages = array(); $break = false; foreach($check as $key => $content) { if (array_key_exists('validators', $content) && in_array('Zend\Validator\File\Count', $content['validators'])) { - $validator = $this->_validators['Zend\Validator\File\Count']; + $validator = $this->validators['Zend\Validator\File\Count']; $count = $content; if (empty($content['tmp_name'])) { continue; @@ -687,7 +692,7 @@ public function isValid($files = null) if (isset($count)) { if (!$validator->isValid($count['tmp_name'], $count)) { - $this->_messages += $validator->getMessages(); + $this->messages += $validator->getMessages(); } } @@ -699,7 +704,7 @@ public function isValid($files = null) if (array_key_exists('validators', $content)) { foreach ($content['validators'] as $class) { - $validator = $this->_validators[$class]; + $validator = $this->validators[$class]; if (method_exists($validator, 'setTranslator')) { $validator->setTranslator($translator); } @@ -723,7 +728,7 @@ public function isValid($files = null) break; } - if (($this->_break[$class]) and (count($fileerrors) > 0)) { + if (($this->break[$class]) and (count($fileerrors) > 0)) { $break = true; break; } @@ -731,18 +736,18 @@ public function isValid($files = null) } if (count($fileerrors) > 0) { - $this->_files[$key]['validated'] = false; + $this->files[$key]['validated'] = false; } else { - $this->_files[$key]['validated'] = true; + $this->files[$key]['validated'] = true; } - $this->_messages += $fileerrors; + $this->messages += $fileerrors; if ($break) { break; } } - if (count($this->_messages) > 0) { + if (count($this->messages) > 0) { return false; } @@ -756,7 +761,7 @@ public function isValid($files = null) */ public function getMessages() { - return $this->_messages; + return $this->messages; } /** @@ -766,7 +771,7 @@ public function getMessages() */ public function getErrors() { - return array_keys($this->_messages); + return array_keys($this->messages); } /** @@ -776,16 +781,17 @@ public function getErrors() */ public function hasErrors() { - return (!empty($this->_messages)); + return (!empty($this->messages)); } /** * Adds a new filter for this class * - * @param string|array $filter Type of filter to add - * @param string|array $options Options to set for the filter - * @param string|array $files Files to limit this filter to - * @return Zend_File_Transfer_Adapter + * @param string|Filter\Filter $filter Type of filter to add + * @param string|array $options Options to set for the filter + * @param string|array $files Files to limit this filter to + * @return AbstractAdapter + * @throws Exception\InvalidArgumentException for invalid type */ public function addFilter($filter, $options = null, $files = null) { @@ -798,10 +804,10 @@ public function addFilter($filter, $options = null, $files = null) throw new Exception\InvalidArgumentException('Invalid filter specified'); } - $this->_filters[$class] = $filter; - $files = $this->_getFiles($files, true, true); + $this->filters[$class] = $filter; + $files = $this->getFiles($files, true, true); foreach ($files as $file) { - $this->_files[$file]['filters'][] = $class; + $this->files[$file]['filters'][] = $class; } return $this; @@ -812,7 +818,7 @@ public function addFilter($filter, $options = null, $files = null) * * @param array $filters * @param string|array $files - * @return \Zend\File\Transfer\Adapter\AbstractAdapter + * @return AbstractAdapter */ public function addFilters(array $filters, $files = null) { @@ -854,9 +860,9 @@ public function addFilters(array $filters, $files = null) /** * Sets a filter for the class, erasing all previous set * - * @param string|array $filter Filter to set - * @param string|array $files Files to limit this filter to - * @return Zend_File_Transfer_Adapter + * @param array $filters Filter to set + * @param string|array $files Files to limit this filter to + * @return AbstractFilter */ public function setFilters(array $filters, $files = null) { @@ -872,21 +878,22 @@ public function setFilters(array $filters, $files = null) */ public function hasFilter($name) { - return (false !== $this->_getFilterIdentifier($name)); + return (false !== $this->getFilterIdentifier($name)); } /** * Retrieve individual filter * * @param string $name - * @return \Zend\Filter\Filter|null + * @return Filter\Filter|null */ public function getFilter($name) { - if (false === ($identifier = $this->_getFilterIdentifier($name))) { + if (false === ($identifier = $this->getFilterIdentifier($name))) { return null; } - return $this->_filters[$identifier]; + + return $this->filters[$identifier]; } /** @@ -894,26 +901,26 @@ public function getFilter($name) * * @param string|array $files (Optional) Returns the filter for this files * @return array List of set filters - * @throws \Zend\File\Transfer\Exception When file not found + * @throws Exception When file not found */ public function getFilters($files = null) { if ($files === null) { - return $this->_filters; + return $this->filters; } - $files = $this->_getFiles($files, true, true); + $files = $this->getFiles($files, true, true); $filters = array(); foreach ($files as $file) { - if (!empty($this->_files[$file]['filters'])) { - $filters += $this->_files[$file]['filters']; + if (!empty($this->files[$file]['filters'])) { + $filters += $this->files[$file]['filters']; } } $filters = array_unique($filters); $result = array(); foreach ($filters as $filter) { - $result[] = $this->_filters[$filter]; + $result[] = $this->filters[$filter]; } return $result; @@ -923,26 +930,26 @@ public function getFilters($files = null) * Remove an individual filter * * @param string $name - * @return \Zend\File\Transfer\Adapter\AbstractAdapter + * @return AbstractAdapter */ public function removeFilter($name) { - if (false === ($key = $this->_getFilterIdentifier($name))) { + if (false === ($key = $this->getFilterIdentifier($name))) { return $this; } - unset($this->_filters[$key]); - foreach (array_keys($this->_files) as $file) { - if (empty($this->_files[$file]['filters'])) { + unset($this->filters[$key]); + foreach (array_keys($this->files) as $file) { + if (empty($this->files[$file]['filters'])) { continue; } - $index = array_search($key, $this->_files[$file]['filters']); + $index = array_search($key, $this->files[$file]['filters']); if ($index === false) { continue; } - unset($this->_files[$file]['filters'][$index]); + unset($this->files[$file]['filters'][$index]); } return $this; } @@ -950,14 +957,15 @@ public function removeFilter($name) /** * Remove all filters * - * @return \Zend\File\Transfer\Adapter\AbstractAdapter + * @return AbstractAdapter */ public function clearFilters() { - $this->_filters = array(); - foreach (array_keys($this->_files) as $file) { - $this->_files[$file]['filters'] = array(); + $this->filters = array(); + foreach (array_keys($this->files) as $file) { + $this->files[$file]['filters'] = array(); } + return $this; } @@ -970,11 +978,11 @@ public function clearFilters() */ public function getFileName($file = null, $path = true) { - $files = $this->_getFiles($file, true, true); + $files = $this->getFiles($file, true, true); $result = array(); $directory = ""; foreach($files as $file) { - if (empty($this->_files[$file]['name'])) { + if (empty($this->files[$file]['name'])) { continue; } @@ -982,7 +990,7 @@ public function getFileName($file = null, $path = true) $directory = $this->getDestination($file) . DIRECTORY_SEPARATOR; } - $result[$file] = $directory . $this->_files[$file]['name']; + $result[$file] = $directory . $this->files[$file]['name']; } if (count($result) == 1) { @@ -1000,7 +1008,7 @@ public function getFileName($file = null, $path = true) */ public function getFileInfo($file = null) { - return $this->_getFiles($file); + return $this->getFiles($file); } /** @@ -1009,8 +1017,8 @@ public function getFileInfo($file = null) * @deprecated Will be changed to be a filter!!! * @param string $destination New destination directory * @param string|array $files Files to set the new destination for - * @return Zend_File_Transfer_Abstract - * @throws \Zend\File\Transfer\Exception when the given destination is not a directory or does not exist + * @return AbstractAdapter + * @throws Exception\InvalidArgumentException when the given destination is not a directory or does not exist */ public function setDestination($destination, $files = null) { @@ -1025,17 +1033,17 @@ public function setDestination($destination, $files = null) } if ($files === null) { - foreach ($this->_files as $file => $content) { - $this->_files[$file]['destination'] = $destination; + foreach ($this->files as $file => $content) { + $this->files[$file]['destination'] = $destination; } } else { - $files = $this->_getFiles($files, true, true); + $files = $this->getFiles($files, true, true); if (empty($files) and is_string($orig)) { - $this->_files[$orig]['destination'] = $destination; + $this->files[$orig]['destination'] = $destination; } foreach ($files as $file) { - $this->_files[$file]['destination'] = $destination; + $this->files[$file]['destination'] = $destination; } } @@ -1051,28 +1059,28 @@ public function setDestination($destination, $files = null) public function getDestination($files = null) { $orig = $files; - $files = $this->_getFiles($files, false, true); + $files = $this->getFiles($files, false, true); $destinations = array(); if (empty($files) and is_string($orig)) { - if (isset($this->_files[$orig]['destination'])) { - $destinations[$orig] = $this->_files[$orig]['destination']; + if (isset($this->files[$orig]['destination'])) { + $destinations[$orig] = $this->files[$orig]['destination']; } else { throw new Exception\InvalidArgumentException(sprintf('The file transfer adapter can not find "%s"', $orig)); } } foreach ($files as $key => $content) { - if (isset($this->_files[$key]['destination'])) { - $destinations[$key] = $this->_files[$key]['destination']; + if (isset($this->files[$key]['destination'])) { + $destinations[$key] = $this->files[$key]['destination']; } else { - $tmpdir = $this->_getTmpDir(); + $tmpdir = $this->getTmpDir(); $this->setDestination($tmpdir, $key); $destinations[$key] = $tmpdir; } } if (empty($destinations)) { - $destinations = $this->_getTmpDir(); + $destinations = $this->getTmpDir(); } else if (count($destinations) == 1) { $destinations = current($destinations); } @@ -1083,17 +1091,17 @@ public function getDestination($files = null) /** * Set translator object for localization * - * @param \Zend\Translator\Translator|null $translator - * @return Zend_File_Transfer_Abstract + * @param Translator\Translator|AbstractAdapter|null $translator + * @return AbstractAdapter */ public function setTranslator($translator = null) { if (null === $translator) { - $this->_translator = null; - } elseif ($translator instanceof \Zend\Translator\Adapter\AbstractAdapter) { - $this->_translator = $translator; - } elseif ($translator instanceof \Zend\Translator\Translator) { - $this->_translator = $translator->getAdapter(); + $this->translator = null; + } elseif ($translator instanceof AbstractAdapter) { + $this->translator = $translator; + } elseif ($translator instanceof Translator\Translator) { + $this->translator = $translator->getAdapter(); } else { throw new Exception\InvalidArgumentException('Invalid translator specified'); } @@ -1104,7 +1112,7 @@ public function setTranslator($translator = null) /** * Retrieve localization translator object * - * @return \Zend\Translator\Adapter\Adapter|null + * @return Translator\Adapter\Adapter|null */ public function getTranslator() { @@ -1112,18 +1120,18 @@ public function getTranslator() return null; } - return $this->_translator; + return $this->translator; } /** * Indicate whether or not translation should be disabled * * @param bool $flag - * @return Zend_File_Transfer_Abstract + * @return AbstractAdapter */ public function setDisableTranslator($flag) { - $this->_translatorDisabled = (bool) $flag; + $this->translatorDisabled = (bool) $flag; return $this; } @@ -1134,7 +1142,7 @@ public function setDisableTranslator($flag) */ public function translatorIsDisabled() { - return $this->_translatorDisabled; + return $this->translatorDisabled; } /** @@ -1143,7 +1151,7 @@ public function translatorIsDisabled() * @param string $hash Hash algorithm to use * @param string|array $files Files to return the hash for * @return string|array Hashstring - * @throws \Zend\File\Transfer\Exception On unknown hash algorithm + * @throws Exception\InvalidArgumentException On unknown hash algorithm */ public function getHash($hash = 'crc32', $files = null) { @@ -1151,7 +1159,7 @@ public function getHash($hash = 'crc32', $files = null) throw new Exception\InvalidArgumentException('Unknown hash algorithm'); } - $files = $this->_getFiles($files); + $files = $this->getFiles($files); $result = array(); foreach($files as $key => $value) { if (file_exists($value['name'])) { @@ -1173,18 +1181,18 @@ public function getHash($hash = 'crc32', $files = null) /** * Returns the real filesize of the file * - * @param string|array $files Files to get the filesize from - * @throws \Zend\File\Transfer\Exception When the file does not exist + * @param string|array $files Files to get the filesize from * @return string|array Filesize + * @throws Exception\InvalidArgumentException When the file does not exist */ public function getFileSize($files = null) { - $files = $this->_getFiles($files); + $files = $this->getFiles($files); $result = array(); foreach($files as $key => $value) { if (file_exists($value['name']) || file_exists($value['tmp_name'])) { if ($value['options']['useByteString']) { - $result[$key] = self::_toByteString($value['size']); + $result[$key] = self::toByteString($value['size']); } else { $result[$key] = $value['size']; } @@ -1208,7 +1216,7 @@ public function getFileSize($files = null) * @param array $value File infos * @return string Filesize of given file */ - protected function _detectFileSize($value) + protected function detectFileSize($value) { if (file_exists($value['name'])) { $result = sprintf("%u", @filesize($value['name'])); @@ -1226,12 +1234,12 @@ protected function _detectFileSize($value) * Uses fileinfo, when not available mime_magic and as last fallback a manual given mimetype * * @param string|array $files Files to get the mimetype from - * @throws \Zend\File\Transfer\Exception When the file does not exist * @return string|array MimeType + * @throws Exception\InvalidArgumentException When the file does not exist */ public function getMimeType($files = null) { - $files = $this->_getFiles($files); + $files = $this->getFiles($files); $result = array(); foreach($files as $key => $value) { if (file_exists($value['name']) || file_exists($value['tmp_name'])) { @@ -1256,7 +1264,7 @@ public function getMimeType($files = null) * @param array $value File infos * @return string Mimetype of given file */ - protected function _detectMimeType($value) + protected function detectMimeType($value) { if (file_exists($value['name'])) { $file = $value['name']; @@ -1301,7 +1309,7 @@ protected function _detectMimeType($value) * @param integer $size * @return string */ - protected static function _toByteString($size) + protected static function toByteString($size) { $sizes = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); for ($i=0; $size >= 1024 && $i < 9; $i++) { @@ -1317,26 +1325,26 @@ protected static function _toByteString($size) * @param string|array $files (Optional) Files to check * @return boolean False on error */ - protected function _filter($files = null) + protected function filter($files = null) { - $check = $this->_getFiles($files); + $check = $this->getFiles($files); foreach ($check as $name => $content) { if (array_key_exists('filters', $content)) { foreach ($content['filters'] as $class) { - $filter = $this->_filters[$class]; + $filter = $this->filters[$class]; try { $result = $filter->filter($this->getFileName($name)); - $this->_files[$name]['destination'] = dirname($result); - $this->_files[$name]['name'] = basename($result); + $this->files[$name]['destination'] = dirname($result); + $this->files[$name]['name'] = basename($result); } catch (Filter\Exception $e) { - $this->_messages += array($e->getMessage()); + $this->messages += array($e->getMessage()); } } } } - if (count($this->_messages) > 0) { + if (count($this->messages) > 0) { return false; } @@ -1347,11 +1355,11 @@ protected function _filter($files = null) * Determine system TMP directory and detect if we have read access * * @return string - * @throws \Zend\File\Transfer\Exception if unable to determine directory + * @throws Exception\RuntimeException if unable to determine directory */ - protected function _getTmpDir() + protected function getTmpDir() { - if (null === $this->_tmpDir) { + if (null === $this->tmpDir) { $tmpdir = array(); if (function_exists('sys_get_temp_dir')) { $tmpdir[] = sys_get_temp_dir(); @@ -1375,25 +1383,25 @@ protected function _getTmpDir() } foreach($tmpdir as $directory) { - if ($this->_isPathWriteable($directory)) { - $this->_tmpDir = $directory; + if ($this->isPathWriteable($directory)) { + $this->tmpDir = $directory; } } - if (empty($this->_tmpDir)) { + if (empty($this->tmpDir)) { // Attemp to detect by creating a temporary file $tempFile = tempnam(md5(uniqid(rand(), TRUE)), ''); if ($tempFile) { - $this->_tmpDir = realpath(dirname($tempFile)); + $this->tmpDir = realpath(dirname($tempFile)); unlink($tempFile); } else { throw new Exception\RuntimeException('Could not determine a temporary directory'); } } - $this->_tmpDir = rtrim($this->_tmpDir, "/\\"); + $this->tmpDir = rtrim($this->tmpDir, "/\\"); } - return $this->_tmpDir; + return $this->tmpDir; } /** @@ -1401,7 +1409,7 @@ protected function _getTmpDir() * * @param string $path */ - protected function _isPathWriteable($path) + protected function isPathWriteable($path) { $tempFile = rtrim($path, "/\\"); $tempFile .= '/' . 'test.1'; @@ -1428,9 +1436,9 @@ protected function _isPathWriteable($path) * @param boolean $names (Optional) Returns only names on true, else complete info * @param boolean $noexception (Optional) Allows throwing an exception, otherwise returns an empty array * @return array Found files - * @throws \Zend\File\Transfer\Exception On false filename + * @throws Exception\RuntimeException On false filename */ - protected function _getFiles($files, $names = false, $noexception = false) + protected function getFiles($files, $names = false, $noexception = false) { $check = array(); @@ -1441,7 +1449,7 @@ protected function _getFiles($files, $names = false, $noexception = false) if (is_array($files)) { foreach ($files as $find) { $found = array(); - foreach ($this->_files as $file => $content) { + foreach ($this->files as $file => $content) { if (!isset($content['name'])) { continue; } @@ -1473,13 +1481,13 @@ protected function _getFiles($files, $names = false, $noexception = false) } foreach ($found as $checked) { - $check[$checked] = $this->_files[$checked]; + $check[$checked] = $this->files[$checked]; } } } if ($files === null) { - $check = $this->_files; + $check = $this->files; $keys = array_keys($check); foreach ($keys as $key) { if (isset($check[$key]['multifiles'])) { @@ -1501,13 +1509,13 @@ protected function _getFiles($files, $names = false, $noexception = false) * @param string $name * @return string */ - protected function _getValidatorIdentifier($name) + protected function getValidatorIdentifier($name) { - if (array_key_exists($name, $this->_validators)) { + if (array_key_exists($name, $this->validators)) { return $name; } - foreach (array_keys($this->_validators) as $test) { + foreach (array_keys($this->validators) as $test) { if (preg_match('/' . preg_quote($name) . '$/i', $test)) { return $test; } @@ -1522,13 +1530,13 @@ protected function _getValidatorIdentifier($name) * @param string $name * @return string */ - protected function _getFilterIdentifier($name) + protected function getFilterIdentifier($name) { - if (array_key_exists($name, $this->_filters)) { + if (array_key_exists($name, $this->filters)) { return $name; } - foreach (array_keys($this->_filters) as $test) { + foreach (array_keys($this->filters) as $test) { if (preg_match('/' . preg_quote($name) . '$/i', $test)) { return $test; } diff --git a/library/Zend/File/Transfer/Adapter/Http.php b/library/Zend/File/Transfer/Adapter/Http.php index a4902523c50..6e8ae7ed0cb 100644 --- a/library/Zend/File/Transfer/Adapter/Http.php +++ b/library/Zend/File/Transfer/Adapter/Http.php @@ -19,17 +19,15 @@ */ namespace Zend\File\Transfer\Adapter; + use Zend\File\Transfer, Zend\File\Transfer\Exception, - Zend\ProgressBar\Adapter, - Zend\ProgressBar; + Zend\ProgressBar, + Zend\ProgressBar\Adapter; /** * File transfer adapter class for the HTTP protocol * - * @uses \Zend\File\Transfer\Adapter\AbstractAdapter - * @uses \Zend\File\Transfer\Exception - * @uses \Zend\ProgressBar\ProgressBar * @category Zend * @package Zend_File_Transfer * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) @@ -37,8 +35,8 @@ */ class Http extends AbstractAdapter { - protected static $_callbackApc = 'apc_fetch'; - protected static $_callbackUploadProgress = 'uploadprogress_get_info'; + protected static $callbackApc = 'apc_fetch'; + protected static $callbackUploadProgress = 'uploadprogress_get_info'; /** * Constructor for Http File Transfers @@ -52,16 +50,16 @@ public function __construct($options = array()) } $this->setOptions($options); - $this->_prepareFiles(); - $this->addValidator('Upload', false, $this->_files); + $this->prepareFiles(); + $this->addValidator('Upload', false, $this->files); } /** * Sets a validator for the class, erasing all previous set * - * @param string|array $validator Validator to set - * @param string|array $files Files to limit this validator to - * @return Zend_File_Transfer_Adapter + * @param array $validators Validator to set + * @param string|array $files Files to limit this validator to + * @return AbstractAdapter */ public function setValidators(array $validators, $files = null) { @@ -73,7 +71,7 @@ public function setValidators(array $validators, $files = null) * Remove an individual validator * * @param string $name - * @return \Zend\File\Transfer\Adapter\AbstractAdapter + * @return AbstractAdapter */ public function removeValidator($name) { @@ -88,12 +86,12 @@ public function removeValidator($name) * Remove an individual validator * * @param string $name - * @return \Zend\File\Transfer\Adapter\AbstractAdapter + * @return AbstractAdapter */ public function clearValidators() { parent::clearValidators(); - $this->addValidator('Upload', false, $this->_files); + $this->addValidator('Upload', false, $this->files); return $this; } @@ -102,12 +100,11 @@ public function clearValidators() * Send the file to the client (Download) * * @param string|array $options Options for the file(s) to send - * @return void - * @throws \Zend\File\Transfer\Exception Not implemented + * @throws Exception\BadMethodCallException Not implemented */ public function send($options = null) { - throw new Exception\RuntimeException('Method not implemented'); + throw new Exception\BadMethodCallException('Method not implemented'); } /** @@ -127,7 +124,7 @@ public function isValid($files = null) } // Workaround for a PHP error returning empty $_FILES when form data exceeds php settings - if (empty($this->_files) && ($content > 0)) { + if (empty($this->files) && ($content > 0)) { if (is_array($files)) { $files = current($files); } @@ -135,11 +132,11 @@ public function isValid($files = null) $temp = array($files => array( 'name' => $files, 'error' => 1)); - $validator = $this->_validators['Zend\Validator\File\Upload']; + $validator = $this->validators['Zend\Validator\File\Upload']; $validator->setTranslator($this->getTranslator()) ->setFiles($temp) ->isValid($files, null); - $this->_messages += $validator->getMessages(); + $this->messages += $validator->getMessages(); return false; } @@ -150,7 +147,7 @@ public function isValid($files = null) * Receive the file from the client (Upload) * * @param string|array $files (Optional) Files to receive - * @return bool + * @return boolean */ public function receive($files = null) { @@ -158,7 +155,7 @@ public function receive($files = null) return false; } - $check = $this->_getFiles($files); + $check = $this->getFiles($files); foreach ($check as $file => $content) { if (!$content['received']) { $directory = ''; @@ -179,38 +176,38 @@ public function receive($files = null) $filename = $directory . $filename; } - $key = array_search(get_class($rename), $this->_files[$file]['filters']); - unset($this->_files[$file]['filters'][$key]); + $key = array_search(get_class($rename), $this->files[$file]['filters']); + unset($this->files[$file]['filters'][$key]); } // Should never return false when it's tested by the upload validator if (!move_uploaded_file($content['tmp_name'], $filename)) { if ($content['options']['ignoreNoFile']) { - $this->_files[$file]['received'] = true; - $this->_files[$file]['filtered'] = true; + $this->files[$file]['received'] = true; + $this->files[$file]['filtered'] = true; continue; } - $this->_files[$file]['received'] = false; + $this->files[$file]['received'] = false; return false; } if ($rename !== null) { - $this->_files[$file]['destination'] = dirname($filename); - $this->_files[$file]['name'] = basename($filename); + $this->files[$file]['destination'] = dirname($filename); + $this->files[$file]['name'] = basename($filename); } - $this->_files[$file]['tmp_name'] = $filename; - $this->_files[$file]['received'] = true; + $this->files[$file]['tmp_name'] = $filename; + $this->files[$file]['received'] = true; } if (!$content['filtered']) { - if (!$this->_filter($file)) { - $this->_files[$file]['filtered'] = false; + if (!$this->filter($file)) { + $this->files[$file]['filtered'] = false; return false; } - $this->_files[$file]['filtered'] = true; + $this->files[$file]['filtered'] = true; } } @@ -221,23 +218,23 @@ public function receive($files = null) * Checks if the file was already sent * * @param string|array $file Files to check - * @return bool - * @throws \Zend\File\Transfer\Exception Not implemented + * @return boolean + * @throws Exception\BadMethodCallException Not implemented */ public function isSent($files = null) { - throw new Exception\RuntimeException('Method not implemented'); + throw new Exception\BadMethodCallException('Method not implemented'); } /** * Checks if the file was already received * * @param string|array $files (Optional) Files to check - * @return bool + * @return boolean */ public function isReceived($files = null) { - $files = $this->_getFiles($files, false, true); + $files = $this->getFiles($files, false, true); if (empty($files)) { return false; } @@ -255,11 +252,11 @@ public function isReceived($files = null) * Checks if the file was already filtered * * @param string|array $files (Optional) Files to check - * @return bool + * @return boolean */ public function isFiltered($files = null) { - $files = $this->_getFiles($files, false, true); + $files = $this->getFiles($files, false, true); if (empty($files)) { return false; } @@ -277,11 +274,11 @@ public function isFiltered($files = null) * Has a file been uploaded ? * * @param array|string|null $file - * @return bool + * @return boolean */ public function isUploaded($files = null) { - $files = $this->_getFiles($files, false, true); + $files = $this->getFiles($files, false, true); if (empty($files)) { return false; } @@ -298,13 +295,14 @@ public function isUploaded($files = null) /** * Returns the actual progress of file up-/downloads * - * @param string $id The upload to get the progress for + * @param string|array $id The upload to get the progress for * @return array|null + * @throws Exception\PhpEnvironmentException whether APC nor UploadProgress extension installed */ public static function getProgress($id = null) { - if (!function_exists('apc_fetch') and !function_exists('uploadprogress_get_info')) { - throw new Exception\PhpEnvironmentException('Neither APC nor uploadprogress extension installed'); + if (!self::isApcAvailable() && !self::isUploadProgressAvailable()) { + throw new Exception\PhpEnvironmentException('Neither APC nor UploadProgress extension installed'); } $session = 'Zend\File\Transfer\Adapter\Http\ProgressBar'; @@ -332,7 +330,7 @@ public static function getProgress($id = null) } } - if (!empty($id) && (($id instanceof Adapter\Adapter) || ($id instanceof ProgressBar\ProgressBar))) { + if (!empty($id) && (($id instanceof Adapter\AbstractAdapter) || ($id instanceof ProgressBar\ProgressBar))) { $adapter = $id; unset($id); } @@ -349,12 +347,12 @@ public static function getProgress($id = null) if (!empty($id)) { if (self::isApcAvailable()) { - $call = call_user_func(self::$_callbackApc, ini_get('apc.rfc1867_prefix') . $id); + $call = call_user_func(self::$callbackApc, ini_get('apc.rfc1867_prefix') . $id); if (is_array($call)) { $status = $call + $status; } } else if (self::isUploadProgressAvailable()) { - $call = call_user_func(self::$_callbackUploadProgress, $id); + $call = call_user_func(self::$callbackUploadProgress, $id); if (is_array($call)) { $status = $call + $status; $status['total'] = $status['bytes_total']; @@ -373,14 +371,14 @@ public static function getProgress($id = null) $status['done'] = true; $status['message'] = 'The upload has been canceled'; } else { - $status['message'] = self::_toByteString($status['current']) . " - " . self::_toByteString($status['total']); + $status['message'] = self::toByteString($status['current']) . " - " . self::toByteString($status['total']); } $status['id'] = $id; } if (isset($adapter) && isset($status['id'])) { - if ($adapter instanceof Adapter\Adapter) { + if ($adapter instanceof Adapter\AbstractAdapter) { $adapter = new ProgressBar\ProgressBar($adapter, 0, $status['total'], $session); } @@ -407,7 +405,7 @@ public static function getProgress($id = null) */ public static function isApcAvailable() { - return (bool) ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable(self::$_callbackApc); + return (bool) ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable(self::$callbackApc); } /** @@ -417,59 +415,58 @@ public static function isApcAvailable() */ public static function isUploadProgressAvailable() { - return is_callable(self::$_callbackUploadProgress); + return is_callable(self::$callbackUploadProgress); } /** * Prepare the $_FILES array to match the internal syntax of one file per entry * - * @param array $files - * @return array + * @return Http */ - protected function _prepareFiles() + protected function prepareFiles() { - $this->_files = array(); + $this->files = array(); foreach ($_FILES as $form => $content) { if (is_array($content['name'])) { foreach ($content as $param => $file) { foreach ($file as $number => $target) { - $this->_files[$form . '_' . $number . '_'][$param] = $target; - $this->_files[$form]['multifiles'][$number] = $form . '_' . $number . '_'; + $this->files[$form . '_' . $number . '_'][$param] = $target; + $this->files[$form]['multifiles'][$number] = $form . '_' . $number . '_'; } } - $this->_files[$form]['name'] = $form; - foreach($this->_files[$form]['multifiles'] as $key => $value) { - $this->_files[$value]['options'] = $this->_options; - $this->_files[$value]['validated'] = false; - $this->_files[$value]['received'] = false; - $this->_files[$value]['filtered'] = false; + $this->files[$form]['name'] = $form; + foreach($this->files[$form]['multifiles'] as $key => $value) { + $this->files[$value]['options'] = $this->options; + $this->files[$value]['validated'] = false; + $this->files[$value]['received'] = false; + $this->files[$value]['filtered'] = false; - $mimetype = $this->_detectMimeType($this->_files[$value]); - $this->_files[$value]['type'] = $mimetype; + $mimetype = $this->detectMimeType($this->files[$value]); + $this->files[$value]['type'] = $mimetype; - $filesize = $this->_detectFileSize($this->_files[$value]); - $this->_files[$value]['size'] = $filesize; + $filesize = $this->detectFileSize($this->files[$value]); + $this->files[$value]['size'] = $filesize; - if ($this->_options['detectInfos']) { + if ($this->options['detectInfos']) { $_FILES[$form]['type'][$key] = $mimetype; $_FILES[$form]['size'][$key] = $filesize; } } } else { - $this->_files[$form] = $content; - $this->_files[$form]['options'] = $this->_options; - $this->_files[$form]['validated'] = false; - $this->_files[$form]['received'] = false; - $this->_files[$form]['filtered'] = false; + $this->files[$form] = $content; + $this->files[$form]['options'] = $this->options; + $this->files[$form]['validated'] = false; + $this->files[$form]['received'] = false; + $this->files[$form]['filtered'] = false; - $mimetype = $this->_detectMimeType($this->_files[$form]); - $this->_files[$form]['type'] = $mimetype; + $mimetype = $this->detectMimeType($this->files[$form]); + $this->files[$form]['type'] = $mimetype; - $filesize = $this->_detectFileSize($this->_files[$form]); - $this->_files[$form]['size'] = $filesize; + $filesize = $this->detectFileSize($this->files[$form]); + $this->files[$form]['size'] = $filesize; - if ($this->_options['detectInfos']) { + if ($this->options['detectInfos']) { $_FILES[$form]['type'] = $mimetype; $_FILES[$form]['size'] = $filesize; } diff --git a/library/Zend/File/Transfer/Exception.php b/library/Zend/File/Transfer/Exception.php index 9a78c21c59c..f3b22dd6749 100644 --- a/library/Zend/File/Transfer/Exception.php +++ b/library/Zend/File/Transfer/Exception.php @@ -21,7 +21,7 @@ namespace Zend\File\Transfer; /** - * Exception class for Zend_File_Transfer + * Exception class for Zend\File\Transfer * * @category Zend * @package Zend_File_Transfer diff --git a/library/Zend/File/Transfer/Exception/BadMethodCallException.php b/library/Zend/File/Transfer/Exception/BadMethodCallException.php index 761f32c2918..399da848e79 100644 --- a/library/Zend/File/Transfer/Exception/BadMethodCallException.php +++ b/library/Zend/File/Transfer/Exception/BadMethodCallException.php @@ -1,7 +1,31 @@ _adapter[$direction] = new $adapter($options); - if (!$this->_adapter[$direction] instanceof Adapter\AbstractAdapter) { - throw new Exception\InvalidArgumentException("Adapter " . $adapter . " does not extend Zend_File_Transfer_Adapter_Abstract"); + $this->adapter[$direction] = new $adapter($options); + if (!$this->adapter[$direction] instanceof Adapter\AbstractAdapter) { + throw new Exception\InvalidArgumentException('Adapter ' . $adapter . ' does not extend Zend\File\Transfer\Adapter\AbstractAdapter'); } return $this; @@ -85,16 +84,16 @@ public function setAdapter($adapter, $direction = false, $options = array()) * @param boolean $direction On null, all directions are returned * On false, download direction is returned * On true, upload direction is returned - * @return array|Zend_File_Transfer_Adapter + * @return array|Transfer\Adapter */ public function getAdapter($direction = null) { if ($direction === null) { - return $this->_adapter; + return $this->adapter; } $direction = (integer) $direction; - return $this->_adapter[$direction]; + return $this->adapter[$direction]; } /** @@ -112,8 +111,8 @@ public function __call($method, array $options) $direction = 0; } - if (method_exists($this->_adapter[$direction], $method)) { - return call_user_func_array(array($this->_adapter[$direction], $method), $options); + if (method_exists($this->adapter[$direction], $method)) { + return call_user_func_array(array($this->adapter[$direction], $method), $options); } throw new Exception\BadMethodCallException("Unknown method '" . $method . "' called!"); diff --git a/tests/Zend/File/ClassFileLocatorTest.php b/tests/Zend/File/ClassFileLocatorTest.php index 225e682a96c..a73f80a5dd6 100644 --- a/tests/Zend/File/ClassFileLocatorTest.php +++ b/tests/Zend/File/ClassFileLocatorTest.php @@ -21,8 +21,7 @@ namespace ZendTest\File; -use Zend\File\ClassFileLocator, - PHPUnit_Framework_TestCase as TestCase; +use Zend\File\ClassFileLocator; /** * Test class for Zend\File\ClassFileLocator @@ -34,7 +33,7 @@ * @license http://framework.zend.com/license/new-bsd New BSD License * @group Zend_File */ -class ClassFileLocatorTest extends TestCase +class ClassFileLocatorTest extends \PHPUnit_Framework_TestCase { public function testConstructorThrowsInvalidArgumentExceptionForInvalidStringDirectory() diff --git a/tests/Zend/File/Transfer/Adapter/AbstractAdapterTestMockAdapter.php b/tests/Zend/File/Transfer/Adapter/AbstractAdapterTestMockAdapter.php new file mode 100644 index 00000000000..58a3cd9766f --- /dev/null +++ b/tests/Zend/File/Transfer/Adapter/AbstractAdapterTestMockAdapter.php @@ -0,0 +1,165 @@ +files = array( + 'foo' => array( + 'name' => 'foo.jpg', + 'type' => 'image/jpeg', + 'size' => 126976, + 'tmp_name' => '/tmp/489127ba5c89c', + 'options' => array('ignoreNoFile' => false, 'useByteString' => true, 'detectInfos' => true), + 'validated' => false, + 'received' => false, + 'filtered' => false, + ), + 'bar' => array( + 'name' => 'bar.png', + 'type' => 'image/png', + 'size' => 91136, + 'tmp_name' => '/tmp/489128284b51f', + 'options' => array('ignoreNoFile' => false, 'useByteString' => true), + 'validated' => false, + 'received' => false, + 'filtered' => false, + ), + 'baz' => array( + 'name' => 'baz.text', + 'type' => 'text/plain', + 'size' => 1172, + 'tmp_name' => $testfile, + 'options' => array('ignoreNoFile' => false, 'useByteString' => true), + 'validated' => false, + 'received' => false, + 'filtered' => false, + ), + 'file_0_' => array( + 'name' => 'foo.jpg', + 'type' => 'image/jpeg', + 'size' => 126976, + 'tmp_name' => '/tmp/489127ba5c89c', + 'options' => array('ignoreNoFile' => false, 'useByteString' => true), + 'validated' => false, + 'received' => false, + 'filtered' => false, + ), + 'file_1_' => array( + 'name' => 'baz.text', + 'type' => 'text/plain', + 'size' => 1172, + 'tmp_name' => $testfile, + 'options' => array('ignoreNoFile' => false, 'useByteString' => true), + 'validated' => false, + 'received' => false, + 'filtered' => false, + ), + 'file' => array( + 'name' => 'foo.jpg', + 'multifiles' => array(0 => 'file_0_', 1 => 'file_1_') + ), + ); + } + + public function send($options = null) + { + return; + } + + public function receive($options = null) + { + $this->received = true; + return; + } + + public function isSent($file = null) + { + return false; + } + + public function isReceived($file = null) + { + return $this->received; + } + + public function isUploaded($files = null) + { + return true; + } + + public function isFiltered($files = null) + { + return true; + } + + public static function getProgress() + { + return; + } + + public function getTmpDir() + { + $this->tmpDir = parent::getTmpDir(); + } + + public function isPathWriteable($path) + { + return parent::isPathWriteable($path); + } + + public function addInvalidFile() + { + $this->files += array( + 'test' => array( + 'name' => 'test.txt', + 'type' => 'image/jpeg', + 'size' => 0, + 'tmp_name' => '', + 'options' => array('ignoreNoFile' => true, 'useByteString' => true), + 'validated' => false, + 'received' => false, + 'filtered' => false, + ) + ); + } + +} diff --git a/tests/Zend/File/Transfer/Adapter/AbstractTest.php b/tests/Zend/File/Transfer/Adapter/AbstractTest.php index 0e20668c779..af1cb242309 100644 --- a/tests/Zend/File/Transfer/Adapter/AbstractTest.php +++ b/tests/Zend/File/Transfer/Adapter/AbstractTest.php @@ -21,14 +21,15 @@ namespace ZendTest\File\Transfer\Adapter; -use Zend\Loader\PrefixPathLoader, - Zend\Loader\ShortNameLocator, - Zend\Validator\File, +use Zend\File, Zend\Filter, - Zend\File\Transfer; + Zend\Filter\Word, + Zend\Loader, + Zend\Validator, + Zend\Validator\File as FileValidator; /** - * Test class for Zend_File_Transfer_Adapter_Abstract + * Test class for Zend\File\Transfer\Adapter\AbstractAdapter * * @category Zend * @package Zend_File @@ -47,7 +48,7 @@ class AbstractTest extends \PHPUnit_Framework_TestCase */ public function setUp() { - $this->adapter = new AbstractTestMockAdapter(); + $this->adapter = new AbstractAdapterTestMockAdapter(); } /** @@ -69,19 +70,19 @@ public function testAdapterShouldThrowExceptionWhenRetrievingPluginLoaderOfInval public function testAdapterShouldHavePluginLoaderForValidators() { $loader = $this->adapter->getPluginLoader('validator'); - $this->assertTrue($loader instanceof PrefixPathLoader); + $this->assertTrue($loader instanceof Loader\PrefixPathLoader); } public function testAdapterShouldAllowAddingCustomPluginLoader() { - $loader = new PrefixPathLoader(); + $loader = new Loader\PrefixPathLoader(); $this->adapter->setPluginLoader($loader, 'filter'); $this->assertSame($loader, $this->adapter->getPluginLoader('filter')); } public function testAddingInvalidPluginLoaderTypeToAdapterShouldRaiseException() { - $loader = new PrefixPathLoader(); + $loader = new Loader\PrefixPathLoader(); $this->setExpectedException('Zend\File\Transfer\Exception\InvalidArgumentException', 'Invalid type "BOGUS" provided to setPluginLoader'); $this->adapter->setPluginLoader($loader, 'bogus'); @@ -150,7 +151,7 @@ public function testValidatorPluginLoaderShouldRegisterPathsForBaseAndFileValida public function testAdapterShouldAllowAddingValidatorInstance() { - $validator = new File\Count(array('min' => 1, 'max' => 1)); + $validator = new FileValidator\Count(array('min' => 1, 'max' => 1)); $this->adapter->addValidator($validator); $test = $this->adapter->getValidator('Zend\Validator\File\Count'); $this->assertSame($validator, $test); @@ -160,7 +161,7 @@ public function testAdapterShouldAllowAddingValidatorViaPluginLoader() { $this->adapter->addValidator('Count', false, array('min' => 1, 'max' => 1)); $test = $this->adapter->getValidator('Count'); - $this->assertTrue($test instanceof File\Count); + $this->assertTrue($test instanceof FileValidator\Count); } @@ -176,20 +177,20 @@ public function testAdapterShouldAllowAddingMultipleValidatorsAtOnceUsingBothIns 'count' => array('min' => 1, 'max' => 1), 'Exists' => 'C:\temp', array('validator' => 'Upload', 'options' => array(realpath(__FILE__))), - new File\Extension('jpg'), + new FileValidator\Extension('jpg'), ); $this->adapter->addValidators($validators); $test = $this->adapter->getValidators(); $this->assertTrue(is_array($test)); $this->assertEquals(4, count($test), var_export($test, 1)); $count = array_shift($test); - $this->assertTrue($count instanceof File\Count); + $this->assertTrue($count instanceof FileValidator\Count); $exists = array_shift($test); - $this->assertTrue($exists instanceof File\Exists); + $this->assertTrue($exists instanceof FileValidator\Exists); $size = array_shift($test); - $this->assertTrue($size instanceof File\Upload); + $this->assertTrue($size instanceof FileValidator\Upload); $ext = array_shift($test); - $this->assertTrue($ext instanceof File\Extension); + $this->assertTrue($ext instanceof FileValidator\Extension); $orig = array_pop($validators); $this->assertSame($orig, $ext); } @@ -205,15 +206,15 @@ public function testAdapterShouldAllowPullingValidatorsByFile() $validators = $this->adapter->getValidators('foo'); $this->assertEquals(1, count($validators)); $validator = array_shift($validators); - $this->assertTrue($validator instanceof \Zend\Validator\Alpha); + $this->assertTrue($validator instanceof Validator\Alpha); } public function testCallingSetValidatorsOnAdapterShouldOverwriteExistingValidators() { $this->testAdapterShouldAllowAddingMultipleValidatorsAtOnceUsingBothInstancesAndPluginLoader(); $validators = array( - new File\Count(1), - new File\Extension('jpg'), + new FileValidator\Count(1), + new FileValidator\Extension('jpg'), ); $this->adapter->setValidators($validators); $test = $this->adapter->getValidators(); @@ -224,14 +225,14 @@ public function testAdapterShouldAllowRetrievingValidatorInstancesByClassName() { $this->testAdapterShouldAllowAddingMultipleValidatorsAtOnceUsingBothInstancesAndPluginLoader(); $ext = $this->adapter->getValidator('Zend\Validator\File\Extension'); - $this->assertTrue($ext instanceof File\Extension); + $this->assertTrue($ext instanceof FileValidator\Extension); } public function testAdapterShouldAllowRetrievingValidatorInstancesByPluginName() { $this->testAdapterShouldAllowAddingMultipleValidatorsAtOnceUsingBothInstancesAndPluginLoader(); $count = $this->adapter->getValidator('Count'); - $this->assertTrue($count instanceof File\Count); + $this->assertTrue($count instanceof FileValidator\Count); } public function testAdapterShouldAllowRetrievingAllValidatorsAtOnce() @@ -241,7 +242,7 @@ public function testAdapterShouldAllowRetrievingAllValidatorsAtOnce() $this->assertTrue(is_array($validators)); $this->assertEquals(4, count($validators)); foreach ($validators as $validator) { - $this->assertTrue($validator instanceof \Zend\Validator\Validator); + $this->assertTrue($validator instanceof Validator\Validator); } } @@ -336,7 +337,7 @@ public function testErrorCodesShouldBePopulatedAfterInvalidTransfer() public function testAdapterShouldHavePluginLoaderForFilters() { $loader = $this->adapter->getPluginLoader('filter'); - $this->assertTrue($loader instanceof ShortNameLocator); + $this->assertTrue($loader instanceof Loader\ShortNameLocator); } public function testFilterPluginLoaderShouldRegisterPathsForBaseAndFileFiltersByDefault() @@ -367,7 +368,7 @@ public function testAdapterShouldAllowAddingFilterViaPluginLoader() public function testAdapterhShouldRaiseExceptionWhenAddingInvalidFilterType() { $this->setExpectedException('Zend\File\Transfer\Exception\InvalidArgumentException', 'Invalid filter specified'); - $this->adapter->addFilter(new File\Extension('jpg')); + $this->adapter->addFilter(new FileValidator\Extension('jpg')); } public function testAdapterShouldAllowAddingMultipleFiltersAtOnceUsingBothInstancesAndPluginLoader() @@ -382,7 +383,7 @@ public function testAdapterShouldAllowAddingMultipleFiltersAtOnceUsingBothInstan $this->assertTrue(is_array($test)); $this->assertEquals(3, count($test), var_export($test, 1)); $count = array_shift($test); - $this->assertTrue($count instanceof \Zend\Filter\Word\SeparatorToCamelCase); + $this->assertTrue($count instanceof Word\SeparatorToCamelCase); $size = array_shift($test); $this->assertTrue($size instanceof Filter\Alpha); $ext = array_shift($test); @@ -609,22 +610,22 @@ public function testIgnoreHashValue() public function testEmptyTempDirectoryDetection() { - $this->adapter->_tmpDir = ""; - $this->assertTrue(empty($this->adapter->_tmpDir), "Empty temporary directory"); + $this->adapter->tmpDir = ""; + $this->assertTrue(empty($this->adapter->tmpDir), "Empty temporary directory"); } public function testTempDirectoryDetection() { $this->adapter->getTmpDir(); - $this->assertTrue(!empty($this->adapter->_tmpDir), "Temporary directory filled"); + $this->assertTrue(!empty($this->adapter->tmpDir), "Temporary directory filled"); } public function testTemporaryDirectoryAccessDetection() { - $this->adapter->_tmpDir = "."; + $this->adapter->tmpDir = "."; $path = "/NoPath/To/File"; $this->assertFalse($this->adapter->isPathWriteable($path)); - $this->assertTrue($this->adapter->isPathWriteable($this->adapter->_tmpDir)); + $this->assertTrue($this->adapter->isPathWriteable($this->adapter->tmpDir)); } public function testFileSizeButNoFileFound() @@ -642,9 +643,10 @@ public function testIgnoreFileSize() public function testFileSizeByTmpName() { + $expectedSize = sprintf("%.2fkB", 1.14); $options = $this->adapter->getOptions(); $this->assertTrue($options['baz']['useByteString']); - $this->assertEquals('1.14kB', $this->adapter->getFileSize('baz.text')); + $this->assertEquals($expectedSize, $this->adapter->getFileSize('baz.text')); $this->adapter->setOptions(array('useByteString' => false)); $options = $this->adapter->getOptions(); $this->assertFalse($options['baz']['useByteString']); @@ -671,7 +673,7 @@ public function testMimeTypeByTmpName() public function testSetOwnErrorMessage() { - $this->adapter->addValidator('Count', false, array('min' => 5, 'max' => 5, 'messages' => array(File\Count::TOO_FEW => 'Zu wenige'))); + $this->adapter->addValidator('Count', false, array('min' => 5, 'max' => 5, 'messages' => array(FileValidator\Count::TOO_FEW => 'Zu wenige'))); $this->assertFalse($this->adapter->isValid('foo')); $message = $this->adapter->getMessages(); $this->assertContains('Zu wenige', $message); @@ -709,8 +711,8 @@ public function testAdapterShouldAllowAddingMultipleValidatorsAtOnceUsingBothIns { $validators = array( array('MimeType', true, array('image/jpeg')), // no files - array('FilesSize', true, array('max' => '1MB', 'messages' => 'файл больше 1MБ')), // no files - array('Count', true, array('min' => 1, 'max' => '1', 'messages' => 'файл не 1'), 'bar'), // 'bar' from config + array('FilesSize', true, array('max' => '1MB', 'message' => 'файл больше 1MБ')), // no files + array('Count', true, array('min' => 1, 'max' => '1', 'message' => 'файл не 1'), 'bar'), // 'bar' from config array('MimeType', true, array('image/jpeg'), 'bar'), // 'bar' from config ); @@ -723,16 +725,16 @@ public function testAdapterShouldAllowAddingMultipleValidatorsAtOnceUsingBothIns $test = $this->adapter->getValidators('foo'); $this->assertEquals(2, count($test)); $mimeType = array_shift($test); - $this->assertTrue($mimeType instanceof File\MimeType); + $this->assertTrue($mimeType instanceof FileValidator\MimeType); $filesSize = array_shift($test); - $this->assertTrue($filesSize instanceof File\FilesSize); + $this->assertTrue($filesSize instanceof FileValidator\FilesSize); $test = $this->adapter->getValidators('bar'); $this->assertEquals(2, count($test)); $filesSize = array_shift($test); - $this->assertTrue($filesSize instanceof File\Count); + $this->assertTrue($filesSize instanceof FileValidator\Count); $mimeType = array_shift($test); - $this->assertTrue($mimeType instanceof File\MimeType); + $this->assertTrue($mimeType instanceof FileValidator\MimeType); $test = $this->adapter->getValidators('baz'); $this->assertEquals(0, count($test)); @@ -758,134 +760,3 @@ public function testSettingAndRetrievingDetectInfosOption() , $this->adapter->getOptions('foo')); } } - -class AbstractTestMockAdapter extends \Zend\File\Transfer\Adapter\AbstractAdapter -{ - public $received = false; - - public $_tmpDir; - - public function __construct() - { - $testfile = __DIR__ . '/_files/test.txt'; - $this->_files = array( - 'foo' => array( - 'name' => 'foo.jpg', - 'type' => 'image/jpeg', - 'size' => 126976, - 'tmp_name' => '/tmp/489127ba5c89c', - 'options' => array('ignoreNoFile' => false, 'useByteString' => true, 'detectInfos' => true), - 'validated' => false, - 'received' => false, - 'filtered' => false, - ), - 'bar' => array( - 'name' => 'bar.png', - 'type' => 'image/png', - 'size' => 91136, - 'tmp_name' => '/tmp/489128284b51f', - 'options' => array('ignoreNoFile' => false, 'useByteString' => true), - 'validated' => false, - 'received' => false, - 'filtered' => false, - ), - 'baz' => array( - 'name' => 'baz.text', - 'type' => 'text/plain', - 'size' => 1172, - 'tmp_name' => $testfile, - 'options' => array('ignoreNoFile' => false, 'useByteString' => true), - 'validated' => false, - 'received' => false, - 'filtered' => false, - ), - 'file_0_' => array( - 'name' => 'foo.jpg', - 'type' => 'image/jpeg', - 'size' => 126976, - 'tmp_name' => '/tmp/489127ba5c89c', - 'options' => array('ignoreNoFile' => false, 'useByteString' => true), - 'validated' => false, - 'received' => false, - 'filtered' => false, - ), - 'file_1_' => array( - 'name' => 'baz.text', - 'type' => 'text/plain', - 'size' => 1172, - 'tmp_name' => $testfile, - 'options' => array('ignoreNoFile' => false, 'useByteString' => true), - 'validated' => false, - 'received' => false, - 'filtered' => false, - ), - 'file' => array( - 'name' => 'foo.jpg', - 'multifiles' => array(0 => 'file_0_', 1 => 'file_1_') - ), - ); - } - - public function send($options = null) - { - return; - } - - public function receive($options = null) - { - $this->received = true; - return; - } - - public function isSent($file = null) - { - return false; - } - - public function isReceived($file = null) - { - return $this->received; - } - - public function isUploaded($files = null) - { - return true; - } - - public function isFiltered($files = null) - { - return true; - } - - public static function getProgress() - { - return; - } - - public function getTmpDir() - { - $this->_tmpDir = parent::_getTmpDir(); - } - - public function isPathWriteable($path) - { - return parent::_isPathWriteable($path); - } - - public function addInvalidFile() - { - $this->_files += array( - 'test' => array( - 'name' => 'test.txt', - 'type' => 'image/jpeg', - 'size' => 0, - 'tmp_name' => '', - 'options' => array('ignoreNoFile' => true, 'useByteString' => true), - 'validated' => false, - 'received' => false, - 'filtered' => false, - ) - ); - } - -} diff --git a/tests/Zend/File/Transfer/Adapter/HttpTest.php b/tests/Zend/File/Transfer/Adapter/HttpTest.php index b31051078d3..55be712bd43 100644 --- a/tests/Zend/File/Transfer/Adapter/HttpTest.php +++ b/tests/Zend/File/Transfer/Adapter/HttpTest.php @@ -21,10 +21,15 @@ */ namespace ZendTest\File\Transfer\Adapter; -use Zend\ProgressBar; + +use Zend\File\Transfer\Adapter, + Zend\File\Transfer\Exception, + Zend\ProgressBar, + Zend\ProgressBar\Adapter as AdapterProgressBar, + Zend\Validator\File as FileValidator; /** - * Test class for Zend_File_Transfer_Adapter_Http + * Test class for Zend\File\Transfer\Adapter\Http * * @category Zend * @package Zend_File @@ -51,7 +56,7 @@ public function setUp() 'size' => 8, 'tmp_name' => __DIR__ . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'test.txt', 'error' => 0)); - $this->adapter = new HTTPTestMockAdapter(); + $this->adapter = new HttpTestMockAdapter(); } /** @@ -73,23 +78,23 @@ public function testEmptyAdapter() public function testAutoSetUploadValidator() { $validators = array( - new \Zend\Validator\File\Count(1), - new \Zend\Validator\File\Extension('jpg'), + new FileValidator\Count(1), + new FileValidator\Extension('jpg'), ); $this->adapter->setValidators($validators); $test = $this->adapter->getValidator('Upload'); - $this->assertTrue($test instanceof \Zend\Validator\File\Upload); + $this->assertTrue($test instanceof FileValidator\Upload); } public function testSendingFiles() { - $this->setExpectedException('Zend\File\Transfer\Exception\RuntimeException', 'not implemented'); + $this->setExpectedException('Zend\File\Transfer\Exception\BadMethodCallException', 'not implemented'); $this->adapter->send(); } public function testFileIsSent() { - $this->setExpectedException('Zend\File\Transfer\Exception\RuntimeException', 'not implemented'); + $this->setExpectedException('Zend\File\Transfer\Exception\BadMethodCallException', 'not implemented'); $this->adapter->isSent(); } @@ -119,7 +124,7 @@ public function testReceiveUnknownFile() { try { $this->assertFalse($this->adapter->receive('unknownFile')); - } catch (\Zend\File\Transfer\Exception $e) { + } catch (Exception $e) { $this->assertContains('not find', $e->getMessage()); } } @@ -133,7 +138,7 @@ public function testReceiveValidatedFile() 'size' => 8, 'tmp_name' => 'unknown.txt', 'error' => 0)); - $adapter = new HTTPTestMockAdapter(); + $adapter = new HttpTestMockAdapter(); $this->assertFalse($adapter->receive()); } @@ -183,7 +188,7 @@ public function testMultiFiles() 'error' => array( 0 => 0, 1 => 0))); - $adapter = new HTTPTestMockAdapter(); + $adapter = new HttpTestMockAdapter(); $adapter->setOptions(array('ignoreNoFile' => true)); $this->assertTrue($adapter->receive('exe')); $this->assertEquals( @@ -194,36 +199,33 @@ public function testMultiFiles() public function testNoUploadInProgress() { - if (!(ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable('apc_fetch')) && - !is_callable('uploadprogress_get_info')) { + if (!Adapter\Http::isApcAvailable() && !Adapter\Http::isUploadProgressAvailable()) { $this->markTestSkipped('Whether APC nor UploadExtension available'); - return; } - $status = HTTPTestMockAdapter::getProgress(); + $status = HttpTestMockAdapter::getProgress(); $this->assertContains('No upload in progress', $status); } public function testUploadProgressFailure() { - if (!(ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable('apc_fetch')) && - !is_callable('uploadprogress_get_info')) { + if (!Adapter\Http::isApcAvailable() && !Adapter\Http::isUploadProgressAvailable()) { $this->markTestSkipped('Whether APC nor UploadExtension available'); - return; } $_GET['progress_key'] = 'mykey'; - $status = HTTPTestMockAdapter::getProgress(); + $status = HttpTestMockAdapter::getProgress(); $this->assertEquals(array( 'total' => 100, 'current' => 100, 'rate' => 10, 'id' => 'mykey', 'done' => false, - 'message' => '100B - 100B'), $status); + 'message' => '100B - 100B' + ), $status); $this->adapter->switchApcToUP(); - $status = HTTPTestMockAdapter::getProgress($status); + $status = HttpTestMockAdapter::getProgress($status); $this->assertEquals(array( 'total' => 100, 'bytes_total' => 100, @@ -234,22 +236,20 @@ public function testUploadProgressFailure() 'cancel_upload' => true, 'message' => 'The upload has been canceled', 'done' => true, - 'id' => 'mykey'), $status); - + 'id' => 'mykey' + ), $status); } public function testUploadProgressAdapter() { - if (!(ini_get('apc.enabled') && (bool) ini_get('apc.rfc1867') && is_callable('apc_fetch')) && - !is_callable('uploadprogress_get_info')) { + if (!Adapter\Http::isApcAvailable() && !Adapter\Http::isUploadProgressAvailable()) { $this->markTestSkipped('Whether APC nor UploadExtension available'); - return; } $_GET['progress_key'] = 'mykey'; - $adapter = new \Zend\ProgressBar\Adapter\Console(); + $adapter = new AdapterProgressBar\Console(); $status = array('progress' => $adapter, 'session' => 'upload'); - $status = HTTPTestMockAdapter::getProgress($status); + $status = HttpTestMockAdapter::getProgress($status); $this->assertTrue(array_key_exists('total', $status)); $this->assertTrue(array_key_exists('current', $status)); $this->assertTrue(array_key_exists('rate', $status)); @@ -259,7 +259,7 @@ public function testUploadProgressAdapter() $this->assertTrue($status['progress'] instanceof ProgressBar\ProgressBar); $this->adapter->switchApcToUP(); - $status = HTTPTestMockAdapter::getProgress($status); + $status = HttpTestMockAdapter::getProgress($status); $this->assertTrue(array_key_exists('total', $status)); $this->assertTrue(array_key_exists('current', $status)); $this->assertTrue(array_key_exists('rate', $status)); @@ -274,50 +274,8 @@ public function testValidationOfPhpExtendsFormError() $_SERVER['CONTENT_LENGTH'] = 10; $_FILES = array(); - $adapter = new HTTPTestMockAdapter(); + $adapter = new HttpTestMockAdapter(); $this->assertFalse($adapter->isValidParent()); $this->assertContains('exceeds the defined ini size', current($adapter->getMessages())); } } - -class HTTPTestMockAdapter extends \Zend\File\Transfer\Adapter\Http -{ - public function __construct() - { - self::$_callbackApc = array('HTTPTestMockAdapter', 'apcTest'); - parent::__construct(); - } - - public function isValid($files = null) - { - return true; - } - - public function isValidParent($files = null) - { - return parent::isValid($files); - } - - public static function isApcAvailable() - { - return true; - } - - public static function apcTest($id) - { - return array('total' => 100, 'current' => 100, 'rate' => 10); - } - - public static function uPTest($id) - { - return array('bytes_total' => 100, 'bytes_uploaded' => 100, 'speed_average' => 10, 'cancel_upload' => true); - } - - public function switchApcToUP() - { - self::$_callbackApc = null; - self::$_callbackUploadProgress = array('HTTPTestMockAdapter', 'uPTest'); - } - - -} diff --git a/tests/Zend/File/Transfer/Adapter/HttpTestMockAdapter.php b/tests/Zend/File/Transfer/Adapter/HttpTestMockAdapter.php new file mode 100644 index 00000000000..503028c524a --- /dev/null +++ b/tests/Zend/File/Transfer/Adapter/HttpTestMockAdapter.php @@ -0,0 +1,74 @@ + 100, 'current' => 100, 'rate' => 10); + } + + public static function uPTest($id) + { + return array('bytes_total' => 100, 'bytes_uploaded' => 100, 'speed_average' => 10, 'cancel_upload' => true); + } + + public function switchApcToUP() + { + self::$callbackApc = null; + self::$callbackUploadProgress = array('ZendTest\File\Transfer\Adapter\HttpTestMockAdapter', 'uPTest'); + } +} \ No newline at end of file