thrift-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Trevor Gattis <gtr...@gmail.com>
Subject THRIFT_ROOT in php and dealing with different /packages/
Date Sat, 23 Jul 2011 00:51:42 GMT
It seems that the standard practice in php is to install common libraries to
be used by all code bases in a place like /usr/lib/php ( ie: Mail.php,
Log.php, etc ) .  To make the thrift libraries available to these code bases
it would be natural to install thrift in this same directory.

It seems, though, the thift structure is build with the assumption it's not
going to be deployed as a common library, but rather in a local directory
with your custom code.  Why do I say this?  Well, because the thrift
generated php code assumes that the generated code is under THRIFT_ROOT .
'/packages/' .  These are the custom files I've generated from my own custom
.thrift files that will most likely live in different projects possibly for
completely different websites.  But because this path is hard coded to be
derived from the thrift library root, it forces me to install completely
different code bases into the same packages directory.  This leads to tying
projects together that shouldn't be related as well as ends up creating name
spacing conflicts.

It seems much more natural to define THRIFT_ROOT to be where the library
files are (Thrift.php, protocol/*, transport/* ) and a new global
THRIFT_PACKAGES to point to a particular project's generated php files.
This would allow multiple projects deployed on the same server to use the
same common thrift libraries (as installed by an RPM or something), yet have
different projects specify where all their own, custom generated files are
supposed to be.

It would be easy enough to change the lines in
compiler/cpp/src/t_php_generator.cc(270-291):

  // Include other Thrift includes
  const vector<t_program*>& includes = program_->get_includes();
  for (size_t i = 0; i < includes.size(); ++i) {
    string package = includes[i]->get_name();
    string prefix = php_path(includes[i]);
    f_types_ <<
*      "include_once $GLOBALS['THRIFT_ROOT'].'/packages/" << prefix << "/"
<< package << "_types.php';" << endl;
*  }
  f_types_ << endl;

  // Print header
  if (!program_->get_consts().empty()) {
    string f_consts_name = package_dir_+program_name_+"_constants.php";
    f_consts_.open(f_consts_name.c_str());
    f_consts_ <<
      "<?php" << endl <<
      autogen_comment() <<
*      "include_once $GLOBALS['THRIFT_ROOT'].'/packages/" +
php_path(program_) + "/" + program_name_ + "_types.php';" << endl <<
*      endl <<
      "$GLOBALS['" << program_name_ << "_CONSTANTS'] = array();" << endl
<<
      endl;
  }

to use THRIFT_PACKAGES instead of the hard coded path:

  // Include other Thrift includes
  const vector<t_program*>& includes = program_->get_includes();
  for (size_t i = 0; i < includes.size(); ++i) {
    string package = includes[i]->get_name();
    string prefix = php_path(includes[i]);
    f_types_ <<
*      "include_once $GLOBALS['THRIFT_PACKAGES'].'/" << prefix << "/" <<
package << "_types.php';" << endl;
*  }
  f_types_ << endl;

  // Print header
  if (!program_->get_consts().empty()) {
    string f_consts_name = package_dir_+program_name_+"_constants.php";
    f_consts_.open(f_consts_name.c_str());
    f_consts_ <<
      "<?php" << endl <<
      autogen_comment() <<
*      "include_once $GLOBALS['THRIFT_PACKAGES'].'/" + php_path(program_) +
"/" + program_name_ + "_types.php';" << endl <<
*      endl <<
      "$GLOBALS['" << program_name_ << "_CONSTANTS'] = array();" << endl
<<
      endl;
  }


To maintain backwards compatibility with people who haven't defined
THRIFT_PACKAGES, you'd update Thrift.php with the following code:

/**
 * Set global THRIFT ROOT automatically via inclusion here
 */
if (!isset($GLOBALS['THRIFT_ROOT'])) {
  $GLOBALS['THRIFT_ROOT'] = dirname(__FILE__);
}

*if (!isset($GLOBALS['THRIFT_PACKAGES'])) {
  $GLOBALS['THRIFT_PACKAGES'] = $GLOBALS['THRIFT_ROOT'] . '/packages';
}
*
include_once $GLOBALS['THRIFT_ROOT'].'/protocol/TProtocol.php';
include_once $GLOBALS['THRIFT_ROOT'].'/transport/TTransport.php';

Does this seem to be the right solution for allowing thrift to be installed
as a shared library, yet allow no interference between projects for the
custom generated php files?

Thanks,
  Trevor

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message