/** * Create a new Route. * * @param string $pattern The pattern (TODO: explain patterns) * @param array $options Associative array containging the route configuration. * * Possible options are: * * option | type | default | description * ---------------- | ------------- | --------- | ------------------------- * controller | string | | The controller for this route * action | string | "Index" | The controller-action to use * model | string | "" | The model for this route. This can be used to auto-generate urls for models, using Route::editPath($modelInstance), Route::newPath(ModelClass), etc. * method | string | "" | HTTP method filter (POST or GET). When no method is passed, all methods are accepted * protocol | string | "" | Server protocol filter ("https" or "http"). By default all protocols are accepted * domains | array | | An array of domains this route is active for. By default this is empty, meaning all domains will match. You can use regular expressions in this array. * formats | array | | The list of accepted formats. See .......... * */ public function __construct($pattern, $options) { // No controller given? if (!array_key_exists("controller", $options) || empty($options['controller'])) { throw new \Exception("You cannot create a Route without specifying a controller.", 1); } // Default options $options = ArrayUtil::Defaults($options, array("action" => "Index", "model" => "", "method" => "", "protocol" => "", "controllerPath" => "", "domains" => null, "formats" => null)); // Localize $this->pattern = $pattern; $this->controller = $options['controller']; $this->action = $options['action']; $this->model = $options['model']; $this->method = $options['method']; $this->protocol = $options['protocol']; $this->controllerPath = $options['controllerPath']; $this->domains = $options['domains']; $this->formats = $options['formats']; // Is there a variable in pattern? if (strstr($this->pattern, "{") !== false) { // Find all { }'s preg_match_all("/({([^}]*)})/", $this->pattern, $matches); $this->_regExVars = $matches[2]; // Create regular expression to match this pattern $regex = "/" . preg_replace(array("/({#([^}]*)})/", "/({([^}]*)})/"), array("([0-9]+)", "([a-zA-Z0-9_-]+)"), str_replace("/", "\\/", $this->pattern)) . "+\$/"; $this->_regExPattern = $regex; } else { $this->_regExPattern = '/' . str_replace("/", "\\/", $this->pattern) . "\$/"; $this->_regExVars = array(); } }
protected function optionsToSQL(array $options) { // Check table (either options['from'] or me-self) $table = array_key_exists("from", $options) ? $options['from'] : $this->getFullyQualifiedName(); // Create new SQL query $sql = new SQL($this->connection, $table); // Any table joins? if (array_key_exists("joins", $options)) { // //@TODO Join tables throw new Exception("Table JOINS not yet implemented.", 1); } // Select fields? if (array_key_exists("select", $options)) { // Apply select $sql->Select($options['select']); } // Conditions given? if (array_key_exists("conditions", $options)) { // Is it a field hash? if (ArrayUtil::IsHash($options['conditions'])) { // Do a where with the hash $sql->Where($options['conditions']); } else { // Is it a single string? if (is_string($options['conditions'])) { // Wrap in array $options['conditions'] = array($options['conditions']); } // Do a where with the string(s) as arguments call_user_func_array(array($sql, "Where"), $options['conditions']); } } // Order. if (array_key_exists("order", $options)) { $sql->Order($options['order']); } // Limit if (array_key_exists("limit", $options)) { $sql->Limit($options['limit']); } // Offset if (array_key_exists("offset", $options)) { $sql->Offset($options['offset']); } // Grouping if (array_key_exists("group", $options)) { $sql->Group($options['group']); } // Having if (array_key_exists("having", $options)) { $sql->Having($options['having']); } // Done return $sql; }
public function Stylesheet($filename, array $options = null) { // No options? if (is_null($options)) { $options = array(); } // Check filename's extension if (pathinfo($filename, PATHINFO_EXTENSION) == "") { $filename .= ".css"; } // Add static path and create URL if (strstr($filename, "://")) { $link = $filename; } else { $link = Url::Create(Path::Construct(ChickenWire::get("pathCSS")) . $filename); } // Create the tag return $this->SingleTag("link", ArrayUtil::Defaults($options, array("src" => $link, "rel" => "stylesheet", "type" => "text/css"))); }
public static function FromHTTPAcceptHeader() { // Get the header $accept = $_SERVER['HTTP_ACCEPT']; // Split on , $types = explode(",", $accept); // Loop through types $foundFormatNames = array(); $foundFormats = array(); foreach ($types as $type) { // Create format from it $format = Format::FromContentType($type); // Not yet found? if (!is_null($format) && !in_array($format->format, $foundFormatNames)) { array_push($foundFormats, $format); array_push($foundFormatNames, $format->format); } } // Sort on preference ArrayUtil::SortOn($foundFormats, "preference", false); // Done. return $foundFormats; }
/** * Check whether given array was a search options hash * @param array $array The hash to validate * @return boolean True is valid, otherwise false */ protected static function isOptionsHash($array) { // Hash at all? if (ArrayUtil::IsHash($array)) { // Check difference with valid-options array $keys = array_keys($array); $diff = array_diff($keys, self::$VALID_OPTIONS); // Difference found? if (!empty($diff)) { throw new ActiveRecordException("Unkown key(s): " . explode(', ', $diff)); } // Is there a resemblance then? (Not an empty array, or something..?) $intersect = array_intersect($keys, self::$VALID_OPTIONS); if (!empty($intersect)) { return true; } } return false; }
protected function applyWhereConditions($args) { // A hash given? if (count($args) == 1 && ArrayUtil::IsHash($args[0])) { // More than 1 table used? => Use table names in fields $hash = $this->quoteFields($args[0], is_null($this->joins)); // Create where expressions $clauses = array(); foreach ($hash as $key => &$value) { // Array? if (is_array($value)) { $clauses[] = $key .= ' IN (?)'; } else { $clauses[] = $key .= ' = ?'; } // Simplify value $value = $this->valueToString($value); } // Store it $this->where = implode(", ", $clauses); $this->where_values = array_values($hash); } elseif (count($args) > 0) { throw new Exception("Not yet implemented.", 1); } }
/** * Use an abstract location to find an actual file * @param string $file Abstract filename, as used in views, layout, etc. E.g.: "views/news/index", or "layout/main", or "views/news/index.txt" * @param boolean &$php If you pass a variable, this will be set to true, when the extension is a PHP extension, and should be parsed. * @return string The file location, or false when not found */ private function lookForFile($file, &$php = null) { //TODO: The Request should have a FORMAT that it requests, based on the extension in the URL or the HTTP-accept header. This should be linked to an extension of a file to look for (.html, .txt, .json, etc.) // Split up in parts on / $dirparts = explode("/", $file); // Check filename for extensions $filename = array_pop($dirparts); // Loop through the request format's extensions $extensions = $this->request->format->extensions; foreach ($extensions as $extension) { // No extension at all? $fileparts = explode(".", $filename); if (count($fileparts) == 1) { // Add format's extensions array_push($fileparts, $extension); } // Look for file now $completeFilename = APPLICATION_DIR . implode($dirparts, "/") . "/" . implode($fileparts, "."); if (file_exists($completeFilename)) { // PHP? $php = ArrayUtil::LastItem($fileparts) == ChickenWire::get("phpExtension"); // Found it! return $completeFilename; } // Should we look for the PHP variant? if (ArrayUtil::LastItem($fileparts) != ChickenWire::get("phpExtension")) { // Add php too array_push($fileparts, ChickenWire::get("phpExtension")); $completeFilename = APPLICATION_DIR . implode($dirparts, "/") . "/" . implode($fileparts, "."); if (file_exists($completeFilename)) { // PHP! $php = true; // Found it! return $completeFilename; } } } // Nothing :( return false; }