在没有数据库的情况下存储、更新、检索 PHP 应用程序的设置

发布于 2024-07-29 17:04:42 字数 716 浏览 2 评论 0原文

我需要能够将 php 应用程序的数据存储在文件中。 我需要能够在没有 PHP 本身之外的任何外部依赖项的情况下执行此操作。

以下是我的要求:

  • 设置不会经常更新/添加/删除。 因此,设置的更新不必非常高效。 然而,我确实希望能够通过 PHP 脚本来完成这一切,而不是通过编辑文件。
  • 设置将被不断读取,因此设置的读取必须非常高效。
  • 设置采用独特的格式,如果我将它们放在数组中,它可能类似于 $Settings["Database"]["AccessSettings"]["Username"]["myDBUsername"];
    $Settings["数据库"]["AccessSettings"]["密码"]["myDBPassword"];

我不想像上面提到的那样将设置存储在数组中。 相反,我更喜欢一些访问方法: getConfig("Database","Accesssettings","Username") 将返回 'myDBUsername'。 这样做的原因是我想限制我存储在全局范围内的变量。

获取/检索这些的最佳方式是什么?

执行我认为可能是 xml 文件的层次结构,但我不确定 PHP 访问 xml 文件是什么样的(特别是我需要能够添加、编辑和删除)。 如果应该是 XML,我应该研究哪种类型的 xml 访问。

如果是另一种格式,我想要一些指向正确方向的指针,以了解如何使用该格式。

I need to be able to store data for a php application in a file. I need to be able to do this without any sort of external dependencies other than PHP itself.

Here are the requirements I have:

  • Settings will not be updated/added/removed very often. So updating of settings does not have to be very efficient. However, I do want to be able to do this all through a PHP script, not through editing of files.
  • Settings will be read constantly, so reading of settings must be very efficient.
  • Settings are in a unique format, if I had them in an array it might be something like $Settings["Database"]["AccessSettings"]["Username"]["myDBUsername"];
    $Settings["Database"]["AccessSettings"]["Password"]["myDBPassword"];

I would prefer to not have settings stored in arrays like I mentioned above. Instead I would prefer some access methods: getConfig("Database","Accesssettings","Username") would return 'myDBUsername'. The reason for this is I want to limit the variables I am storing in the global scope.

What would the best way of getting/retrieving these be?

Do the the hierarchy I was thinking possibly an xml file, but I wasn't sure what PHP was like for accessing xml files (particularly the fact that I need to be able to add, edit, and remove). If it should be XML what sort of xml access should I look into.

If it is another format, I would like some pointers to the right direction for what to look into for how to use that format.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

_畞蕅 2024-08-05 17:04:42

Brian,parse_ini_file 就是您所需要的。

啊,我错过了您将通过 PHP 编辑此内容的要求。

在这种情况下,PHP 本身没有任何东西可以用于此目的。 你必须自己动手。

只需使用 Zend_Config_Ini 。 我知道您说过您不想使用其他任何东西,但是 Zend Framework 的结构允许您使用您需要的任何部分。 Zend_Config 可以单独使用。 您当然可以将这几个类添加到您的项目中,并让它们处理您的 INI 文件解析。

这是使用上面示例的示例:

[config]
Database.AccessSettings.Username = myDBUsername
Database.AccessSettings.Password = myDBPassword

您可以简单地加载和访问它:

$config = new Zend_Config_Ini('/path/to/ini', 'config');
echo $config->Datbase->AccessSettings->Username; // prints "myDBUsername"
echo $config->Datbase->AccessSettings->Password; // prints "myDBPassword"

要编辑和保存您的配置,您可以使用以下命令:

$config->Database->AccessSettings->Password = "foobar";
$writer = new Zend_Config_Writer_Ini(array('config'   => $config,
                                           'filename' => 'config.ini'));
$writer->write();

编辑

不太确定为什么人们会根据以下内容对此进行否决vog的误导性评论。 通过使用独占锁使其可供多人写入非常简单。 Zend_Config_Writer 使用 file_put_contents 进行写入,它始终支持 LOCK_EX 标志,它专门锁定文件以进行写入。 使用此标志时,不能让多个写入者同时尝试更新文件。

要将此标志与 Zend_Config_Writer 一起使用,如下所示简单:

$writer = new Zend_Config_Writer_Ini(array('config'   => $config,
                                           'filename' => 'config.ini'));
$writer->setExclusiveLock(true);
$writer->write();

替代语法:

$writer = new Zend_Config_Writer_Ini();
$writer->write('config.ini', $config, true); // 3rd parameter is $exclusiveLock

Brian, parse_ini_file is what you need.

Ah, I missed the requirement that you'd be editing this via PHP.

In that case there is nothing native to PHP that you can use for this purpose. You'd have to roll your own.

You could save yourself a ton of time by simply using Zend_Config_Ini though. I know you state that you don't want to use anything else, but Zend Framework is structured to allow you to use whatever pieces of it you need. Zend_Config can be used on it's own. You can certainly just add these few classes to your project and let them handle your INI file parsing.

Here is an example using your samples above:

[config]
Database.AccessSettings.Username = myDBUsername
Database.AccessSettings.Password = myDBPassword

You would load and access this as simply as:

$config = new Zend_Config_Ini('/path/to/ini', 'config');
echo $config->Datbase->AccessSettings->Username; // prints "myDBUsername"
echo $config->Datbase->AccessSettings->Password; // prints "myDBPassword"

To edit and save your config you would use the following:

$config->Database->AccessSettings->Password = "foobar";
$writer = new Zend_Config_Writer_Ini(array('config'   => $config,
                                           'filename' => 'config.ini'));
$writer->write();

Edit

Not really sure why people are voting this down based on vog's misguided comments. It is very simple to make this writable by multiple persons by using an exclusive lock. Zend_Config_Writer uses file_put_contents to do it's writing, which has always supported the the LOCK_EX flag, which exclusively locks a file for writing. When using this flag, you cannot have multiple writers attempting to update the file at the same time.

To use this flag with Zend_Config_Writer it's as simple as follows:

$writer = new Zend_Config_Writer_Ini(array('config'   => $config,
                                           'filename' => 'config.ini'));
$writer->setExclusiveLock(true);
$writer->write();

An alternate syntax:

$writer = new Zend_Config_Writer_Ini();
$writer->write('config.ini', $config, true); // 3rd parameter is $exclusiveLock
洒一地阳光 2024-08-05 17:04:42

如果您不进行手动编辑,请使用序列化数据。

使用 serialize 写入配置:

file_put_contents('myConfig.txt', serialize($Settings));

使用 unserialize

$Settings = unserialize(file_get_contents('myConfig.txt'));

您可以使用 PHP 魔术函数 __get() 和 __set() 编写一个类来修改和获取此数据的值。

If you're not hand editing, use serialized data.

Writing config using serialize:

file_put_contents('myConfig.txt', serialize($Settings));

Reading config using unserialize:

$Settings = unserialize(file_get_contents('myConfig.txt'));

You could write a class for modifying and getting values for this data using PHP magic functions __get() and __set().

雪落纷纷 2024-08-05 17:04:42

如果您愿意加入库,您可以使用 Spyc 并将您的配置放在一个 YAML 文件。

您还可以使用 PHP 对 SQLite 的支持 这意味着您的数据库只是一个文件,因此不需要数据库服务器。

If you are willing to drop in a library, you could use Spyc and have your configuration in a YAML file.

You could also use PHP's support for SQLite which would mean your database is just a file so no need for a DB server.

帅冕 2024-08-05 17:04:42

DBM 文件可能不是一个坏主意。 PHP 内置了对各种 DBM 文件类型的支持:

http://www .php.net/manual/en/book.dba.php

DBM files may not be a bad idea. PHP has built in support for the various DBM-file varieties:

http://www.php.net/manual/en/book.dba.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文