declare global namespace variable from TypeScript - meteor

How to explicitly declare a variable in the global namespace from typescript?
I need the compiler to generate the following javascript code:
MyExtension = someFunction()
unfortunately, I can only have it generate
var MyExtension = someFunction()
This comes to an issue with the latest version (still in rc) of meteor packages.
Meteor introduced a way to scope namespaces in packages - the issue is, the variable needs to be defined in the global namespace (which meteor reroutes to its own Package object).
There is a video about it at https://www.eventedmind.com/posts/meteor-linker-package-namespacing.
Is there some kind of global keyword available or in the plans?

Use the declare keyword. These are known as ambient declarations.
declare var MyExtentention:any;

Related

Export Javascript symbols in a library using the Google Closure Compiler

I'm missing something when it comes to exporting symbols using the Google Closure Compiler. What I want to accomplish is to create a Javascript library and export only those functions that clients can use.
The documentation states to use the #export annotation on those functions and properties that you want to export. But it also states that you must define both goog.exportSymbol and goog.exportProperty with the same method signature in their own codebase. So for example:
/** #export */
var DoSomething = (function () {
});
goog.exportSymbol('DoSomething', goog.exportSymbol);
I downloaded the closure library and it does contain a base.js file that contains the goog.exportSymbol function.
But how do you include base.js? All examples show it being included within a web page. But I don't want a web page and the compiler works with javascript code and not web pages.
When the --generate_exports flag is used, the compiler adds the appropriate goog.exportSymbol or goog.exportProperty call for each occurrence of #export. This is why the symbols must be included in the compilation.
To include base.js in your compilation, simply pass it to the compiler using the --js flag before you pass in your own code. There's nothing magic going on here - it's just a javascript library.

meteor iron-router and getting the names of all routes for the accounts-entry package

The popular accounts-entry package has an iron-router related bug in it. I believe the later versions of iron-router changed to work better as middleware and so a call to Router.routes
At line 87 of this file the following code is used:
_.each Router.routes, (route)->
exclusions.push route.name
# Change the fromWhere session variable when you leave a path
Router.onStop ->
# If the route is an entry route, no need to save it
if (!_.contains(exclusions, Router.current().route?.getName()))
Session.set('fromWhere', Router.current().path)
Unfortunately it does not seems like doing an _.each on Router.routes is a solution that works anymore because Router.routes does not return and object with .name properties in it.
How would you get the name of all the routes with the latest iron-router?
This one is a little tricky : in the latest version of iron:router, Router.routes is now defined as an array of functions.
Thing is, functions already have a default name property in JS which contains the name the function was assigned on definition.
var myFunc = function funcName(){...};
console.log(myFunc.name); // == "funcName"
Fortunately, there is a getName method defined on the route items of the array and you can use this piece of code to iterate over all routes and get their name :
_.each(Router.routes, function(route){
console.log(route.getName());
});

trying to use Symfony component and having name space issues

So i am starting a new project and want to use some of the Symfony Components. I have not used name spaces before with PHP, put i am familiar with the concept from my work in java.
i have this simple piece of code and when i run it i get the error:
PHP Fatal error: Class 'Symfony\Component\CssSelector\XPath\Translator' not found in /home/me/scrapes/Symfony/Component/CssSelector/CssSelector/CssSelector.php on line 52
I am thinking it my lack of knowledge of the name space thing.
/home/me/scrapes/Symfony/Component/CssSelector/CssSelector/XPath/Translator.php does exist.
<?php
set_include_path('/home/me/html/inc');
require 'functions.php';
require 'Symfony/Component/DomCrawler/Crawler/Crawler.php';
require 'Symfony/Component/CssSelector/CssSelector/CssSelector.php';
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\CssSelector\CssSelector;
$crawler = new Crawler();
$crawler->addContent('<html><body><p>Hello World!</p></body></html>');
print $crawler->filter('body > p')->text();
curl_close($ch);
require 'cleanup.php';
?>
thanks for any help
I think this is caused because, if you look in the files you required (for example Symfony/Component/DomCrawler/Crawler/Crawler.php) you'll see that those files use their own use statements (to load other classes).
Namespaces
Let's start with the namespaces. A namespace is used to easily create classes with the same name in different packages. Let's say I have a package called Foo and a package called Bar. Both packages contain a Client class that is used to do some client work (one to call the Google Maps API for example and the other to call the Facebook Graph API). Let's also assume neither of the packages uses namespaces.
If I execute the following code:
<?php
require 'Foo/Client.php';
require 'Bar/Client.php';
This is not going to work, because both packages declare a Client class. Oops, how is PHP going to know which Client class to use if you do this?
<?php
$client = new Client();
It's not going to know which Client to use, so it gives up and throws an error.
If you use namespaces (declared using the namespace keyword in PHP at the top of your file, directly below <?php) you can prevent this from happening. The Foo package can create a Client class in the Foo namespace and the Bar package in the Bar namespace. Now we can
actually use both files and create a client:
<?php
require 'Foo/Client.php'
require 'Bar/Client.php'
$fooClient = new Foo\Client();
$barClient = new Bar\Client();
This will work fine.
I think you might have encountered the Foo_Client notation in older PHP libraries. This is an old way to create namespaces before PHP natively supported them.
"But", I hear you say, "it's quite cumbersome to write Foo\Bar\Baz\Client() every time I want to instantiated a class".
It is, and that's where the use keyword comes in. When using the use keyword, I can tell PHP I want to use a specific client and just use the class name, like so:
<?php
require 'Foo/Bar/Baz/Client.php'
use Foo\Bar\Baz\Client;
$client = new Client();
This will work, if you use the use Foo\Bar\Baz\Client statement, because you tell PHP "Okay, I want to use the Client class from the Foo\Bar\Baz namespace to be used when I use the Client class.
Autoloading
Now, what happens if you use a lot of different classes and you seperated them into several files (which you should do). You get a lot of different require and use statements on the top of a file. That's where autoloading comes in.
There has been a spl_register_autoloader function for quite some time in PHP. This function is used by PHP to find out which files to use when you instantiate a class that is not known because you did not require the file. This function is used both when creating a class, or, and this is the key part when you use a class.
And that's what's happening in your code. You don't have an autoloader registered that can translate the use statements in the files you required to actual class declarations.
Great, how do I fix it?
To fix it, I suggest you read up on the PHP-FIG and PSR-4. These people created standards (which you can follow but are not obliged to). To create easy to use libraries, such as the Symfony component. After you've done that, read up on Composer. After you've done this, you can drop the require statements from your code and use Composer to autoload all the classes you need.

Class not found Symfony2

I've installed the ffmpeg.so extension on my server. I want to use the ffmpeg-php library in my Symfony 2 application. If I do:
$ffmpeg = new ffmpeg_movie('movie.flv');
In a standalone php file, it works beautifully. But if I put the same code into my Symfony2 controller, I get
Fatal error: Class 'Example\ExampleBundle\Controller\ffmpeg_movie' not found in...
It must have to do with Symfony's namespace options, but I'm not sure how to resolve it.
When you're within a particular namespace, any class references without a namespace will be treated as a class local to that namespace. In your example, its treating ffmpeg_movie as a Example\ExampleBundle\Controller namespace class.
If you want to access another namespace, which includes the global namespace (which includes PHP classes, interfaces, as well as any custom defined global items), you have two choices.
Access it using it's full namespace (which is \ for global) & class name:
$obj = new \ffmpeg_movie;
$obj = new \DateTime;
Reference the external class using use:
use ffmpeg_movie;
use DateTime as AwesomeDateTimeClass;
$obj = new ffmpeg_movie;
$obj = new AwesomeDateTimeClass;
Oops, turns out you can just add
use ffmpeg_movie;
At the top of your controller, same as any other class. Silly me.

Getting error: Non-static method Phactory\Sql\Phactory::reset() should not be called statically

I have a trivially small PHPUnit test that looks like this:
<?php
namespace VNN\PressboxBundle\Tests\Entity;
namespace VNN\PressboxBundle\Entity;
use VNN\PressboxBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Phactory\Sql\Phactory;
class UserTest extends EntityTest
{
public function testCreate()
{
Phactory::reset();
}
}
When I try to run it, I get this:
There was 1 error:
1) VNN\PressboxBundle\Entity\UserTest::testCreate
ErrorException: Runtime Notice: Non-static method Phactory\Sql\Phactory::reset() should not be called statically, assuming $this from incompatible context in /Users/jason/Web/pressbox/src/VNN/PressboxBundle/Tests/Entity/UserTest.php line 13
What's up with that? All the docs call it statically.
I'm doing this on Symfony 2.0, if that makes a difference.
The documentation says you should be using the top-level Phactory class directly under lib/--not the individual implementations such as Phactory/Sql/Phactory which get instantiated based on the PDO object you pass to setConnection. Change
use Phactory\Sql\Phactory;
to
require_once 'Phactory/lib/Phactory.php';
The main class is in the global namespace and doesn't require a use statement.
https://github.com/chriskite/phactory/issues/30
From the code, setConnection, define and create are not static functions but the README and website guide do not reflect that.
e.g. test code
https://github.com/chriskite/phactory/blob/next/tests/Phactory/Sql/PhactoryTest.php
use Phactory\Sql\Phactory;
...
$this->pdo = new \PDO("sqlite:test.db");
$this->phactory = new Phactory($this->pdo);
$this->phactory->define('user');
$this->phactory->reset();
I don't know when it has been changed.
Too late anyways...
The current version, 0.3.2, is not backward compatible with the static method structure that is documented.
Here is the breaking commit: https://github.com/chriskite/phactory/commit/d3b60eeedea955ab7b5803ec29446d19888d3849
Unfortunately, the documentation has not been updated on http://phactory.org and the pearhub repo is no longer available.
I would look to the tests for examples: https://github.com/chriskite/phactory/blob/next/tests/Phactory/Sql/PhactoryTest.php

Resources