#!/usr/bin/env php getConfig(); Api::init($config->appId, $config->appSecret, $config->accessToken); if (in_array('--dump', $_SERVER['argv'])) { Api::instance()->setLogger(new CurlLogger(STDERR)); } $get_class_name = function($object) { return (new ReflectionClass($object))->getShortName(); }; $delete_object = function(AbstractCrudObject $object) use ($get_class_name) { echo sprintf( ' > Deleting %s %d', $get_class_name($object), $object->{AbstractCrudObject::FIELD_ID}).PHP_EOL; try { $object->deleteSelf(); } catch (Exception $e) { echo sprintf(" > Deletion failed with %s: %s", $get_class_name($e), $e->getMessage()).PHP_EOL; } }; $clean_edge = function($cursor_provider) use ($get_class_name, $delete_object) { try { $cursor = call_user_func($cursor_provider); if (!$cursor instanceof Cursor) { throw new ErrorException(sprintf( "Provider returned instance of %s", $get_class_name($cursor))); } $cursor->setUseImplicitFetch(true); foreach ($cursor as $object) { if (!$object instanceof AbstractCrudObject) { throw new ErrorException(sprintf( "Provider returned instance of %s, not a CRUD object", $get_class_name($object))); } $trait = 'FacebookAds\Object\Traits\CannotDelete'; if (in_array($trait, (new ReflectionClass($object))->getTraitNames())) { break; } $delete_object($object); } } catch (Exception $e) { echo sprintf(" > Edge iteration failed with %s: %s", $get_class_name($e), $e->getMessage()).PHP_EOL; } }; $account = new AdAccount($config->accountId); $reflection = new ReflectionClass($account); foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { if ( strncasecmp($method->getName(), 'get', 3) !== 0 || preg_match('/@return\s+Cursor/', $method->getDocComment()) !== 1 ) { continue; } echo sprintf(' > Fetching AdAccount::%s', $method->getName()).PHP_EOL; $clean_edge($method->getClosure($account)); }