function indexAction() { /* @var $model CidadesModel */ $model = newModel('CidadesModel'); /* @var $v CidadeVO */ $start = microtime(true); $estado = url_parans(0) ? url_parans(0) : inputPost('estado'); $cidade = url_parans(1) ? url_parans(1) : inputPost('cidade'); if ($estado > 0 or preg_match('/^[A-Z]{2}$/i', $estado)) { $cache = new Cache('cidades.' . $estado, 60); if (!($options = $cache->getContent())) { $cidades = $model->getCidades($estado); if (count($cidades)) { $options = formOption('-- Selecione a cidade --', ''); } else { $options = formOption('-- Selecione o estado --', ''); } foreach ($cidades as $v) { $options .= formOption($v->getTitle(), $v->getId(), false); } # Salvando cache $cache->setContent($options); } echo preg_replace(['/(value="' . preg_quote($cidade) . '")/', '/>(' . preg_quote($cidade) . ')</'], ['$1 selected=""', 'selected="" >$1<'], $options); } $end = microtime(true); echo "\n\n<!-- " . number_format(($end - $start) * 1000, 5, ',', '.') . "ms --> Buscou por {$cidade}"; exit; }
/** * Create a new presenter/default view pair. * @param string $name presenter e.g.: "hello" * @param string $model make connection to a model */ function newPresenter($name, $model = NULL) { // lowercase $lowercase = strtolower($name); if (!empty($model)) { $model = ucfirst($model); $modelLowercase = strtolower($model); } $presenterCode = <<<CODE <?php if (!defined('FARI')) die(); /** * Description of {$name}. * * @package Application\\Presenters */ final class {$name}Presenter extends Fari_ApplicationPresenter { /********************* filters *********************/ /** var filters to apply to these actions before they are called */ //public \$beforeFilter = array( // array('nameOfFilter' => array('nameOfAction')) //); /** var filters to apply after all processing has occured */ //public \$afterFilter = array( // array('nameOfFilter' => array('nameOfAction')) //); /** * Applied automatically before any action is called. * @example use it to authenticate users or setup locales */ public function filterStartup() { } /********************* actions *********************/ /** Responsible for presenting a collection back to the user. */ \tpublic function actionIndex(\$p) { \$this->renderAction('index'); } /** Responsible for showing a single specific object to the user. */ \tpublic function actionShow() { } /** Responsible for providing the user with an empty form to create a new object. */ \tpublic function actionNew() { } /** Receives the form submission from the new action and creates the new object. */ \tpublic function actionCreate() { } /** Responsible for providing a form populated with a specific object to edit. */ \tpublic function actionEdit() { } /** Receives the form submission from the edit action and updates the specific object. */ \tpublic function actionUpdate() { } /** Deletes the specified object from the database. */ \tpublic function actionDelete() { } } CODE; $presenterAndModelCode = <<<CODE <?php if (!defined('FARI')) die(); /** * Description of {$name}. * * @package Application\\Presenters */ final class {$name}Presenter extends Fari_ApplicationPresenter { /** var {$model} Table connection */ private \${$modelLowercase}; /********************* filters *********************/ /** var filters to apply to these actions before they are called */ //public \$beforeFilter = array( // array('nameOfFilter' => array('nameOfAction')) //); /** var filters to apply after all processing has occured */ //public \$afterFilter = array( // array('nameOfFilter' => array('nameOfAction')) //); /** * Applied automatically before any action is called. * @example use it to authenticate users or setup locales */ public function filterStartup() { // setup table connection \$this->{$modelLowercase} = new {$model}(); } /********************* actions *********************/ /** Responsible for presenting a collection back to the user. */ \tpublic function actionIndex(\$p) { dump(\$this->{$modelLowercase}->findAll()); } /** Responsible for showing a single specific object to the user. */ \tpublic function actionShow(\$id) { dump(\$this->{$modelLowercase}->findFirst()->where(\$id)); } /** Responsible for providing the user with an empty form to create a new object. */ \tpublic function actionNew() { } /** Receives the form submission from the new action and creates the new object. */ \tpublic function actionCreate() { \$this->{$modelLowercase}->save(\$this->request->getPost()); \$this->redirectTo('{$lowercase}/index'); } /** Responsible for providing a form populated with a specific object to edit. */ \tpublic function actionEdit() { } /** Receives the form submission from the edit action and updates the specific object. */ \tpublic function actionUpdate(\$id) { \$this->{$modelLowercase}->update()->set(\$this->request->getPost())->where(\$id); \$this->redirectTo("{$lowercase}/show/{\$id}"); } /** Deletes the specified object from the database. */ \tpublic function actionDelete(\$id) { \$this->{$modelLowercase}->destroy()->where(\$id); \$this->redirectTo('{$lowercase}/index'); } } CODE; $layoutCode = <<<CODE <?php if (!defined('FARI')) die(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Language" content="en" /> <title>{$name}</title> <?php stylesheetLinkTag('style'); ?> </head> <body> <?php echo \$template; ?> </body> </html> CODE; $viewCode = <<<CODE <?php if (!defined('FARI')) die(); ?> <pre>This is a default view template in 'application/views/{$name}/index.phtml'</pre> CODE; $presenterPath = 'application/presenters'; $viewsPath = 'application/views'; // is dir writable? if (!is_writable(BASEPATH . "/{$presenterPath}")) { echo message("Cannot write into {$presenterPath} directory!", 'red'); } else { // check path to presenters exists, dir-wise $path = ''; foreach (explode('/', $presenterPath) as $dir) { $path .= $dir . '/'; createDirectory($path); } // does the presenter file exist? if (!empty($model)) { createFile("{$presenterPath}/{$name}Presenter.php", $presenterAndModelCode); newModel($model, 'id'); } else { createFile("{$presenterPath}/{$name}Presenter.php", $presenterCode); } // check/create views directory createDirectory($viewsPath . '/'); // presenter layout createFile("{$viewsPath}/@{$lowercase}.phtml", $layoutCode); // create appropriate presenter-named views dir createDirectory("{$viewsPath}/{$name}/"); // default index file createFile("{$viewsPath}/{$name}/index.phtml", $viewCode); } }
/** * Build schema from settings file. */ function buildSchema() { // does the settings file exist? if (!file_exists(BASEPATH . '/config/config.php')) { message("Couldn't find settings file config/config.php", 'red'); } else { // include file if (!defined('FARI')) { define('FARI', 'Fari Framework CLI'); } require BASEPATH . '/config/config.php'; // check database settings are present if (!defined('DB_DRIVER')) { message("Database driver 'DB_DRIVER' needs to be specified", 'red'); } if (!defined('DB_HOST')) { message("Database host 'DB_HOST' needs to be specified", 'red'); } if (!defined('DB_NAME')) { message("Database name 'DB_NAME' needs to be specified", 'red'); } // try connecting to the database try { switch (strtolower(DB_DRIVER)) { // MySQL case 'mysql': $pdoInstance = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME . ';unix_socket=/var/run/mysqld/mysqld.sock', DB_USER, DB_PASS); break; // PostgreSQL (untested) // PostgreSQL (untested) case 'pgsql': $pdoInstance = new PDO('pgsql:dbname=' . DB_NAME . ';host=' . DB_HOST, DB_USER, DB_PASS); break; // SQLite 3 // SQLite 3 case 'sqlite3': case 'sqlite': $pdoInstance = new PDO('sqlite:' . BASEPATH . '/' . DB_NAME); break; // SQLite 2 // SQLite 2 case 'sqlite2': $pdoInstance = new PDO('sqlite2:' . BASEPATH . '/' . DB_NAME); break; } } catch (PDOException $exception) { message('Cannot connect to DB: ' . $exception->getMessage(), 'red'); die; } // can we open schema file? if (!is_readable($f = BASEPATH . '/db/schema.sql')) { message("Couldn't find schema file db/schema.sql", 'red'); } else { $schemas = explode("\n", file_get_contents($f)); // traverse executing each line foreach ($schemas as $schema) { // a query (maybe) :) if (substr(trim($schema), 0, 1) !== '#') { if ($pdoInstance->exec($schema) === FALSE) { // did we try to create a table? $schema = explode(' ', $schema); if ($schema[0] == 'CREATE' && $schema[1] == 'TABLE') { // check if table exists if ($pdoInstance->exec("SELECT * FROM {$schema[2]} LIMIT 1") !== FALSE) { $schema = implode(' ', $schema); message(" exists '{$schema}'", 'gray'); continue; } } message(" failed '{$schema}'", 'red'); } else { message(" create '{$schema}'", 'black'); // were we trying to create table? $schema = explode(' ', $schema); if ($schema[0] == 'CREATE' && $schema[1] == 'TABLE') { $tableName = ucfirst(preg_replace("/[^a-zA-Z0-9\\s]/", "", $schema[2])); // determine primary key in the table $schema = explode(',', implode(' ', $schema)); foreach ($schema as $element) { // do we have primary key? if (strpos($element, 'PRIMARY KEY') !== FALSE) { $element = substr($element, strpos($element, '(') + 1); $fields = explode(', ', $element); foreach ($fields as $field) { // column field with PRIMARY KEY defined if (strpos($field, 'PRIMARY KEY') !== FALSE) { $primaryKey = preg_replace("/[^a-zA-Z0-9\\s]/", "", current(explode(' ', $field))); break; } } break; } } if (!defined('BACKSTAGE')) { define('BACKSTAGE', TRUE); } include_once 'generate.php'; // create model newModel($tableName, $primaryKey); } } } else { // a comment... message($schema, 'gray'); } } } } }