Example #1
0
	/**
	 * Install this component.
	 *
	 * Returns false if nothing changed, else will return an array containing all changes.
	 *
	 * @return boolean | array
	 * @throws InstallerException
	 */
	public function install() {

		if ($this->isInstalled()) return false;

		if (!$this->isLoadable()) return false;

		$changes = $this->_performInstall();


		// Run through each task under <install> and execute it.
		/** @var $u DOMNode */
		$u = $this->_xmlloader->getRootDOM()->getElementsByTagName('install')->item(0);

		// This gets a bit tricky, I need to get all the valid upgrade elements in the order that they
		// are defined in the component.xml.
		if($u){
			$children = $u->childNodes;
		}
		else{
			$children = [];
		}


		// The various upgrade tasks that can happen
		foreach($children as $child){
			/** @var $child DOMNode */
			switch($child->nodeName){
				case 'dataset':
					$datachanges = $this->_parseDatasetNode($child);
					if($datachanges !== false) $changes = array_merge($changes, $datachanges);
					break;
				case 'phpfileinclude':
					// I need to do this in a method so that include file doesn't mess with my local variables!
					$this->_includeFileForUpgrade(ROOT_PDIR . trim($child->nodeValue));
					$changes[] = 'Included custom php file ' . basename($child->nodeValue);
					break;
				case 'php':
					$file = $child->getAttribute('file');
					if($file){
						// I need to do this in a method so that include file doesn't mess with my local variables!
						$this->_includeFileForUpgrade($this->getBaseDir() . $file);
						$changes[] = 'Included custom php file ' . $file;
					}
					else{
						$changes[] = 'Ignoring invalid &lt;php&gt; directive, no file attribute provided!';
					}
					break;
				case 'sql':
					$file = $child->getAttribute('file');
					if($file){
						$contents = file_get_contents($this->getBaseDir() . $file);
						$execs = 0;
						$parser = new SQL_Parser_Dataset($contents, SQL_Parser::DIALECT_MYSQL);
						$datasets = $parser->parse();
						foreach($datasets as $ds){
							$ds->execute();
							$execs++;
						}
						$changes[] = 'Executed custom sql file ' . $file . ' and ran ' . $execs . ($execs == 1 ? ' query' : 'queries');
					}
					else{
						$changes[] = 'Ignoring invalid &lt;sql&gt; directive, no file attribute provided!';
					}
					break;
				case '#text':
					// Text entries can silently be ignored.
					break;
				default:
					$changes[] = 'Ignoring unsupported install directive: [' . $child->nodeName . ']';
			}
		}

		if(is_array($changes) && sizeof($changes)){
			SystemLogModel::LogInfoEvent('/updater/component/install', 'Component ' . $this->getName() . ' installed successfully!', implode("\n", $changes));
		}


		// Yay, it should be installed now.	Update the version in the database.
		$c = new ComponentModel($this->_name);
		$c->set('version', $this->_version);
		$c->save();
		$this->_versionDB = $this->_version;
		$this->_enabled = ($c->get('enabled') == '1');

		// And load this component into the system so anything else can access it immediately.
		$this->loadFiles();
		if (class_exists('Core')) {
			$ch = Core::Singleton();
			$ch->_registerComponent($this);
		}

		return $changes;
	}