/** * Saves the data changes in the data store. * * @param Enlight_Config $config * @param array $fields * @param bool $update * @return Enlight_Config_Adapter_DbTable */ public function write(Enlight_Config $config, $fields = null, $update = true) { $name = $this->_namePrefix . $config->getName() . $this->_nameSuffix; $section = $config->getSection(); $dbTable = new Enlight_Components_Table(array('name' => $name, 'db' => $this->_db)); $db = $dbTable->getAdapter(); if ($fields === null) { $fields = $config->getDirtyFields(); } if (empty($fields)) { return $this; } $updateData = array(); $insertData = array(); if ($this->_updatedColumn !== null) { $updateData[$this->_updatedColumn] = new Zend_Date(); $insertData[$this->_updatedColumn] = new Zend_Date(); } if ($this->_createdColumn !== null) { $insertData[$this->_createdColumn] = new Zend_Date(); } $where = array(); if ($section !== null) { if (is_array($this->_sectionColumn)) { foreach ($this->_sectionColumn as $key => $sectionColumn) { if (isset($section[$key])) { $where[] = $db->quoteInto($sectionColumn . '=?', $section[$key]); $insertData[$sectionColumn] = $section[$key]; } } } else { $where[] = $db->quoteInto($this->_sectionColumn . '=?', $section); $insertData[$this->_sectionColumn] = $section; } } foreach ((array) $fields as $field) { $fieldWhere = $where; $fieldWhere[] = $db->quoteInto($this->_nameColumn . '=?', $field); $row = $dbTable->fetchRow($fieldWhere); if ($row !== null) { if ($update) { $data = $updateData; if ($this->_automaticSerialization) { $data[$this->_valueColumn] = serialize($config->get($field)); } else { $data[$this->_valueColumn] = $config->get($field); } $dbTable->update($data, $fieldWhere); } } else { $data = $insertData; $data[$this->_nameColumn] = $field; if ($this->_automaticSerialization) { $data[$this->_valueColumn] = serialize($config->get($field)); } else { $data[$this->_valueColumn] = $config->get($field); } $dbTable->insert($data); } } $config->setDirtyFields(array_diff($config->getDirtyFields(), $fields)); return $this; }
/** * Saves the data changes to the data store. * * @param Enlight_Config $config * @param bool $forceWrite * @return Enlight_Config_Adapter_File */ public function write(Enlight_Config $config, $forceWrite = false) { if (!$this->_allowWrites) { return $this; } $section = $config->getSection(); $filename = $this->getFilename($config->getName()); if (!$config->isDirty() && !$forceWrite) { return $this; } if (!empty($section)) { $base = $this->readBase($filename); if (is_array($section)) { foreach (array_reverse($section) as $sectionName) { if (!isset($base->{$sectionName})) { $base->{$sectionName} = array(); } } $sectionName = $extendingSection = array_shift($section); foreach ($section as $extendedSection) { $base->setExtend($extendingSection, $extendedSection); $extendingSection = $extendedSection; } } else { $sectionName = (string) $section; } $base->{$sectionName} = $config; } else { $base = $config; } $dir = dirname($filename); if (!file_exists($dir) || !is_writeable($dir)) { $old = umask(0); mkdir($dir, 0777, true); chmod($dir, 0777); umask($old); } if (!is_writeable($dir)) { return $this; } $writer = 'Enlight_Config_Writer_' . ucfirst($this->_configType); /** @var $writer Enlight_Config_Writer_Writer */ $writer = new $writer(array('config' => $base, 'filename' => $filename)); $writer->write(); return $this; }
/** * Removes the data from the data store. * * @param Enlight_Config $config * @param array $fields * @param bool $update * @return Enlight_Config_Adapter_DbTable */ public function delete(Enlight_Config $config, $fields = null, $deleteDirty = false) { $name = $this->_namePrefix . $config->getName() . $this->_nameSuffix; $section = explode($config->getSectionSeparator(), $config->getSection()); $dbTable = $this->getTable($this->_namespaceColumn === null ? $name : null); $db = $dbTable->getAdapter(); if ($fields === null) { $fields = $config->getDirtyFields(); } if (empty($fields)) { return $this; } $where = array(); $insertData = array(); if ($this->_namespaceColumn !== null) { $insertData[$this->_namespaceColumn] = $name; $where[] = $db->quoteInto($this->_namespaceColumn . '=?', $name); } if ($section !== null) { if (is_array($this->_sectionColumn)) { foreach ($this->_sectionColumn as $key => $sectionColumn) { if (isset($section[$key])) { $where[] = $db->quoteInto($sectionColumn . '=?', $section[$key]); $insertData[$sectionColumn] = $section[$key]; } } } else { $where[] = $db->quoteInto($this->_sectionColumn . '=?', $section); $insertData[$this->_sectionColumn] = $section; } } foreach ((array) $fields as $field) { $fieldWhere = $where; $fieldWhere[] = $db->quoteInto($this->_nameColumn . '=?', $field); if (!$deleteDirty) { $fieldWhere[] = $db->quoteInto($this->_dirtyColumn . '=?', 0); } $row = $dbTable->fetchRow($fieldWhere); if ($row !== null) { $row->delete(); } } return $this; }
/** * Saves the data changes to the data store. * * @param Enlight_Config $config * @return Enlight_Config_Adapter_File */ public function write(Enlight_Config $config) { $section = $config->getSection(); $filename = $this->getFilename($config->getName()); if (!empty($section)) { $base = $this->readBase($filename); $base->{$section} = $config; } else { $base = $config; } try { $writer = 'Zend_Config_Writer_' . ucfirst($this->_configType); /** @var $writer Zend_Config_Writer */ $writer = new $writer(array('config' => $base, 'filename' => $filename)); $writer->write(); } catch (Zend_Exception $e) { throw new Enlight_Config_Exception($e->getMessage(), $e->getCode(), $e); } return $this; }
/** * Test case */ public function testConfigName() { $config = new Enlight_Config('test'); $this->assertEquals('test', $config->getName()); }