/** * * @param array $info */ function generatePhp($info) { $php = "<?php\n"; $php .= "/**\n"; $php .= " * " . $info['class'] . "\n"; $php .= " */\n"; $php .= "namespace Sledgehammer\\Facebook;\n"; $php .= "\n"; $php .= "use Sledgehammer\\Collection;\n"; $php .= "use Sledgehammer\\GraphObject;\n"; $php .= "\n"; $php .= "/**\n"; $php .= " * " . $info['description'] . "\n"; if (count($info['fields']['id']['permissions'])) { $php .= " * Requires " . quoted_human_implode(' or ', $info['fields']['id']['permissions']) . " permissions.\n"; } $php .= " *\n"; $php .= " * @link " . $info['link'] . "\n"; $php .= " * @package Facebook\n"; $php .= " */\n"; $php .= "class " . $info['class'] . " extends \\Sledgehammer\\GraphObject {\n"; $php .= "\n"; foreach ($info['fields'] as $field) { $php .= "\t/**\n"; $php .= "\t * " . $this->addDot($field['description']) . "\n"; if (count($field['permissions']) !== 0 && $info['fields']['id']['permissions'] !== $field['permissions']) { $php .= "\t * Requires " . quoted_human_implode(' or ', $field['permissions']) . " permissions.\n"; } if (in_array($field['returns'], array('number', 'string', 'boolean'))) { $php .= "\t * @var " . $field['returns'] . "\n"; } else { $php .= "\t *\n\t * " . $field['returns'] . "\n"; } $php .= "\t */\n"; $php .= "\tpublic \$" . $field['name'] . ";\n"; $php .= "\n"; } $typeMapping = array('Achievement(instance)' => 'Achievement', 'Album' => 'Album', 'Event' => 'Event', 'Message' => 'Message', 'Order' => 'Order', 'Page' => 'Page', 'Photo' => 'Photo', 'Link' => 'Link', 'Thread' => 'Thread', 'User' => 'User', 'Friend' => 'User', 'Book' => 'Page', 'Movie' => 'Page', 'Like' => 'Page', 'Music' => 'Page', 'Television' => 'Page', 'Conversation' => 'Message', 'Conversation' => 'Message'); $invalidTypes = array('Id', 'The', 'Activity', 'Interest', 'Setting', 'Account'); if (count($info['connections']) != 0) { $knownConnections = ''; foreach ($info['connections'] as $connection) { $php .= "\t/**\n"; $php .= "\t * " . $this->addDot($connection['description']) . "\n"; if (count($connection['permissions']) !== 0 && $info['fields']['id']['permissions'] !== $connection['permissions']) { $php .= "\t * Requires " . quoted_human_implode(' or ', $connection['permissions']) . " permissions.\n"; } $type = false; // 'GraphObject'; if (preg_match('/array of ([a-z()]+) objects/', $connection['returns'], $match)) { $type = ucfirst($match[1]); } elseif (preg_match('/array of objects containing ([a-z()]+) /', $connection['returns'], $match)) { $type = ucfirst($match[1]); } if (isset($typeMapping[$type])) { // Is the detected type valid? $type = $typeMapping[$type]; } elseif ($type) { if (in_array($type, $invalidTypes) == false) { notice('Unknown type: "' . $type . '"'); } $type = false; } if ($type) { $php .= "\t * @var Collection|" . $type . "\n"; } else { $php .= "\t *\n\t * Returns " . $connection['returns'] . "\n"; $php .= "\t * @var Collection|GraphObject\n"; } $php .= "\t */\n"; if (isset($info['fields'][$connection['name']])) { $php .= '//'; // Prevent "duplicate property" parse error. } $php .= "\tpublic \$" . $connection['name'] . ";\n"; $php .= "\n"; $knownConnections .= "\t\t\t'" . $connection['name'] . "' => array("; if ($type) { $knownConnections .= "'class' => '\\Sledgehammer\\Facebook\\" . $type . "'"; } $knownConnections .= "),\n"; } } if ($info['constructor']) { $php .= "\t/**\n"; $php .= "\t * Constructor\n"; $php .= "\t * @param mixed \$id\n"; $php .= "\t * @param array \$parameters\n"; $php .= "\t * @param bool \$preload true: Fetch fields now. false: Fetch fields when needed.\n"; $php .= "\t */\n"; $php .= "\tfunction __construct(\$id, \$parameters = null, \$preload = false) {\n"; $php .= "\t\tif (\$id === null || is_array(\$id)) {\n"; $php .= "\t\t\tparent::__construct(\$id, \$parameters, \$preload);\n"; $php .= "\t\t\treturn;\n"; $php .= "\t\t}\n"; $php .= "\t\tif (\$parameters === null) { // Fetch all allowed fields?\n"; $php .= "\t\t\t\$parameters = array(\n"; $php .= "\t\t\t\t'fields' => implode(',', \$this->getAllowedFields(array('id' => \$id))),\n"; $php .= "\t\t\t);\n"; $php .= "\t\t}\n"; $php .= "\t\tparent::__construct(\$id, \$parameters, \$preload);\n"; $php .= "\t}\n"; $php .= "\n"; } // @todo generate getFieldPermissions() if (count($info['connections']) != 0) { $php .= "\tprotected static function getKnownConnections(\$options = array()) {\n"; $php .= "\t\t\$connections = array(\n"; $php .= $knownConnections; $php .= "\t\t);\n"; // @todo Add permissions based on 'user/friend' option. $php .= "\t\treturn \$connections;\n"; $php .= "\t}\n"; $php .= "\n"; } $php .= "}\n\n"; $php .= "?>"; return $php; }
/** * Post a link or status-message to the users feed. * @permission publish_stream * * @param Facebook\Post|GraphObject|array $post * @return Facebook\Post */ function postToFeed($post) { if (is_array($post)) { $post = new Facebook\Post($post); } $fb = Facebook::getInstance(); if (in_array('publish_stream', $fb->getPermissions()) === false) { notice('Posting to the user\'s feed requires the "publish_stream" permission', 'Current permissions: ' . quoted_human_implode(' and ', $fb->getPermissions())); } $response = $fb->post($this->id . '/feed', $post); $class = get_class($post); return new $class($response['id']); }
/** * Handle postTo* methods. * * @param string $method * @param array $arguments */ function __call($method, $arguments) { if (text($method)->startsWith('get')) { // a get*($parameters) method? if (empty($this->id)) { throw new \Exception('Can\'t fetch a connection without an id'); } if (count($arguments) > 0) { $parameters = $arguments[0]; } else { $parameters = array(); } $connection = lcfirst(substr($method, 3)); $connections = $this->getKnownConnections(array('id' => $this->id)); if (isset($connections[$connection]['class'])) { $class = $connections[$connection]['class']; } else { $class = '\\Sledgehammer\\GraphObject'; } if (isset($connections[$connection]['permission']) && $connections[$connection]['permission'] !== 'denied' && in_array($connections[$connection]['permission'], Facebook::getInstance()->getPermissions()) === false) { notice('Connection "' . $connection . '" requires the "' . $connections[$connection]['permission'] . '" permission', 'Current permissions: ' . quoted_human_implode(' and ', Facebook::getInstance()->getPermissions())); } $objects = array(); $response = Facebook::all($this->id . '/' . $connection, $parameters); foreach ($response as $data) { $objects[] = new $class($data); } if (empty($arguments['fields'])) { foreach ($objects as $object) { $object->_state = 'partial'; } } return new Collection($objects); } if (text($method)->startsWith('postTo')) { // a postTo*($data) method? if (empty($this->id)) { throw new \Exception('Can\'t post to a connection without an id'); } if (count($arguments) > 0) { $parameters = $arguments[0]; } else { notice('Missing argument 1 for ' . $method . '()'); $parameters = array(); } $response = Facebook::post($this->id . '/' . lcfirst(substr($method, 6)), $parameters); return new GraphObject($response['id']); } else { return parent::__call($method, $arguments); } }