Example #1
0
    /**
     * Initiates the installation process after all settings have been set.
     *
     * @param boolean $output whether or not the installation process should display any output
     *
     * @throws Exception if any step in the installation process fails
     * @return boolean whether or not the process was successful
     */
	public static function install($output = true) {
		try {
			// Setup
			$xmlString = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
			$xmlObj = null;
			$htaccess = '';
			self::$_output = $output;
			date_default_timezone_set(self::$_data['timezone']);

			ini_set('output_buffering', false);
			ini_set('implicit_flush', 'true');

			// Install
			self::out('Installing Bedrock Framework ' . self::$_data['bedrock_version'], 'status');

            // Install: Directories
			self::out('Creating directory structure...', 'info');

			self::out('- Creating configuration directory.', 'info');
			if(!is_dir(self::$_data['root_cfg'])) {
				mkdir(self::$_data['root_cfg'], 0775);
			}

			self::out('- Creating log directory.', 'info');
			if(!is_dir(self::$_data['root_log'])) {
				mkdir(self::$_data['root_log'], 0775);
			}

			// Install: XML File
			self::out('Building XML File...', 'info');
			$xmlString .= '<config></config>';
			$xmlObj = simplexml_load_string($xmlString);
			$xmlObj->addChild('main');

			// Install: Application Details
			self::out('- Writing application details.', 'info');
			$xmlObj->main->addChild('meta');
			$xmlObj->main->meta->addChild('title', self::$_data['app_name']);
			$xmlObj->main->meta->addChild('namespace', self::$_data['app_namespace']);
			$xmlObj->main->meta->addChild('version');
			$xmlObj->main->meta->version->addChild('application', self::$_data['app_version']);
			$xmlObj->main->meta->version->addChild('bedrock', self::$_data['bedrock_version']);
			$xmlObj->main->meta->addChild('timezone', self::$_data['timezone']);
			$xmlObj->main->meta->addChild('dates');
			$xmlObj->main->meta->dates->addChild('installed', date('Y-m-d H:i:s'));
			$xmlObj->main->meta->dates->addChild('updated', date('Y-m-d H:i:s'));

			// Install: Directory Settings
			self::out('- Writing directory settings.', 'info');
			$xmlObj->main->addChild('root');
			$xmlObj->main->root->addChild('web', self::$_data['root_web']);
			$xmlObj->main->root->addChild('system', self::$_data['root_system']);
			$xmlObj->main->root->addChild('cfg', self::$_data['root_cfg']);
			$xmlObj->main->root->addChild('lib', self::$_data['root_lib']);
			$xmlObj->main->root->addChild('log', self::$_data['root_log']);
			$xmlObj->main->root->addChild('pub', self::$_data['root_pub']);

			// Install: Database Settings
			self::out('- Writing database configuration.', 'info');
			$xmlObj->main->addChild('database');
			$xmlObj->main->database->addChild('type', 'mysql');
			$xmlObj->main->database->addChild('host', self::$_data['database_address']);
			$xmlObj->main->database->addChild('dbname', self::$_data['database_name']);
			$xmlObj->main->database->addChild('username', self::$_data['database_username']);
			$xmlObj->main->database->addChild('password', self::$_data['database_password']);

			// Install: Additional Settings
			self::out('- Writing logger configuration.', 'info');
			$xmlObj->main->addChild('logger');
			$xmlObj->main->logger->addChild('targets');
			$xmlObj->main->logger->targets->addChild('system');
			$xmlObj->main->logger->targets->system->addChild('active', '0');
			$xmlObj->main->logger->targets->system->addChild('level', 'warn');
			$xmlObj->main->logger->targets->addChild('file');
			$xmlObj->main->logger->targets->file->addChild('active', '0');
			$xmlObj->main->logger->targets->file->addChild('level', 'warn');
			$xmlObj->main->logger->targets->addChild('firephp');
			$xmlObj->main->logger->targets->firephp->addChild('level', 'traverse');
			$xmlObj->main->logger->targets->firephp->addChild('active', '0');
			$xmlObj->main->logger->targets->addChild('growl');
			$xmlObj->main->logger->targets->growl->addChild('level', 'warn');
			$xmlObj->main->logger->targets->growl->addChild('active', '0');

			foreach(self::$_data['logto'] as $target) {
				if($xmlObj->main->logger->targets->$target) {
					$xmlObj->main->logger->targets->$target->active = '1';
				}
			}

			$xmlObj->main->addChild('env');
			$xmlObj->main->env->addChild('os', '');
			$xmlObj->main->env->addChild('file');
			$xmlObj->main->env->file->addChild('maxsize', 2097152);
			$xmlObj->main->env->addChild('error', 'E_ALL - E_NOTICE');
			$xmlObj->main->addChild('growl');
			$xmlObj->main->growl->addChild('host', self::$_data['growl_address']);
			$xmlObj->main->addChild('cookie');
			$xmlObj->main->cookie->addChild('name', self::$_data['app_namespace']);
			$xmlObj->main->cookie->addChild('life', 172800);

			$dom = dom_import_simpleXml($xmlObj)->ownerDocument;
			$dom->formatOutput = true;

			if(!$dom->save($xmlObj->main->root->cfg . 'application.xml')) {
				throw new Exception('Error: Configuration XML file could not be saved to "' . $xmlObj->main->root->config . 'application.xml"');
			}

			// Install: .htaccess
			if(self::$_data['friendly']) {
				self::out('Building .htaccess file...', 'info');
				$htaccess .= 'RewriteEngine on' . "\n";
				$htaccess .= "\n";
				$htaccess .= 'RewriteCond %{REQUEST_FILENAME} !-f' . "\n";
				$htaccess .= 'RewriteCond %{REQUEST_FILENAME} !-d' . "\n";
				$htaccess .= "\n";
				$htaccess .= 'RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]' . "\n";

				if(!file_put_contents($xmlObj->main->root->pub . '.htaccess', $htaccess)) {
					throw new Exception('Error: .htaccess file could not be written!');
				}
			}

			// Install: Class Hierarchy
			self::out('Building namespace "' . self::getValue('app_namespace') . '"...');
			if(!is_dir(self::getValue('root_lib') . self::getValue('app_namespace'))) {
				if(!mkdir(self::getValue('root_lib') . self::getValue('app_namespace'))) {
					throw new Exception('Could not create the application library directory.');
				}
			}

			self::out('- ' . self::getValue('app_namespace') . '::Control');
			if(false === file_put_contents(self::getValue('root_lib') . self::getValue('app_namespace') . DIRECTORY_SEPARATOR . 'Control.php', self::getScriptContent('controller', self::getValue('app_namespace')))) {
				throw new Exception('Creation of base Controller failed!');
			}

			self::out('- ' . self::getValue('app_namespace') . '::Control::Index');
			if(!is_dir(self::getValue('root_lib') . self::getValue('app_namespace') . DIRECTORY_SEPARATOR . 'Control')) {
				if(!mkdir(self::getValue('root_lib') . self::getValue('app_namespace') . DIRECTORY_SEPARATOR . 'Control')) {
					throw new Exception('Could not create the application Controller root directory.');
				}
			}

			if(false === file_put_contents(self::getValue('root_lib') . self::getValue('app_namespace') . DIRECTORY_SEPARATOR . 'Control' . DIRECTORY_SEPARATOR . 'Index.php', self::getScriptContent('controller:index', self::getValue('app_namespace')))) {
				throw new Exception('Creation of index Controller failed!');
			}

			self::out('- ' . self::getValue('app_namespace') . '::View');
			if(false === file_put_contents(self::getValue('root_lib') . self::getValue('app_namespace') . DIRECTORY_SEPARATOR . 'View.php', self::getScriptContent('view', self::getValue('app_namespace')))) {
				throw new Exception('Creation of base View failed!');
			}

			self::out('- ' . self::getValue('app_namespace') . '::View::Index');
			if(!is_dir(self::getValue('root_lib') . self::getValue('app_namespace') . DIRECTORY_SEPARATOR . 'View')) {
				if(!mkdir(self::getValue('root_lib') . self::getValue('app_namespace') . DIRECTORY_SEPARATOR . 'View')) {
					throw new Exception('Could not create the application View root directory.');
				}
			}

			if(false === file_put_contents(self::getValue('root_lib') . self::getValue('app_namespace') . DIRECTORY_SEPARATOR . 'View' . DIRECTORY_SEPARATOR . 'Index.php', self::getScriptContent('view:index', self::getValue('app_namespace')))) {
				throw new Exception('Creation of index View failed!');
			}

			self::out('Installation Complete!', 'success');

			return true;
		}
		catch(Exception $ex) {
			self::out($ex->getMessage(), 'error');
			self::out('Installation Failed!', 'end');

			if(is_file($xmlObj->main->root->config . 'application.xml')) {
				@unlink($xmlObj->main->root->config . 'application.xml');
			}
			return false;
		}
	}
Example #2
0
	/**
	 * Returns the current table's schema as a string of the specified type.
	 *
	 * @param integer $formatType the format type to use
	 * @return string the schema represented as a string
	 */
	public function schemaToString($formatType = \Bedrock\Model::FORMAT_SQL) {
		try {
			// Setup
			$result = '';

			$this->load();
			
			switch($formatType) {
				default:
					
				// SQL Query
				case \Bedrock\Model::FORMAT_SQL:
					$result = 'CREATE TABLE `' . self::sanitize($this->_name) . '` (';
					
					// Table Columns
					foreach($this->_columns as $column) {
						$result .= $column->definition . ', ';
					}
					
					$result = substr($result, 0, strlen($result)-2) . ')';
					
					// Table Properties
					if($this->_properties['engine'] != '') {
						$result .= ' ENGINE = ' . $this->_properties['engine'];
					}
					
					if($this->_properties['charset'] != '') {
						$result .= ' DEFAULT CHARACTER SET = ' . $this->_properties['charset'];
					}
					
					if($this->_properties['collation'] != '') {
						$result .= ' DEFAULT COLLATE = ' . $this->_properties['collation'];
					}
					
					if($this->_properties['comment'] != '') {
						$result .= ' COMMENT = \'' . self::sanitize($this->_properties['comment']) . '\'';
					}
					break;
					
				// XML String
				case \Bedrock\Model::FORMAT_XML:
					$xml = new \SimpleXMLElement('<table></table>');
					$xml->addAttribute('name', $this->_name);
					
					// Table Properties
					$xml->addChild('properties');
					
					if($this->_properties['engine'] != '') {
						$xml->properties->addChild('engine', $this->_properties['engine']);
					}
					
					if($this->_properties['charset'] != '') {
						$xml->properties->addChild('charset', $this->_properties['charset']);
					}
					
					if($this->_properties['collation'] != '') {
						$xml->properties->addChild('collation', $this->_properties['collation']);
					}
					
					if($this->_properties['comment'] != '') {
						$xml->properties->addChild('comment', $this->_properties['comment']);
					}
					
					// Table Columns
					$xml->addChild('columns');
					
					foreach($this->_columns as $column) {
						$currentColumn = $xml->columns->addChild('column');
						$currentColumn->addAttribute('name', $column->name);
						$currentColumn->addChild('type', $column->type);
						$currentColumn->addChild('length', $column->length);
						$currentColumn->addChild('size', $column->size);
						$currentColumn->addChild('default', $column->default);
						$currentColumn->addChild('comment', $column->comment);
						
						$flags = $currentColumn->addChild('flags');
						$flags->addChild('primarykey', $column->primary_key ? 1 : 0);
						$flags->addChild('autoincrement', $column->auto_increment ? 1 : 0);
						$flags->addChild('unique', $column->unique ? 1 : 0);
						$flags->addChild('null', $column->null ? 1 : 0);
					}

					$dom = dom_import_simpleXml($xml)->ownerDocument;
					$dom->formatOutput = true;
					
					$result = $dom->saveXML();
					break;
					
				// YAML String
				case \Bedrock\Model::FORMAT_YAML:
					// Build Array
					$data = array(
						'table' => array(
							'name' => $this->_name,
							'properties' => array()
						)
					);

					// Table Properties
					if($this->_properties['engine'] != '') {
						$data['table']['properties']['engine'] = $this->_properties['engine'];
					}

					if($this->_properties['charset'] != '') {
						$data['table']['properties']['charset'] = $this->_properties['charset'];
					}

					if($this->_properties['collation'] != '') {
						$data['table']['properties']['collation'] = $this->_properties['collation'];
					}

					if($this->_properties['comment'] != '') {
						$data['table']['properties']['comment'] = $this->_properties['comment'];
					}

					// Table Columns
					foreach($this->_columns as $column) {
						$colArr = array();
						
						if($column->name != '') $colArr['name'] = $column->name;
						if($column->type != '') $colArr['type'] = $column->type;
						if($column->length != '') $colArr['length'] = $column->length;
						if($column->size != '') $colArr['size'] = $column->size;
						if($column->default != '') $colArr['default'] = $column->default;
						if($column->comment != '') $colArr['comment'] = $column->comment;

						$colArr['flags'] = array(
							'primarykey' => $column->primary_key ? 1 : 0,
							'autoincrement' => $column->auto_increment ? 1 : 0,
							'unique' => $column->unique ? 1 : 0,
							'null' => $column->null ? 1 : 0
						);
						
						$data['table']['columns'][] = $colArr;
					}

					// Build YAML
					$result = \Bedrock\Common\Data\YAML::encode($data);
					break;
					
				// CSV String
				case \Bedrock\Model::FORMAT_CSV:
					// Build Array
					$columns = array('class', 'name', 'type', 'length', 'size', 'default', 'comment', 'primarykey', 'autoincrement', 'unique', 'null', 'engine', 'charset', 'collation');

					// Table Properties
					$table = array(
						'class' => 'table',
						'name' => $this->_name
					);

					if($this->_properties['engine'] != '') {
						$table['engine'] = $this->_properties['engine'];
					}

					if($this->_properties['charset'] != '') {
						$table['charset'] = $this->_properties['charset'];
					}

					if($this->_properties['collation'] != '') {
						$table['collation'] = $this->_properties['collation'];
					}

					if($this->_properties['comment'] != '') {
						$table['comment'] = $this->_properties['comment'];
					}

					$data[] = $table;

					// Table Columns
					foreach($this->_columns as $column) {
						$data[] = array(
							'class' => 'column',
							'name' => $column->name,
							'type' => $column->type,
							'length' => $column->length,
							'size' => $column->size,
							'default' => $column->default,
							'comment' => $column->comment,
							'primarykey' => $column->primary_key ? 1 : 0,
							'autoincrement' => $column->auto_increment ? 1 : 0,
							'unique' => $column->unique ? 1 : 0,
							'null' => $column->null ? 1 : 0
						);
					}

					$result = \Bedrock\Common\Data\CSV::encode($data, ', ', $columns);
					break;
			}
			return $result;
		}
		catch(\DOMException $ex) {
			\Bedrock\Common\Logger::exception($ex);
			throw new \Bedrock\Model\Exception('A problem was encountered converting the schema for table "' . $this->_name . '" to a string.');
		}
		catch(\Exception $ex) {
			\Bedrock\Common\Logger::exception($ex);
			throw new \Bedrock\Model\Exception('A problem was encountered converting the schema for table "' . $this->_name . '" to a string.');
		}
	}