public function __construct($uri = "") { // Uri passed? if (empty($uri)) { // HTTPS? $this->protocol = array_key_exists("HTTPS", $_SERVER) && $_SERVER['HTTPS'] == 'on' ? "https" : "http"; // Use server data to form url $this->uri = $this->protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // And get some other info at the same time $this->method = $_SERVER['REQUEST_METHOD']; $this->domain = $_SERVER['HTTP_HOST']; $this->fullPath = $_SERVER['REQUEST_URI']; $this->accept = Format::FromHTTPAcceptHeader($_SERVER['HTTP_ACCEPT']); $this->acceptLanguage = explode(",", substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'], ";"))); } else { // Parse url $this->uri = $uri; throw new Exception("Parsing of url not implemented", 1); //preg_match_all() } // Check base path $this->path = $this->fullPath; if (StringUtil::startsWith($this->path, BASE_PATH)) { // Remove it from path $basePath = BASE_PATH; if (StringUtil::endsWith($basePath, "/")) { $basePath = substr($basePath, 0, -1); } $this->path = substr($this->path, strlen($basePath)); } // Check path for ?querystring $qPos = strpos($this->path, '?'); if ($qPos > 0) { $this->path = substr($this->path, 0, $qPos); } // Check path for extension preg_match_all("/(.+)\\.([a-z]+)\$/i", $this->path, $matches); if (count($matches[0]) == 1 && !is_null(Format::FromExtension($matches[2][0]))) { // Extension found $this->extension = $matches[2][0]; $this->path = $matches[1][0]; // Now look for format that matches this one (if extension overrides Accept headers) if (ChickenWire::get("extensionOverridesAcceptHeaders") !== false || is_null($this->accept)) { // Find format $format = Format::FromExtension($this->extension); if (!is_null($format)) { $this->accept = array($format); } } } // Store format $this->format = $this->accept[0]; }
public static function Create($link) { // Is it already a complete url? if (strstr($link, "://")) { return $link; } // Include domain? if (ChickenWire::get("useAbsoluteUrls") == true) { // Prefix with url return BASE_URL . $link; } else { // Prefix with path return BASE_PATH . $link; } }
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"))); }
function __autoload($class_name) { // Convert namespaces to file path $parts = explode("\\", strtolower($class_name)); // A ChickenWire class? if (count($parts) > 0 && $parts[0] == "chickenwire") { // Look for it one level up (I'm in chickenwire/core/) array_shift($parts); $filename = __DIR__ . "/../" . implode("/", $parts) . ".class.php"; if (file_exists($filename)) { require_once $filename; } } elseif (count($parts) > 0 && $parts[0] == strtolower(ChickenWire::get("applicationNamespace"))) { // Look for it in the application directory array_shift($parts); $filename = APPLICATION_DIR . "/" . implode("/", $parts) . ".class.php"; if (file_exists($filename)) { require_once $filename; } } }
<?php use ChickenWire\Core\ChickenWire; // Detect environment switch ($_SERVER['HTTP_HOST']) { case "admin.wipkip.com": ChickenWire::set("environment", "production"); break; case 'admin.wipkip.dev': default: ChickenWire::set("environment", "development"); break; }
<?php use ChickenWire\Core\ChickenWire; use ChickenWire\Request\Format; // Set application namespace ChickenWire::set("applicationNamespace", "WipkipAdmin"); // Set PHP extension ChickenWire::set("phpExtension", "php"); // Make it so that extensions in urls are more important than accept headers ChickenWire::set("extensionOverridesAcceptHeaders", true); // Set the default output format ChickenWire::set("defaultFormat", Format::HTML()); ChickenWire::set("defaultCharset", "utf-8"); // Set the default output formats for routes ChickenWire::set("defaultRouteFormats", array(Format::HTML(), Format::JSON())); // Set locations for static assets ChickenWire::set("pathCSS", "static/css"); ChickenWire::set("pathJavascript", "static/js"); ChickenWire::set("pathImages", "static/images"); // Set whether to use absolute URLs ChickenWire::set("useAbsoluteUrls", false); // Memory cache (http://www.php.net/manual/en/book.memcache.php) ChickenWire::set("memCache", "localhost:11211"); // Logging if (ChickenWire::get("environment") == "development") { ChickenWire::set("log", "FireBug"); }
private function getFormats() { // No formats given? if (is_null($this->formats)) { // Use the default formats $formats = ChickenWire::get("defaultRouteFormats"); // Still empty? if (is_null($formats)) { // That's no good throw new \Exception("ChickenWire could not determine a format for Route '" . $this->pattern . "`. Either define formats for each Route, or use ChickenWire::set(\"defaultRouteFormats\", ...) in your configuration.", 1); } return $formats; } else { return $this->formats; } }
<?php use ChickenWire\Core\ChickenWire; // Set database connections ChickenWire::set("database::connections", array("development" => "mysql://*****:*****@localhost/wipkip_admin", "production" => "")); // Set default ChickenWire::set("database::default", ChickenWire::get("environment"));
/** * Execute a raw SQL query on the database * @param string $sql SQL query string * @param array $values Optional values array to bind in query * @return mixed A result set object */ public function Query($sql, &$values = array()) { // Log it if (count($values) > 0) { ChickenWire::Log($sql . " ==> " . implode(", ", $values), "SQL Query"); } else { ChickenWire::Log($sql, "SQL Query"); } // Store query $this->lastQuery = $sql; // Try to prepare the query try { if (!($sth = $this->pdo->prepare($sql))) { throw new DBException($this); } } catch (PDOException $e) { throw new DBException($this); } // Set fetch mode $sth->setFetchMode(PDO::FETCH_ASSOC); // Execute query try { if (!$sth->execute($values)) { throw new DBException($sth); } } catch (PDOException $e) { throw new DBException($e); } return $sth; }
public static function DB($connection = "") { // No connection given? if (empty($connection)) { // Get default connection $connection = ChickenWire::get("database::default"); // Null? if (is_null($connection)) { throw new Exception("No default database has been selected. Use ChickenWire::set('database::default').", 1); } } // Is the connection already made? if (array_key_exists($connection, self::$_databases)) { // Return it now return self::$_databases[$connection]; } // Look up settings $dbs = ChickenWire::get("database::connections"); if (is_null($dbs) || !array_key_exists($connection, $dbs)) { throw new Exception("No database connection found for '{$connection}'.", 1); } // Create $conn = Connection::Create($dbs[$connection]); // Store and return self::$_databases[$connection] = $conn; return $conn; }
/** * Find the appropriate DB connection for this model */ protected function initConnection() { // Check connection property on the model if ($connection = $this->class->getStaticPropertyValue('connection', null)) { // Get that connection $this->connection = ChickenWire::DB($connection); } else { // Get default connection $this->connection = ChickenWire::DB(); } }
/** * 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; }
<?php use ChickenWire\Core\ChickenWire; use ChickenWire\Request\Format; ChickenWire::AddRoute("/", array("controller" => "Hours", "action" => "Index", "alias" => "/index"));