Skip to content

Commit

Permalink
- Nette::Collections rewritten to be ArrayObject based (better perfor…
Browse files Browse the repository at this point in the history
…mance and integration with PHP core)

- added Environment::exportConstant()
- Environment::loadConfig is able to define constants
- Nette::Web::HttpRequest::getQuery(), getPost(), getFiles() and getCookies() returns Nette::Collection::Hashtable
  • Loading branch information
dg committed May 14, 2008
1 parent 7a65855 commit ee86c6e
Show file tree
Hide file tree
Showing 55 changed files with 1,101 additions and 730 deletions.
10 changes: 5 additions & 5 deletions Nette/Callback.php
Expand Up @@ -26,7 +26,7 @@


/**
* Immutable encapsulation of pseudo-type callback
* Immutable encapsulation of pseudo-type callback.
*
* @author David Grudl
* @copyright Copyright (c) 2004, 2008 David Grudl
Expand All @@ -52,7 +52,7 @@ public function __construct($a, $b = NULL)


/**
* Verifies that user function is callable
* Verifies that user function is callable.
* @return bool
*/
public function isCallable()
Expand All @@ -63,7 +63,7 @@ public function isCallable()


/**
* Calls a user function
* Calls a user function.
* @return mixed
*/
public function invoke()
Expand All @@ -75,7 +75,7 @@ public function invoke()


/**
* Calls a user function
* Calls a user function.
* @param array
* @return mixed
*/
Expand All @@ -87,7 +87,7 @@ public function invokeArgs(array $args)


/**
* Returns native PHP callback
* Returns native PHP callback.
* @return callback
*/
public function getNative()
Expand Down
28 changes: 17 additions & 11 deletions Nette/Collections/ArrayList.php
Expand Up @@ -51,12 +51,14 @@ class ArrayList extends Collection implements IList
public function insertAt($index, $item)
{
$index -= $this->base;
if ($index < 0 || $index > count($this->data)) {
if ($index < 0 || $index > count($this)) {
throw new /*::*/ArgumentOutOfRangeException;
}

$this->beforeAdd($item);
array_splice($this->data, (int) $index, 0, array($item));
$data = $this->getArrayCopy();
array_splice($data, (int) $index, 0, array($item));
$this->setArray($data);
return TRUE;
}

Expand All @@ -76,7 +78,9 @@ public function remove($item)
if ($index === FALSE) {
return FALSE;
} else {
array_splice($this->data, $index, 1);
$data = $this->getArrayCopy();
array_splice($data, $index, 1);
$this->setArray($data);
return TRUE;
}
}
Expand Down Expand Up @@ -113,14 +117,14 @@ public function offsetSet($index, $item)
$this->beforeAdd($item);

if ($index === NULL) { // append
$this->data[] = $item;
parent::offsetSet(NULL, $item);

} else { // replace
$index -= $this->base;
if ($index < 0 || $index >= count($this->data)) {
if ($index < 0 || $index >= count($this)) {
throw new /*::*/ArgumentOutOfRangeException;
}
$this->data[$index] = $item;
parent::offsetSet($index, $item);
}
}

Expand All @@ -135,11 +139,11 @@ public function offsetSet($index, $item)
public function offsetGet($index)
{
$index -= $this->base;
if ($index < 0 || $index >= count($this->data)) {
if ($index < 0 || $index >= count($this)) {
throw new /*::*/ArgumentOutOfRangeException;
}

return $this->data[$index];
return parent::offsetGet($index);
}


Expand All @@ -152,7 +156,7 @@ public function offsetGet($index)
public function offsetExists($index)
{
$index -= $this->base;
return $index >= 0 && $index < count($this->data);
return $index >= 0 && $index < count($this);
}


Expand All @@ -166,12 +170,14 @@ public function offsetExists($index)
public function offsetUnset($index)
{
$index -= $this->base;
if ($index < 0 || $index >= count($this->data)) {
if ($index < 0 || $index >= count($this)) {
throw new /*::*/ArgumentOutOfRangeException;
}

$this->beforeRemove();
array_splice($this->data, (int) $index, 1);
$data = $this->getArrayCopy();
array_splice($data, (int) $index, 1);
$this->setArray($data);
}

}
186 changes: 126 additions & 60 deletions Nette/Collections/Collection.php
Expand Up @@ -21,25 +21,20 @@



require_once dirname(__FILE__) . '/../Object.php';

require_once dirname(__FILE__) . '/../Collections/ICollection.php';



/**
* Provides the base class for a generic collection.
* SPL ArrayObject customization.
*
* @author David Grudl
* @copyright Copyright (c) 2004, 2008 David Grudl
* @package Nette::Collections
* @version $Revision$ $Date$
*/
class Collection extends /*Nette::*/Object implements ICollection
abstract class Collection extends /*::*/ArrayObject implements ICollection
{
/** @var array of objects */
protected $data = array();

/** @var string type (class, interface, PHP type) */
protected $itemType;

Expand Down Expand Up @@ -83,21 +78,6 @@ public function setReadOnly()



/**
* Appends the specified element to the end of this collection.
* @param mixed
* @return bool true if this collection changed as a result of the call
* @throws ::InvalidArgumentException, ::NotSupportedException
*/
public function add($item)
{
$this->beforeAdd($item);
$this->data[] = $item;
return TRUE;
}



/**
* Removes the first occurrence of the specified element.
* @param mixed
Expand All @@ -111,7 +91,7 @@ public function remove($item)
if ($index === FALSE) {
return FALSE;
} else {
unset($this->data[$index]);
parent::offsetUnset($index);
return TRUE;
}
}
Expand All @@ -126,7 +106,7 @@ public function remove($item)
*/
protected function search($item)
{
return array_search($item, $this->data, TRUE);
return array_search($item, $this->getArrayCopy(), TRUE);
}


Expand All @@ -139,7 +119,7 @@ protected function search($item)
public function clear()
{
$this->beforeRemove();
$this->data = array();
parent::exchangeArray(array());
}


Expand All @@ -165,8 +145,9 @@ public function contains($item)
public function import($arr)
{
if (is_array($arr) || $arr instanceof Traversable) {
$this->clear();
foreach ($arr as $item) {
$this->add($item);
$this->offsetSet(NULL, $item);
}
} else {
throw new /*::*/InvalidArgumentException("Argument must be traversable.");
Expand All @@ -175,17 +156,6 @@ public function import($arr)



/**
* Returns an array containing all of the elements in this collection.
* @return array
*/
public function toArray()
{
return $this->data;
}



/**
* Returns a value indicating whether collection is read-only.
* @return bool
Expand All @@ -197,28 +167,6 @@ public function isReadOnly()



/**
* Returns the number of elements in collection (::Countable implementation).
* @return int
*/
public function count()
{
return count($this->data);
}



/**
* Returns an iterator over the elements in collection (::IteratorAggregate implementation).
* @return ::ArrayIterator
*/
public function getIterator()
{
return new /*::*/ArrayIterator($this->data);
}



/********************* internal notifications ****************d*g**/


Expand Down Expand Up @@ -263,4 +211,122 @@ protected function beforeRemove()
}
}

}


/********************* ArrayObject cooperation ****************d*g**/



/**
* Returns the iterator.
* @return ArrayIterator
*/
public function getIterator()
{
return new /*::*/ArrayIterator($this->getArrayCopy());
}



/**
* Not supported. Use import().
*/
public function exchangeArray($array)
{
throw new /*::*/NotSupportedException('Use ' . __CLASS__ . '::import()');
}



/**
* Protected exchangeArray().
* @param array new array
* @return void
*/
protected function setArray($array)
{
parent::exchangeArray($array);
}



/********************* Nette::Object behaviour ****************d*g**/



/**
* Returns the name of the class of this object.
*
* @return string
*/
final public function getClass()
{
return get_class($this);
}



/**
* Call to undefined method.
*
* @throws ::MemberAccessException
*/
protected function __call($name, $args)
{
$class = get_class($this);
throw new /*::*/MemberAccessException("Call to undefined method $class::$name().");
}



/**
* Call to undefined static method.
*
* @throws ::MemberAccessException
*/
protected static function __callStatic($name, $args)
{
$class = get_called_class();
throw new /*::*/MemberAccessException("Call to undefined static method $class::$name().");
}



/**
* Returns property value. Do not call directly.
*
* @throws ::MemberAccessException if the property is not defined.
*/
protected function &__get($name)
{
$class = get_class($this);
throw new /*::*/MemberAccessException("Cannot read an undeclared property $class::\$$name.");
}



/**
* Sets value of a property. Do not call directly.
*
* @throws ::MemberAccessException if the property is not defined or is read-only
*/
protected function __set($name, $value)
{
$class = get_class($this);
throw new /*::*/MemberAccessException("Cannot assign to an undeclared property $class::\$$name.");
}



/**
* Access to undeclared property.
*
* @throws ::MemberAccessException
*/
protected function __unset($name)
{
$class = get_class($this);
throw new /*::*/MemberAccessException("Cannot unset an property $class::\$$name.");
}

}

0 comments on commit ee86c6e

Please sign in to comment.