You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by Trevor Gattis <gt...@gmail.com> on 2011/07/23 02:51:42 UTC

THRIFT_ROOT in php and dealing with different /packages/

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

RE: THRIFT_ROOT in php and dealing with different /packages/

Posted by Trevor Gattis <gt...@gmail.com>.
I'm not sure if this question is better suited for the development list
instead (apologies if this is not the correct place to post this question on
code change).  This is a change I'd like to get added to the repository if
possible.  If there's a better solution to this issue, please let me know.

Thanks,
  Trevor


---------- Forwarded message ----------
From: Trevor Gattis <gt...@gmail.com>
Date: Fri, Jul 22, 2011 at 5:51 PM
Subject: THRIFT_ROOT in php and dealing with different /packages/
To: user@thrift.apache.org


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